I cannot connect jsp pages to mysql server
I am using tomcat 6.0 This is my catalina home -> C:\P开发者_高级运维rogram Files\Apache Software Foundation\Tomcat 6.0
I am using jdk 1.6 This is my JAVA_HOME C:\Program Files\Java\jdk1.6.0_24
I am using windows 7
OK i wrote a jsp page This is the code Its saved in -> C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\myapp\wow.jsp
<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:3306/unni";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>
<%
Class.forName("com.mysql.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "footyfreak");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM unni");
while (rs.next()) {
out.println(rs.getString("player")+"");
}
rs.close();
%>
I downloaded connector and copied it to :- C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\myapp\WEB-INF\lib\mysql-connector-java-5.1.15-bin.jar
and also to C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-connector-java-5.1.15-bin.jar
Then when I hit url http://localhost:8080/myapp/wow.jsp
I get an error as -------------->
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /wow.jsp at line 12
9:
10:
11: <%
12: Class.forName("com.mysql.Driver").newInstance();
13: connection = DriverManager.getConnection(connectionURL, "root", "footyfreak");
14: statement = connection.createStatement();
15: rs = statement.executeQuery("SELECT * FROM unni");
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:519)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.Driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
org.apache.jsp.wow_jsp._jspService(wow_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.ClassNotFoundException: com.mysql.Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.wow_jsp._jspService(wow_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.32 logs.
Apache Tomcat/6.0.32
My classpath details are
variable : CLASSPATH
value : C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar ; C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-connector-java-5.1.15-bin.jar;
Help me out. I have been sitting on this for days.
I did just as in the documentation.
Is there nething to be done apart from whats mentioned here?
java.lang.ClassNotFoundException: com.mysql.Driver
The class name is wrong. It is com.mysql.jdbc.Driver
.
Class.forName("com.mysql.jdbc.Driver");
That newInstance()
call is by the way superfluous. Please note that putting all that code inside a JSP file is a poor practice. That code should usually go in normal Java classes which you use in a servlet. Also note that the CLASSPATH
environment variable is totally ignored when running Tomcat.
See also:
- Java connectivity with MySQL
- How to avoid Java code in JSP?
connection = DriverManager.getConnection(connectionURL, "root", "footyfreak");
In this connectionURL statement you want to give your database name. Example:
String st="jdbc:mysql://localhost:3306/staff";
Connection con=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(st,"root","admin" );
In this example, staff is my database name.
After giving like this, I didnt get that error message.
Instead of adding external jar copy the jar file to the following location
WEB-INF/lib
精彩评论