0

嗨,我是 jsp 的新手,正在尝试显示来自 mysql db 的一些数据。为此,我在 tomcat 7 服务器上安装了 JDK1.7、MySQL Server 5.5、mysql-connector-java-5.1.19-bin.jar 作为连接器。我在%CATALINA_HOME%webapps/ROOT/ws/文件夹中创建了一个文件,即带有代码的index.jsp

<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:test?user=root;password=sumant";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>


<html><body>


<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "", "");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT name FROM store");

while (rs.next()) {
out.println(rs.getString("name")+"<br>");
}

rs.close();
%>


</body></html>

在调用 url: http://localhost:8080/ws/index.jsp我得到

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 /ws/index.jsp at line 14

11: 
12: 
13: <%
14: Class.forName("org.gjt.mm.mysql.Driver").newInstance();
15: connection = DriverManager.getConnection(connectionURL, "", "");
16: statement = connection.createStatement();
17: rs = statement.executeQuery("SELECT name FROM store");


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

javax.servlet.ServletException: java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
    org.apache.jsp.ws.index_jsp._jspService(index_jsp.java:98)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Unknown Source)
    org.apache.jsp.ws.index_jsp._jspService(index_jsp.java:78)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

现在我不明白这里有什么问题。请帮我连接到mysql数据库。谢谢。

4

2 回答 2

3

使用com.mysql.jdbc.Driver类(所谓的 JDBC 驱动程序)而不是org.gjt.mm.mysql.Driver. 任何第三方 API 都需要添加到类路径中才能使用它。所以,这个 JAR 需要在 Tomcat 中可见。因此,我建议将mysql-connector-java-5.1.19-bin.jar物理位置放置在/WEB-INF/lib您的项目目录中,在您的情况下是ws

在早些时候,我经历过ClassNotFoundExceptionjar 是否不在物理位置。然后重新启动Tomcat应该可以工作。

于 2012-04-09T10:01:34.240 回答
2

将 mysql_connector/j 驱动程序 jar 文件放在类路径中。然后使用连接

Class.forName("com.mysql.jdbc.Driver");

您的连接网址也应该有 mysql 端口,即 3306

String connectionURL = "jdbc:mysql://localhost:3306/test?user=root;password=sumant";
于 2012-04-09T09:45:35.663 回答