2016-12-06 19 views
0

で値の数と一致していない私は、テーブル内のデータを挿入しようとしているが、それは次のようなエラーが表示されます。ます。java.sql.SQLException:列数が行1エラー

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

私が検索しましたこのエラーと私はすべてのソリューションを試しましたが、まだそれを動作させることはできません。

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"); 
    } 
%> 
+2

[SQLインジェクション](https://en.wikipedia.org/wiki/SQL_injection)を防止するには、プリペアドステートメントを使用する必要があります。 – GriffeyDog

+1

BTW SQL Serverエラーに関連するHTMLコードはどのようになっていますか? –

+0

パラメータ付きプリペアドステートメントの使い方を学んでください。 **クエリ文字列に値を連結しないでください**。 SQLインジェクションに脆弱です! –

答えて

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()を削除します。

+0

( "" + cname + "'、" + cstrength + "'、" + croom + "'、+ csection +"'、CURDATE());別の列名 "regdate"を追加して "date"と入力しても、エラーが表示されます –

+0

'class(name、strength、room、section、regdate)'というコードを更新しましたか? –

+0

はいSourabh Mahajan私のコード –

1

ご入力文が4つの列がリストされている - nameは、strengthは、roomsection - 、その後は5つの値を提供しています。ここに私のコードです: cname,cstrength,croom,csectionおよびCURDATE()

他の列をinsert文に追加するだけで済みます。

+0

別の列名 "regdate"を追加して "date"と入力しても、エラーが表示された後でも –

+0

列を追加したら、クエリを調整する必要があります: 'insert into class(name、strength、room、section、regdate)values – Jerry

+0

私はこれを試しましたが、同じエラーが表示されます –

関連する問題