嗨,我是jsp&的新手。试图从mysql db显示一些数据。为此我已经安装了JDK1.7,MySQL Server 5.5,mysql-connector-java-5.1.19-bin.jar作为tomcat 7服务器上的连接器。我在%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数据库。 感谢。
答案 0 :(得分:3)
使用com.mysql.jdbc.Driver
类(所谓的JDBC驱动程序)而不是org.gjt.mm.mysql.Driver
进行连接。需要在类路径中添加任何第三方API才能使用它。因此,需要在Tomcat中显示JAR。因此,我建议将mysql-connector-java-5.1.19-bin.jar
放置在项目的/WEB-INF/lib
目录的物理位置,在您的情况下 ws 。
在早些时候,如果jar不在物理位置,我会遇到ClassNotFoundException
。然后重启Tomcat应该可以工作。
答案 1 :(得分:2)
将mysql_connector / j驱动程序jar文件放在类路径中。然后使用
连接Class.forName("com.mysql.jdbc.Driver");
你的连接url也应该有mysql端口3306
String connectionURL = "jdbc:mysql://localhost:3306/test?user=root;password=sumant";