各位老大:
我写了一个望数据库里插入数据的jsp页面,可是运行老是出错,请各位老大看看,多多指教,感激不尽,我的数据库已经连接好了,如果我把这个程序改为查询就可以从数据库中读出数据,可插入就是不行,以下是程序:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%@ page session="true" %>
<%
if(request.getParameter("user")!=null&&!request.getParameter("user").equals("")){
String user1=request.getParameter("user");
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="123456";
//数据库名
String dbName="joss";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="insert into hot (user) values ('"+user1+"');
statement.executeUpdate(sql);
}%>
<html>
<body>
<script language="javascript">
function checkform(){
if(document.form1.user.value==""){
alert("用户名为空!");
return false;
}
return true;
}
</script>
<form name="form1" method="post" action="hot.jsp">
<table width="400" border="0" cellspacing="1" cellpadding="1" align="center">
<tr>
<td width="147" align="right">用户名:<br>
</td>
<td width="346" valign="top">
<input type="text" name="user" size="32" maxlength="64">
</td>
</tr>
<tr>
<td width="147" align="right"> </td>
<td width="246" valign="top">
<input type="submit" name="Submint" value="提交" onclick="javascript:return(checkform());">
<input type="reset" name="Submit2" value="重写">
</tr>
</table>
</form>
</body>
</html>
我用表单提交一个用户名,并把它插入到数据库joss的表hot中的user的字段中,可是老出错:以下是报的错误:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 10 in the jsp file: /hot.jsp
Generated servlet error:
String literal is not properly closed by a double-quote
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)