我正在尝试在 Eclipse 中使用 JSP 从 MySQL 表中获取行。我的 Tomcat 服务器已启动,jConnector 在 JRE 库中,我的代码是:
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public Connection getDBConnection() throws Exception{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","myuname","mypass");
return conn;
}
}
JSP是:
<jsp:useBean id="dbConn" scope="request" class="dbConn.DBConnection"/>
<%
Connection conn=dbConn.getDBConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
%>
<html>
...
<body>
<%while (rs.next()){ %>
<h1><%=rs.getString(2) %></h1>
<%} %>
</body>
</html>
<%
if(conn!=null)
conn.close();
%>
最后的结果是
错误:根本原因
javax.servlet.ServletException:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver