Reputation: 61
I am trying to get rows from a MySQL table with JSP in Eclipse. My Tomcat server is started, jConnector is in JRE Library and my code is:
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 is :
<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();
%>
And final result is
error: root cause
javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Upvotes: 0
Views: 1894
Reputation: 1108732
jConnector is in JRE Library
This is not correct. You should drop the JAR file in web project's /WEB-INF/lib
folder. That should be basically all you need to do. Everything in this folder ends up in webapp's runtime classpath. If you have also fiddled around in project's Build Path properties, then I recommend to undo that all. It would otherwise possibly break or confuse something else now or in the future.
Unrelated to the concrete problem: writing Java code in a JSP file is strongly discouraged since a decade. I recommend to write Java code in normal Java classes and learn about servlets to control the HTTP requests.
Upvotes: 2