jsp 添加到数据库错误
小弟刚写的jsp页面,用来添加新用户和密码并显示,可是有点错误不是很明白
求解决方法,由于是新手,请说明原理,详细为好。。谢!
错误页面如下:
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 6 in the jsp file: /jdbc.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\test\org\apache\jsp\jdbc_jsp.java:59: unclosed character literal
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ ' ", ' "+pwd+ ' ") ");
^
An error occurred at line: 6 in the jsp file: /jdbc.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\test\org\apache\jsp\jdbc_jsp.java:59: unclosed character literal
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ ' ", ' "+pwd+ ' ") ");
^
An error occurred at line: 6 in the jsp file: /jdbc.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\test\org\apache\jsp\jdbc_jsp.java:59: unclosed character literal
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ ' ", ' "+pwd+ ' ") ");
^
An error occurred at line: 6 in the jsp file: /jdbc.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\test\org\apache\jsp\jdbc_jsp.java:59: unclosed string literal
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ ' ", ' "+pwd+ ' ") ");
^
An error occurred at line: 6 in the jsp file: /jdbc.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\test\org\apache\jsp\jdbc_jsp.java:63: ') ' expected
ResultSet rs=stmt.executeQuery( "select * from tbuser ");
^
5 errors
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
我写的jsp源码如下:
<html>
<head> <title> Add User to DB </title> </head>
<%@page import= "java.sql.* "%>
<body>
<pre>
<%
//Get parameters from request
String name,pwd;
name=request.getParameter( "name ");
pwd=request.getParameter( "pwd ");
try{
//add information to DB
Class.forName( "sun.jdbc.odbc.JdbcOdbcDrive ");
Connection con=DriverManager.getConnection( "jdbc:odbc:jspTest ");
Statement stmt=con.createStatement();
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ ' ", ' "+pwd+ ' ") ");
//query DB for display all users
ResultSet rs=stmt.executeQuery( "select * from tbuser ");
//rs.first();
out.println( "All user in DB! ");
while(rs.next()){
out.print( "user name: ");
out.print(rs.getString( "name ")+ " password: ");
out.println(rs.getString( "password "));
}
//clear,must do it
rs.close();
stmt.close();
con.close();
}catch(Exception ex){
out.println(ex.getMessage());
}
%>
前面有个html的静态页面:
代码如下:
<html>
<head> <title> UserAdd </title> </head>
<body>
<form method= "POST " action= "jdbc.jsp ">
<p align= "center "> 姓名
<input type= "text " name= "name " size= "20 "> </p>
<p align= "center "> 密码
<input type= "password " name= "pwd " size= "20 "> </p>
<p align= "center ">
<input type= "submit " value= "提交 " name= "B1 "> </p>
</form>
</body>
</html>
我用的是sql server2000dbms
os 是xp
跪求最佳答案。。。小弟感激不尽!!!
[解决办法]
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ ' ", ' "+pwd+ ' ") ");
改成
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ " ', ' "+pwd+ " ') ");
看还有几个错误
[解决办法]
不是Class.forName( "sun.jdbc.odbc.JdbcOdbcDrive ");
应该是JdbcOdbcDriver.
少了一个r哦~
[解决办法]
stmt2.executeUpdate( "insert into admin(用户名,密码,权限)values( ' "+v1+ " ', ' "+v2+ " ', ' "+v3+ " ') ");
上面是个例子,首先插入的参数是用 ' "+参数+ " ' 这个格式
然后values 前面对应的表字段的名称也要写上
[解决办法]
stmt.excuteUpdate( "insert into tbuser values( ' "+name+ ' ", ' "+pwd+ ' ") ");
引号和双引号错了
‘“+name+”’,
[解决办法]
1、2楼的集合!!!!!!!!!!呵呵。。。2个错!!!!!!!
[解决办法]
拼接SQL时,变量两边的双引号在里面,单引号在外面
[解决办法]
趁结分前回一把
楼主以后可以试试在IDE中开发(eclipse,jbuilder netbeans,idea等),有语法错误,直接可以提示.
不过一开始用简单的文本编辑工具有利于打好基础
[解决办法]
其实这个 ' " +的问题说大不大说小不小
这里应该懂它为好
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html