I’m very new to development, and I’m attempting to run an old project on my local machine. However, when I try to run it, I encounter the error message: “java: package com.mysql.cj.jdbc does not exist.” I have configured Java 1.8 in the Project Structure settings, as well as in the Edit Configuration, since it was the version used by the previous developer. Any guidance or assistance on this issue would be greatly appreciated.
package com.kv.pg.instanceManager;
import java.sql.Connection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import com.kv.pg.commons.helper.PropertiesLoader;
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
public class DBManager{
private static volatile DBManager INSTANCE = null;
private static Logger logger = Logger.getLogger(DBManager.class);
private DBManager()
{
setUpDataSourceInContext();
}
public static DBManager getInstance()
{
if (INSTANCE == null)
{
synchronized (DBManager.class)
{
if (INSTANCE == null)
{
INSTANCE = new DBManager();
}
}
}
return INSTANCE;
}
public DataSource getDataSource()
{
DataSource dataSource = null;
try
{
InitialContext iniCtx = new InitialContext();
Context envCtx = (Context) iniCtx.lookup("java:comp/env/");
Object obj = envCtx.lookup("jdbc/PGIMDB");
if (obj != null)
{
dataSource = (DataSource) obj;
}
if (dataSource == null)
{
throw new Exception("Unable to find data source, please check database configurations.");
}
} catch (Exception exp)
{
logger.error("Error Getting Data Source" + exp.getMessage(), exp);
dataSource = null;
}
return dataSource;
}
public Connection getConnection()
{
Connection conn = null;
try
{
DataSource dataSource = getDataSource();
if (dataSource != null)
{
conn = dataSource.getConnection();
}
} catch (Exception exp)
{
logger.error("Error Getting Connection" + exp.getMessage(), exp);
// throw exp;
conn = null;
}
return conn;
}
/*
* public void close() { // DbUtils.closeQuietly(conn, stmt, rs); }
*/
@Bean
private DataSource getMySQLDataSource()
{
Properties props = null;
MysqlConnectionPoolDataSource ds = null;
try
{
logger.info("DataSourceFactory Try loading Database Properties");
props = PropertiesLoader.load("im.db.properties");
logger.debug("Properties Loaded::" + props);
ds = new MysqlConnectionPoolDataSource();
ds.setURL(props.getProperty("MYSQL_DB_URL"));
ds.setUser(props.getProperty("MYSQL_DB_USERNAME"));
ds.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
} catch (Exception exp)
{
logger.error("Error Creating DataSource" + exp.getMessage(), exp);
throw exp;
}
return ds;
}
private void setUpDataSourceInContext()
{
try
{
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:comp");
ic.createSubcontext("java:comp/env");
ic.createSubcontext("java:comp/env/jdbc");
ic.bind("java:comp/env/jdbc/PGIMDB", this.getMySQLDataSource());
} catch (Exception exp)
{
logger.error("Error Binding DataSource To context" + exp.getMessage(), exp);
// throw exp;
}
}
Pom.xml:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
can you share the maven
pom.xml
file or gradle build fileWhat Build System are u using? Maven or Gradle?
I am using Maven @Morty
@AndreiLisa I have updated it in the description
Why you dont use a starter to do connection with database instead of do it manually? I mean something like
spring-boot-data-jpa
orspring-boot-jdbc
it is more easy and increase development experience, great technologies for great peoplesShow 8 more comments