我試圖將我的Java Web應用程序連接到mysql服務器。我正在部署在tomcat上。我有一個我創建的JDBC類,它返回一個正常的Java主程序中的連接對象。但是,當我在部署的tomcat應用程序上使用JDBC類時,它將返回null。我不明白。它將主要返回連接,但不會在servlet/Web應用程序中。謝謝。代碼:JDBC連接對象在Servlet/Web應用程序中返回null
Registration.Servlet:
Jdbc dbConn = new Jdbc();
Connection con = dbConn.connect();
String insert_member = "INSERT INTO Members (id, name, address, dob, "
+ "dor, status, balance)" + " VALUES (?, ?, ?, ?, ?, ?, ?)";
//String c = con.toString();
//con = null for some reason
pstmt = con.prepareStatement(insert_member);
pstmt.setString(1, "coo");
pstmt.setString(2, "aa");
pstmt.setString(3, "26 road");
pstmt.setDate(4, new java.sql.Date(new Date().getTime()));
pstmt.setDate(5, new java.sql.Date(new Date().getTime()));
pstmt.setString(6, "APPLIED");
pstmt.setFloat(7, 10);
pstmt.execute();
JDBC類:
public class Jdbc {
Connection connection;
Statement statement;
ResultSet rs;
public Jdbc() {
String db = "xyz_assoc";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection
("jdbc:mysql://localhost:3306/"+db.trim(), "root", "");
}
catch(ClassNotFoundException cfe){
}
//SQL exception
catch (SQLException ex) {
Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
}
}
//open connection to database
public Connection connect(){
return connection;
}
//execute mysql queryS
public ResultSet executeQuery(String sql_query) throws SQLException {
statement = null;
statement = connection.createStatement();
rs = statement.executeQuery(sql_query);
return rs;
}
//close connection to database
//destroy any remaining objects
public void close() throws SQLException{
statement.close();
rs.close();
connection.close();
}
}
}
堆棧跟蹤:
2016年11月30日上午11點12分十八秒org.apache.catalina .core.ApplicationContext log INFO:SessionListener:contextDestroyed()Nov 30,2016 11:12:18 AM org.apach e.catalina.core.ApplicationContext log INFO:ContextListener: contextDestroyed()2016年11月30日11:12:23 AM org.apache.catalina.core.ApplicationContext日誌信息:ContextListener: contextInitialized()2016年11月30日11 :12:23 AM org.apache.catalina.core.ApplicationContext log INFO:SessionListener: contextInitialized()2016年11月30日11:12:23 AM org.apache.catalina.core.ApplicationContext日誌信息:ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', '[email protected]')11月30,2016 11:12:59 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE:servlet [註冊]與Servlet.service()中的 路徑[/ XYZGroup]拋出異常[Servlet的執行拋出異常 ]與根源拋出java.lang.ClassNotFoundException: model.Jdbc在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) 在model.MemberManager.addMember(MemberManager.java:46)在 web.Registration.processRequest(Registration.java:52)在 web.Registration.doPost(Registration.java:101)at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)at org.apache.ca talina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在 org.apache.tomcat.websocket.server.WsFilter。的doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve。的java:122) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 在 有機.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve。的java:103) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 在 有機.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler .process(AbstractProtocol.java:611) at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316) at java.util.con current.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) 在 org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable。運行(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:745)