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");
    }
%>
mkobit
  • 43,979
  • 12
  • 156
  • 150
Kamran Ali
  • 29
  • 1
  • 9

2 Answers2

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.

Jerry
  • 3,391
  • 1
  • 19
  • 28
  • i add another column name "regdate" and type "date" but still that error is appearing – Kamran Ali Dec 06 '16 at 20:00
  • 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());` – Jerry Dec 06 '16 at 20:01
  • i tried this but still same error is appearing – Kamran Ali Dec 06 '16 at 20:03
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().

Sourabh
  • 429
  • 1
  • 4
  • 11
  • i add another column name "regdate" and type "date" but still that error is appearing – Kamran Ali Dec 06 '16 at 20:01
  • did you update the code too to say `class(name, strength ,room, section,regdate)`? – Sourabh Dec 06 '16 at 20:02
  • yes Sourabh Mahajan i updated my code – Kamran Ali Dec 06 '16 at 20:04
  • 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. – Sourabh Dec 06 '16 at 20:08
  • 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 – Kamran Ali Dec 06 '16 at 20:10
  • please print the query that's finally constructed and update here. that should confirm the state. – Sourabh Dec 06 '16 at 20:11
  • your last code is working fine thank you so much brother i am working on this since morning – Kamran Ali Dec 06 '16 at 20:13