请各位帮忙看看我的jsp连接mysql哪里出了问题拉
故障:JSP连不上MYSQL数据库!!
配置:
JDK5.0.11 + tomcat6.0 + mysql5.0 分别测试安装成功,运行.
然后下载驱动mm.mysql,(这个解释一下,没有用最新那个什么驱动是因为我做的毕业设计上是用这个驱动).然后解压出一个文件org\gjt\mm\mysql,放到这个路径C:\Tomcat 6.0\webapps\ROOT\WEB-INF\classes\org\gjt\mm\mysql
测试代码:
<%@ page contentType= "text/html;charset=gb22 " %>
<%@ page import= "java.sql.* " %>
<html>
<body>
<%
Class.forName( "org.gjt.mm.mysql.Driver ").newInstance();
String url= "jdbc:mysql://localhost/bookstore?user=depthman& "+ "password=depthman&useUnicode=true;characterEncoding=8859_1 "
//depthman是mysql的用户名,密码也是depthman,我不知道这一句写的对不对!?
bookstore是已经存在的数据库.
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
String sql= "select * from bookstore; ";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){%>
第一个字段内容为: <% =rs.getString(1) %>
第二个字段内容为: <% =rs.getString(2) %>
<%}%>
<% out.print( "数据库操作成功,恭喜你!!! "); %>
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
错误:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 7 in the jsp file: /test.jsp
Syntax error, insert "; " to complete LocalVariableDeclarationStatement
4: <body>
5: <%
6: Class.forName( "org.gjt.mm.mysql.Driver ").newInstance();
7: String url= "jdbc:mysql://localhost/bookstore?user=depthman& "+ "password=depthman&useUnicode=true;characterEncoding=8859_1 "
8: Connection conn=DriverManager.getConnection(url);
9: Statement stmt=conn.createStatement();
10: String sql= "select * from bookstore; ";
An error occurred at line: 13 in the jsp file: /test.jsp
Syntax error on token "= ", @ expected
10: String sql= "select * from bookstore; ";
11: ResultSet rs=stmt.executeQuery(sql);
12: while(rs.next()){%>
13: 第一个字段内容为: <% =rs.getString(1) %>
14: 第二个字段内容为: <% =rs.getString(2) %>
15: <%}%>
16: <% out.print( "数据库操作成功,恭喜你!!! "); %>
An error occurred at line: 13 in the jsp file: /test.jsp
Syntax error on token(s), misplaced construct(s)
10: String sql= "select * from bookstore; ";
11: ResultSet rs=stmt.executeQuery(sql);
12: while(rs.next()){%>
13: 第一个字段内容为: <% =rs.getString(1) %>
14: 第二个字段内容为: <% =rs.getString(2) %>
15: <%}%>
16: <% out.print( "数据库操作成功,恭喜你!!! "); %>
An error occurred at line: 14 in the jsp file: /test.jsp
Syntax error on token "= ", @ expected
11: ResultSet rs=stmt.executeQuery(sql);
12: while(rs.next()){%>
13: 第一个字段内容为: <% =rs.getString(1) %>
14: 第二个字段内容为: <% =rs.getString(2) %>
15: <%}%>
16: <% out.print( "数据库操作成功,恭喜你!!! "); %>
17: rs.close();
An error occurred at line: 14 in the jsp file: /test.jsp
Syntax error on token(s), misplaced construct(s)
11: ResultSet rs=stmt.executeQuery(sql);
12: while(rs.next()){%>
13: 第一个字段内容为: <% =rs.getString(1) %>
14: 第二个字段内容为: <% =rs.getString(2) %>
15: <%}%>
16: <% out.print( "数据库操作成功,恭喜你!!! "); %>
17: rs.close();
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
谢谢你能看完,请给点意见,小生感激不尽!
[解决办法]
你先确定用depthman,depthman在查询分析器上能否登陆.
错误显示的是你的帐户没有获得授权访问阿.
或者删除帐户后重新建.
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html