有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mysql java。sql。SQLException:列计数与第1行的值计数不匹配错误

我试图在表中插入数据,但显示以下错误:

java.sql.SQLException: Column count doesn't match value count at row 1

我已经搜索了这个错误,并尝试了所有的解决方案,但仍然无法让它工作。这是我的密码:

班级。html

<html>
    <head>
        <title>Class</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
         <form method="post" action="class.jsp">
            <center>
                <table border="1" width="30%" cellpadding="5">
                    <thead>
                    <tr>
                        <th colspan="2">Enter Information Here</th>
                    </tr>
                </thead>
                <tbody>
            <tr>
                        <td>Class Name</td>
                        <td><input type="text" name="name" value="" /></td>
                    </tr>
                    <tr>
                        <td>Class Strength</td>
                        <td><input type="text" name="strength" value="" /></td>
                    </tr>
                    <tr>
                        <td>Room</td>
                        <td>
                           <input type="text" name="room" value=""> 

                        </td>
                    </tr>
                    <tr>
                        <td>Section</td>
                        <td><input type="text" name="section" value="" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Submit" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>

            </tbody>
                </table>
            </center>
        </form>
    </body>
</html>

班级。jsp

<%@ page import ="java.sql.*"  import= "java.sql.Connection"
%>
<%
    String cname = request.getParameter("name");
    String cstrength = request.getParameter("strength");
    String croom = request.getParameter("room");
    String csection = request.getParameter("section");

    //String available = request.getParameter("bavailable");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web",
            "root", "");
    Statement st = con.createStatement();
    //ResultSet rs;
    int i = st.executeUpdate("insert into class(name, strength ,room, section) values ('" + cname + "','" + cstrength + "','" + croom + "','" + csection + "', CURDATE());");
    if (i > 0) {
        //session.setAttribute("userid", user);
        response.sendRedirect("wel.jsp");
       // out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
    } else {
        response.sendRedirect("index.jsp");
    }
%>

共 (2) 个答案

  1. # 1 楼答案

    这是您正在运行的查询:

    insert into class(name, strength ,room, section) values ('" + cname + "','" + cstrength + "','" + croom + "','" + csection + "', CURDATE());")
    

    您提到了要传递的4个列值(class(name, strength ,room, section)),但接下来要传递5个值(CURDATE()的一个额外值)

    在表中添加新列并更新查询以包括该列(即(class(name, strength ,room, section, curdate)))或删除CURDATE()

  2. # 2 楼答案

    您的输入语句列出了四列——namestrengthroomsection,然后提供五个值:cnamecstrengthcroomcsectionCURDATE()

    只需在insert语句中添加另一列