java.sql.SQLException:列数与第1行错误处的值数不匹配 [英] java.sql.SQLException: Column count doesn't match value count at row 1 error

查看:71
本文介绍了java.sql.SQLException:列数与第1行错误处的值数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to insert data in a table, but it shows the following error:

java.sql.SQLException:列计数与第1行的值计数不匹配

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

我已经搜索了此错误,并尝试了所有解决方案,但仍然无法正常工作.这是我的代码:

I've searched this error and I tried all solutions but still it can't get it to work. Here's my code :

class.html

class.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>

class.jsp

class.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");
    }
%>

推荐答案

这是您正在运行的查询:

This is the query that you're running:

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

您提到了要传递的4个列值(class(name, strength ,room, section)),但是随后传递了5个值(CURDATE()的附加值)

you've mentioned 4 column values to be passed (class(name, strength ,room, section)), but then you're passing in 5 values (an extra value for CURDATE())

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

Either add that new column in the table and update the query to include that column as well (i.e. (class(name, strength ,room, section, curdate))) OR remove CURDATE().

这篇关于java.sql.SQLException:列数与第1行错误处的值数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆