日期:2014-05-17  浏览次数:20800 次

jsp页面中用sql语句怎样添加一条记录?
也就是一个简单的注册验证页面,我的代码是这样的:
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="../error.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>无标题文档</title>
</head>
<%
String zID = request.getParameter("zID");
String zpassword = request.getParameter("zpassword");
String infom = "0";
Connection con = null;
Statement st = null;
ResultSet rs = null;
try
{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String url = "jdbc:mysql://localhost:3306/mydb1";
    con = DriverManager.getConnection(url,"root","123456");
    st = con.createStatement();
    rs = st.executeQuery("select * from userinfo where userid='"+zID+"'"); 
if(rs.next())
{
   response.sendRedirect("zhuce.jsp?infom=1");
}
else
{
   insert into userinfo(userid,password) values(zID,zpassword);//这句报错了
           response.sendRedirect("../index.jsp");
}
}
catch(Exception e)
{
  e.printStackTrace();
}
 finally
 {
     if(rs!=null)
 {
    try{ rs.close();}catch(Exception e){e.printStackTrace();}
  }
     if(st!=null)
 {
    try{ st.close();}catch(Exception e){e.printStackTrace();}
  }  
  if(con!=null)
 {
    try{ con.close();}catch(Exception e){e.printStackTrace();}
  } 
}
%>
<body>

</body>
</html>
从zhuce.jsp的表单中取得输入的字符串并在数据库中查找有无相同记录,若有则返回zhuce.jsp并携带参数提示用户,若无则加入该记录到数据库中并返回首页。
运行时insert into userinfo(userid,password) values(zID,zpassword);这句报500错误了...如果把这句注释掉除了无法添加新记录以外其他功能都能正常实现。
该怎样写语句才能顺利添加新记录呢?望各位指点,谢谢!
另外我初接触java web开发,只懂一些基础的jsp知识,如果能不用javabean或者框架什么的而是靠纯jsp和sql实现最好,目前要做这么一个登录模块,不是很大的系统而且时间紧,只用model 1体系结构解决问题就好,麻烦各位了!
SQL JSP 数据库 web java

------解决方案--------------------
 insert into userinfo(userid,password) values(zID,zpassword);//这句报错了
java 是不能直接执行这种语句的,要把它当成一条sql来执行。
eg:
String sql = " insert into userinfo(、、、、、";  //拼sql
st.execute(sql);
------解决方案--------------------
st.executeQuery("insert into userinfo(userid,password)
 values("+zID+","+zpassword+")");
------解决方案--------------------
没有插入数据库,String zID = request.getParameter("zID");
String zpassword = request.getParameter("zpassword");
这个里面是否有值,或者数据库是否已经别关闭了。。。。
------解决方案-