jsp插入数据问题
package action;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class actionmyinfo extends HttpServlet {
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("UTF-8");
String ud=(String)session.getAttribute("uname");
//String userid=new String(request.getParameter("userid").getBytes("UTF-8"),"UTF-8");
String realname=new String(request.getParameter("realname").getBytes("UTF-8"),"UTF-8");
String nickname=new String(request.getParameter("nickname").getBytes("UTF-8"),"UTF-8");
String sex=new String(request.getParameter("sex").getBytes("UTF-8"),"UTF-8");
String birthyear=new String(request.getParameter("birthyear").getBytes("UTF-8"),"UTF-8");
String birthmonth=new String(request.getParameter("birthmonth").getBytes("UTF-8"),"UTF-8");
String birthday=new String(request.getParameter("birthday").getBytes("UTF-8"),"UTF-8");
String mobile=new String(request.getParameter("mobile").getBytes("UTF-8"),"UTF-8");
String workplace=new String(request.getParameter("workplace").getBytes("UTF-8"),"UTF-8");
String address=new String(request.getParameter("address").getBytes("UTF-8"),"UTF-8");
String qq=new String(request.getParameter("qq").getBytes("UTF-8"),"UTF-8");
String signiature=new String(request.getParameter("signiature").getBytes("UTF-8"),"UTF-8");
Connection con=null;
Statement sm=null;
//ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost/schooldb";
con = DriverManager.getConnection(url, "root", "123");
sm = con.createStatement();
String sql="insert into userinfo (realname,nickname,sex,birthyear,birthmonth,birthday,mobile,workplace,address,qq,signiature) values ('"+realname+"','"+nickname+"','"+sex+"','"+birthyear+"','"+birthmonth+"','"+birthday+"','"+mobile+"','"+workplace+"','"+address+"','"+qq+"','"+signiature+"') where userid='"+ud+"'";
int ok=sm.executeUpdate(sql);
if(ok>0){
out.println("添加信息成功!<br>");
//out.println("你的个人详细信息如下:<br>");
//out.println("<table border='1'>");
//out.println("<tr><td>你的用户名:</td><td>userid</td></tr>");
//out.println("</table>");
}
else{
out.println("添加信息失败!");
}
sm.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
以上是处理前面页面插入数据库信息的代码
String ud=(String)session.getAttribute("uname");
uname是前面页面设置的session
userid是数据库里的字段 已经插入过了 这段代码是想在这个基础上继续插入其他的数据
但是执行以后却显示下面的出错信息 也不能插入数据
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where userid='qiang'' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2988)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:935)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:873)
at action.actionmyinfo.doPost(actionmyinfo.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
我不知道是怎么回事 请高手给指教
谢谢!!!
|