我正在尝试使用 Java Struts1 建立 Mysql 与数据库的连接。这里是代码片段。我还将 mysql-connector-java-5.0.8-bin.jar 添加到 web-inf / lib 文件夹。 同样将其添加到classpath变量中。
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class select {
public void connection() {
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}.
这是错误讯息。我错过了哪些步骤。请尽快建议。
-------- MySQL JDBC Connection Testing ------------
Where is your MySQL JDBC Driver?
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Com.Candidjava.select.connection(select.java:14)
at Com.Candidjava.LoginAction.execute(LoginAction.java:26)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
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:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
试试这是jdbc连接到mysql数据库的一个例子:
import java.sql.DriverManager;
public class Connection {
private static java.sql.Connection conex = null;
public static java.sql.Connection getConnectionInstance() {
if (conex == null) {
try {
// connection to database
Class.forName("com.mysql.jdbc.Driver");
conex = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/databaseName","root",
"MySQLPassword");
return conex;
} catch (Exception e) {
}
}
return conex;
}
}
确保您已打开安装期间选择的端口。
答案 1 :(得分:0)
将mysql-connector-java-5.0.8-bin.jar添加到tomcat安装目录中。当右键单击您的项目并选择构建路径然后库。和内部库Apache Tomcat你应该找到你的连接器jar然后只有你的mysql将工作