m0_67345796 2023-04-06 13:27 采纳率: 44.4%
浏览 23
已结题

jsp 链接MySQL数据库使用excuteUpdate()方法出错

连接数据库时添加用户信息功能可以实现,修改就不能实现了


HTTP Status 500 - An exception occurred processing JSP page /doxiugai.jsp at line 45
type Exception report

message An exception occurred processing JSP page /doxiugai.jsp at line 45

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 /doxiugai.jsp at line 45

42:                  tmt.setString(3, c);
43:                  tmt.setString(4, d);       
44:                  tmt.setString(5, id);
45:                     int rst=tmt.executeUpdate();  
46:                      if (rst!= 0){  
47:                            out.println("<script type='text/javascript'>alert('鐢ㄦ埛淇敼鎴愬姛锛�);window.location.href='123456.jsp';</script>");                     
48:                      }


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'add=null, num=null, js=null WHERE id='2'' at line 1
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.doxiugai_jsp._jspService(doxiugai_jsp.java:128)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'add=null, num=null, js=null WHERE id='2'' at line 1
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
    com.mysql.jdbc.Util.getInstance(Util.java:386)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2675)
    com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1915)
    com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2136)
    com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2070)
    com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5187)
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2055)
    org.apache.jsp.doxiugai_jsp._jspService(doxiugai_jsp.java:114)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.30 logs.

这是实现修改页面的代码


<%@ page language="java" import="java.util.*,java.sql.*,java.net.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    
    <title>My JSP 'doxiugai.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    

  </head>
  
  <body>
   <%
   request.setCharacterEncoding("utf-8");
        String a = request.getParameter("name");
        String b = request.getParameter("add");
        String c = request.getParameter("num");
        String d = request.getParameter("js");
       String id = (String) session.getAttribute("id");
      Class.forName("com.mysql.jdbc.Driver");//加载驱动   
       Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jz?useSSL=false","root","root");
      PreparedStatement pStmt=conn.prepareStatement("select * from yg where id=?");  
      pStmt.setString(1, id);
       ResultSet rs = pStmt.executeQuery(); 
                 PreparedStatement tmt = conn.prepareStatement("update yg set name=?,add=?, num=?, js=? WHERE id=?");  
                 tmt.setString(1, a);
                 tmt.setString(2, b);
                 tmt.setString(3, c);
                 tmt.setString(4, d);       
                 tmt.setString(5, id);
                    int rst=tmt.executeUpdate();  
                     if (rst!= 0){  
                           out.println("<script type='text/javascript'>alert('用户修改成功!');window.location.href='123456.jsp';</script>");                     
                     }
     
%>
  </body>
</html>

  • 写回答

2条回答 默认 最新

  • allyfireshen 2023-04-06 13:49
    关注

    sql语句错了,add是mysql的预留字(关键字),用作表名或者字段名时,在sql语句中需要用反引号包裹,修改如下:

    PreparedStatement tmt = conn.prepareStatement("update yg set name=?,`add`=?, num=?, js=? WHERE id=?");  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 创建了问题 4月6日

悬赏问题

  • ¥20 如何解决访问网址被屏蔽的情况
  • ¥15 本地运行的vue2项目,使用内网穿透,前端界面一直转圈圈
  • ¥20 powerbuilder datawindow控件导出Excel数据,可不可以不自动覆盖原数据,而是在后面新插入入数据。
  • ¥100 无轴承永磁同步电机控制
  • ¥15 eps里添加本地倾斜模型
  • ¥15 telegram 问题
  • ¥15 nrf52810-c三个a 程序
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止