程序每次到stmt.executeUpdate()是就回出错,下面是一小段代码: 
Class.forName("com.mysql.jdbc.Driver"); 
Connection connection = DriverManager.getConnection(url); 
connection.setAutoCommit(false); 
String sql ="insert into article values(null,0,?,?,?,now(),0)"; 
PreparedStatement stmt2 = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); 
Statement stmt = connection.createStatement(); 
    stmt2.setInt(1,-1); 
stmt2.setString(2,title); 
cont=cont.replaceAll("\n"," <br>"); 
stmt2.setString(3,cont); 
stmt2.executeUpdate(); 
ResultSet rsKey = stmt2.getGeneratedKeys(); 
rsKey.next(); 
int key = rsKey.getInt(1); 
        rsKey.close(); 
        stmt.executeUpdate("update article set id ="+ key + "where id=" + key);//在这老是出错。。 
        connection.commit(); 
connection.setAutoCommit(true); 
stmt2.close();                
stmt.close(); 
connection.close(); 
请大家帮忙谢谢。

解决方案 »

  1.   

    出错信息是这样的:
    错误信息是这样的: 
    HTTP Status 500 - -------------------------------------------------------------------------------- type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /ShowAticleTree.jsp at line 82 79: rsKey.next(); 
    80: int key = rsKey.getInt(1); 
    81:    rsKey.close(); 
    82:    stmt.executeUpdate("update article set id ="+ key + "where id=" + key); 
    83:      connection.commit(); 
    84: connection.setAutoCommit(true); 
    85: stmt2.close();                
    Stacktrace: 
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
    root cause javax.servlet.ServletException: 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 'id=221' at line 1 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779) 
    org.apache.jsp.ShowAticleTree_jsp._jspService(ShowAticleTree_jsp.java:220) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
    root cause 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 'id=221' at line 1 
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975) 
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600) 
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695) 
    com.mysql.jdbc.Connection.execSQL(Connection.java:2998) 
    com.mysql.jdbc.Statement.executeUpdate(Statement.java:1071) 
    com.mysql.jdbc.Statement.executeUpdate(Statement.java:1005) 
    org.apache.jsp.ShowAticleTree_jsp._jspService(ShowAticleTree_jsp.java:137) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs. 
    -------------------------------------------------------------------------------- Apache Tomcat/6.0.14
      

  2.   

    You have an error in your SQL syntax   sql语法错误 
      

  3.   

    sql语法错误,检查一下sql语句。
      

  4.   

     stmt.executeUpdate("update article set id ="+ key + "where id=" + key); 
    貌似少了个空格stmt.executeUpdate("update article set id ="+ key + " where id=" + key); 
      

  5.   

    where前面加空格啦,细节决定成败啊