I am trying to connect to MySQL service using the following code, but it cannot initialize the database class:
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
public class Database {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final BasicDataSource dataSource = new BasicDataSource();
static {
dataSource.setDriverClassName(JDBC_DRIVER);
dataSource.setUrl(ServerConfig.DB_URL);
dataSource.setUsername(ServerConfig.DB_USERNAME);
dataSource.setPassword(ServerConfig.DB_PASSWORD);
}
...
}
Here's the ServerConfig class:
public class ServerConfig {
public static final String DB_URL = "jdbc:mysql://localhost:3306/relay";
public static final String DB_USERNAME = "my_username";
public static final String DB_PASSWORD = "mypassword";
public static final boolean DEBUG = true;
}
Here's the stack trace:
java.lang.NoClassDefFoundError: Could not initialize class com.relayapp.server.database.Database
at com.relayapp.server.model.User.add(User.java:125)
at com.relayapp.server.request.AddUser.processRequest(AddUser.java:51)
at com.relayapp.server.request.RelayRequestServlet.doPost(RelayRequestServlet.java:55)
at com.relayapp.server.request.RelayRequestServlet.doGet(RelayRequestServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)
The user credentials seem to be correct, and I can run the mysql
command successfully. What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire