0

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

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

<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

<%@ 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");
    }
%>
3
  • 2
    You should be using prepared statements to prevent SQL injection. Commented Dec 6, 2016 at 20:08
  • 1
    BTW how is HTML code related to the SQL server error? Commented Dec 7, 2016 at 3:16
  • Please learn how to use prepared statements with parameters. Do not concatenate values into you query string. It leaves you vulnerable to SQL injection! Commented Dec 7, 2016 at 9:20

2 Answers 2

1

This is the query that you're running:

insert into class(name, strength ,room, section) values ('" + cname + "','" + cstrength + "','" + croom + "','" + csection + "', 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())

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().

Sign up to request clarification or add additional context in comments.

7 Comments

i add another column name "regdate" and type "date" but still that error is appearing
did you update the code too to say class(name, strength ,room, section,regdate)?
yes Sourabh Mahajan i updated my code
ok. please try taking this whole thing "insert into class(name, strength ,room, section, regdate) values ('" + cname + "','" + cstrength + "','" + croom + "','" + csection + "', CURDATE());" as a variable, print it, and run it directly on mysql studio. i'm pretty sure that the error, if at all it still comes, should not be about mismatching count.
i have another file of same code but different database name and values that file works fine i don't know why this file is not working
|
1

Your input statement has four columns listed — name, strength, room, section — and then provides five values: cname, cstrength, croom, csection, and CURDATE().

You just need to add the other column in the insert statement.

3 Comments

i add another column name "regdate" and type "date" but still that error is appearing
once you add the column, you need to adjust your query: insert into class(name, strength ,room, section, regdate) values ('" + cname + "','" + cstrength + "','" + croom + "','" + csection + "', CURDATE());
i tried this but still same error is appearing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.