无法从jsp页面访问数据库保存信息

问题描述 投票:0回答:1

堆栈跟踪如下: 堆栈跟踪: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:599) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:478) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 根本原因

jakarta.servlet.ServletException: java.sql.SQLSyntaxErrorException: 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行“?,?,?)”附近使用的正确语法 org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:666) org.apache.jsp.add_005fstu_jsp._jspService(add_005fstu_jsp.java:197) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 根本原因

java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行“?,?,?)”附近使用的正确语法 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121) com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1334) com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2084) com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1245) org.apache.jsp.add_005fstu_jsp._jspService(add_005fstu_jsp.java:151) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

这是 HTML 代码:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style1.css"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Student Registration</title>
    </head>
    <body>
        <div class="container">
        <h2>Student Registration</h2>
        <form id="add_stu_form" action="add_stu.jsp" method="POST">
            
            <label for="enrol" style="color:black">Enrollment no:</label>
            <input type="text" name="enrol" id="enrol" placeholder="Enrollment Number" required>
            
            <label for="fullname" style="color:black">Name:</label>
            <input type="text" name="fullname" id="fullname" placeholder="Full Name" required>
            
            <!--button type="submit">Add Student</button-->
            <p class="submit">
                <input type="submit" name="action" value="Save"> 
                <input type="submit" name="action" value="Delete"> 
                <input type="submit" name="action" value="Update"> 
            </p>
            <script></script>
        </form>
    </div>
    </body>
</html>

这是jsp页面:

<%-- 
    Document   : add_stu
    Created on : 08-Apr-2024, 11:06:07 pm
    Author     : DC
--%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    Class.forName("com.mysql.cj.jdbc.Driver"); 
    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/ams","root","");
   
    long enrl = Long.parseLong(request.getParameter("enrol"));
    String name = request.getParameter("fullname");
    Statement s0 = con.createStatement();
    ResultSet rs0 = s0.executeQuery("SELECT `No` FROM `stu_att`");
    int size = 0;
        while (rs0.next()) {
            size++;
        }
    int count = size + 1;
    int result;
    String sql;
    String button = request.getParameter("action");
    if("Save".equals(button)){
        sql = "INSERT INTO `stu_att`(`No`, `name`, `enrl`) VALUES (?,?,?)";
        PreparedStatement ps1 = con.prepareStatement(sql);
        ps1.setInt(1, count);
        ps1.setString(2, name);
        ps1.setLong(3, enrl);
        result = ps1.executeUpdate(sql);
    }
    else if("Delete".equals(button)){
        long del = enrl; //button2;
        sql = "DELETE FROM `stu_att` WHERE `enrl`=?";
        PreparedStatement ps2 = con.prepareStatement(sql);
        ps2.setLong(1, del);
        result = ps2.executeUpdate(sql);
    }
    else if("Update".equals(button)){
        long upd = enrl; //button2;
        sql = "UPDATE `stu_att` SET `name`='?' WHERE `enrl`=?";
        PreparedStatement ps3 = con.prepareStatement(sql);
        ps3.setString(1, name);
        ps3.setLong(1, upd);
        result = ps3.executeUpdate(sql);
    }
    else{
        result=001;
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add Student</title>
    </head>
    <body>
        <p><%= result %> Record inserted!</p>
        <a href="">Back to home page</a>
    </body>
</html>
java mysql database jsp servlets
1个回答
0
投票

该错误与连接数据库无关,而是与尝试运行时发生错误

INSERT INTO `stu_att`(`No`, `name`, `enrl`) VALUES (?,?,?)

您正在尝试通过

将值传递给此命令
        PreparedStatement ps1 = con.prepareStatement(sql);
        ps1.setInt(1, count);
        ps1.setString(2, name);
        ps1.setLong(3, enrl);
        result = ps1.executeUpdate(sql);

您需要运行

ps1.execute()
而不是
ps1.executeUpdate(sql)

© www.soinside.com 2019 - 2024. All rights reserved.