菜鸟提问:关于TOMCAT连接MYSQL的问题!!!
我写了一个简短的程序:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<html>
<body>
<%Class.forName( "com.jdbc.mysql.Driver ").newInstance();
String url = "jdbc:mysql://localhost/first?user=test&password=123456&useUnicode=true&characterEncoding=8859_1 ";
//first为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql= "select * from first ";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为: <%=rs.getString(1)%>
您的第二个字段内容为: <%=rs.getString(2)%>
<%}%>
<%out.print( "数据库操作成功,恭喜你 ");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
但是我执行的时候确老是出错如下:
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: Exception in JSP: /first.jsp:5
2: <%@ page import= "java.sql.* "%>
3: <html>
4: <body>
5: <%Class.forName( "com.jdbc.mysql.Driver ").newInstance();
6: String url = "jdbc:mysql://localhost/first?user=test&password=123456&useUnicode=true&characterEncoding=8859_1 ";
7: //firstΪÄãµÄÊý¾Ý¿âÃû
8: Connection conn= DriverManager.getConnection(url);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
javax.servlet.ServletException: com.jdbc.mysql.Driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:843)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:776)
org.apache.jsp.first_jsp._jspService(first_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.ClassNotFoundException: com.jdbc.mysql.Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1355)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1201)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:127)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:65)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
org.apache.jsp.first_jsp._jspService(first_jsp.java:47)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.
能否指点指点我解决问题,谢谢
[解决办法]
tomcat目录下的common下的bin下
[解决办法]
common/lib
[解决办法]
<%Class.forName( "com.jdbc.mysql.Driver ").newInstance();
String url = "jdbc:mysql://localhost/first?user=test&password=123456&useUnicode=true&characterEncoding=8859_1 ";
错误就在这几句里,一点一点核对,以确保无误
[解决办法]
根据提示,是驱动的问题。
1、将驱动jar文件拷到 %tomcat%/common/lib 下
2、将Class.forName( "com.jdbc.mysql.Driver ")用try-catch环绕
try {
Class.forName( "com.jdbc.mysql.Driver ");
} catch (ClassNotFoundException e) {
out.print(e);
}
try {
String url = "jdbc:mysql://localhost/first?user=test&password=123456 ";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
out.print(e);
}
[解决办法]
Class.forName( "com.jdbc.mysql.Driver ")改成Class.forName( "com.mysql.jdbc.Driver ")
[解决办法]
呵呵,楼上的很仔细啊,看出是驱动程序名写错了。
应该是com.mysql.jdbc.Driver
[解决办法]
呵呵,我都没看出来。。。兄弟你细心点。不然永远都是个菜鸟
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html