我有一个 DatabaseConnectionOpen.java 类,如下所示:
package db;
import java.sql.*;
public class DatabaseConnectionOpen {
static String dbHost = "jdbc:mysql://localhost:3306/javaweb?useSSL=false";
static String dbUsername = "user";
static String dbPassword = "user";
static Connection myConn = null;
static Statement myStmt = null;
static ResultSet myRes = null;
public void openConnection(){
try {
myConn = DriverManager.getConnection(dbHost, dbUsername, dbPassword);
}
catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet runQuery(String query){
try{
myStmt = myConn.createStatement();
myRes = myStmt.executeQuery(query);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
return myRes;
}
}
public void closeConnection(){
try {
if (myRes != null) myRes.close();
if (myStmt != null) myStmt.close();
if (myConn != null) myConn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
当我从名为ViewUsers.java的类中使用它时,它的工作正常,其代码如下:
package dbImplement;
import db.DatabaseConnectionOpen;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ViewUsers {
public static void main(String[] args) {
ResultSet myRes = null;
DatabaseConnectionOpen dbObject = new DatabaseConnectionOpen();
dbObject.openConnection();
try {
myRes = dbObject.runQuery("SELECT * FROM users");
while (myRes.next()) {
System.out.println(myRes.getString("fname") + " " + myRes.getString("lname"));
}
myRes = dbObject.runQuery("SELECT * FROM users");
while (myRes.next()) {
System.out.println(myRes.getString("fname"));
}
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
dbObject.closeConnection();
}
dbObject.closeConnection();
}
}
但是当我想从servlet使用DatabaseConnectionOpen.java时会出现问题,代码如下:
package servlets;
import db.DatabaseConnectionOpen;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet(name = "FirstServlet", urlPatterns = {"/msg"})
public class FirstServlet extends HttpServlet {
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ResultSet myRes = null;
DatabaseConnectionOpen dbObject = new DatabaseConnectionOpen();
dbObject.openConnection();
out.println("<h1>Hello Sevlet</h1>");
try {
myRes = dbObject.runQuery("SELECT * FROM users");
while (myRes.next()) {
out.println("<h3>"+myRes.getString("fname") + " " + myRes.getString("lname")+"</h3>");
}
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
dbObject.closeConnection();
}
dbObject.closeConnection();
}
}
它只打印&#34; Hello Servlet&#34;但不是h3部分,并说:
> java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/javaweb?useSSL=false
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at db.DatabaseConnectionOpen.runQuery(DatabaseConnectionOpen.java:28)
at servlets.FirstServlet.doGet(FirstServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Feb 16, 2017 2:14:30 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcat/webapps/manager
Feb 16, 2017 2:14:30 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcat/webapps/manager has finished in 100 ms
它说找不到驱动程序,但我有mysql连接器的外部库,而我的其他类(ViewUsers.java)可以同时工作,但不是Servlet。