jsp无法把数据插入到数据库
想要把数据插入到数据库中,但是一直不行,显示更新数据发生错误数据库可以连接,也可以查询表中的数据,但就是不能插入数据,请问这是什么原因啊a.jsp代码<%@pagecon...
想要把数据插入到数据库中,但是一直不行,显示更新数据发生错误
数据库可以连接,也可以查询表中的数据,但就是不能插入数据,请问这是什么原因啊
a.jsp代码
<%@page contentType="text/html;charset=gb2312" import="java.util.*,com.Bean.*"%>
<link rel="stylesheet" href="css/code43.css" type="text/css">
<HTML><HEAD>
<title>插入数据</title>
<script language="javascript">
function buy()
{
document.form1.submit();
}
function check()
{
if(document.form.namea.value=="")
{
alert("注册名不能为空");
document.form.namea.focus();
return false;
}
else
{
form.action="crecord";
form.target="_parent";
form.submit();
}
}
</script>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
</HEAD>
<BODY style=scrollbar-arrow-color:#227776;scrollbar-base-color:#C6DCDE text=#000000 bgColor=#ffffff leftMargin=0 topMargin=0 background="img/bg.gif" marginwidth="0" marginheight="0" >
<form name="form" method="post">
<div align="center">
<table width="551" height="482" border="1">
<tr>
<td height="37" align="right">*注册名:</td>
<td width="363"><input type="text" name="namea" />
(3到50个字符) </td>
</tr>
<tr>
<td height="43" align="right">真实姓名:</td>
<td>
<input type="text" name="nameb" />
(2到50个字符) </td>
</tr>
<tr>
<td height="82" colspan="2" align="center">
<input type="button" name="Submit" value="注册" onClick="check()">
<input type="button" name="Submit2" value="返回" onClick="window.history.go(-1);"> </td>
</tr>
</table>
</div>
</form>
</TD>
</BODY></HTML>
crecord.java代码
package com.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.Bean.*;
public class crecord extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("GBK");
PrintWriter out=response.getWriter();
String namea,nameb;
namea=request.getParameter("namea");
nameb=request.getParameter("nameb");
/*
out.println(namea);
out.println(nameb);
*/
dbconn dbcon=new dbconn();
String insertStr="insert into aaa(namea,nameb) values";
insertStr=insertStr+"('"+namea+"',";
insertStr=insertStr+"'"+nameb+"',";
//out.println(insertStr);
boolean flag=dbcon.executeUpdate(insertStr);
if(flag==true)
{
dbcon.destory();
out.print("<script language=\"javascript\">window.location.replace(\"usercenter.jsp\"); </script>");
}
else
{
dbcon.destory();
out.print("<script language=\"javascript\">window.history.go(-1);</script>");
}
}
} 展开
数据库可以连接,也可以查询表中的数据,但就是不能插入数据,请问这是什么原因啊
a.jsp代码
<%@page contentType="text/html;charset=gb2312" import="java.util.*,com.Bean.*"%>
<link rel="stylesheet" href="css/code43.css" type="text/css">
<HTML><HEAD>
<title>插入数据</title>
<script language="javascript">
function buy()
{
document.form1.submit();
}
function check()
{
if(document.form.namea.value=="")
{
alert("注册名不能为空");
document.form.namea.focus();
return false;
}
else
{
form.action="crecord";
form.target="_parent";
form.submit();
}
}
</script>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
</HEAD>
<BODY style=scrollbar-arrow-color:#227776;scrollbar-base-color:#C6DCDE text=#000000 bgColor=#ffffff leftMargin=0 topMargin=0 background="img/bg.gif" marginwidth="0" marginheight="0" >
<form name="form" method="post">
<div align="center">
<table width="551" height="482" border="1">
<tr>
<td height="37" align="right">*注册名:</td>
<td width="363"><input type="text" name="namea" />
(3到50个字符) </td>
</tr>
<tr>
<td height="43" align="right">真实姓名:</td>
<td>
<input type="text" name="nameb" />
(2到50个字符) </td>
</tr>
<tr>
<td height="82" colspan="2" align="center">
<input type="button" name="Submit" value="注册" onClick="check()">
<input type="button" name="Submit2" value="返回" onClick="window.history.go(-1);"> </td>
</tr>
</table>
</div>
</form>
</TD>
</BODY></HTML>
crecord.java代码
package com.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.Bean.*;
public class crecord extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("GBK");
PrintWriter out=response.getWriter();
String namea,nameb;
namea=request.getParameter("namea");
nameb=request.getParameter("nameb");
/*
out.println(namea);
out.println(nameb);
*/
dbconn dbcon=new dbconn();
String insertStr="insert into aaa(namea,nameb) values";
insertStr=insertStr+"('"+namea+"',";
insertStr=insertStr+"'"+nameb+"',";
//out.println(insertStr);
boolean flag=dbcon.executeUpdate(insertStr);
if(flag==true)
{
dbcon.destory();
out.print("<script language=\"javascript\">window.location.replace(\"usercenter.jsp\"); </script>");
}
else
{
dbcon.destory();
out.print("<script language=\"javascript\">window.history.go(-1);</script>");
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询