我正在尝试在表中插入数据,但是显示以下错误:
java.sql.SQLException:列数与第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"); } %>
这是您正在运行的查询:
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)
在表中添加该新列,然后更新查询以使其也包含该列(即(class(name, strength ,room, section, curdate))),或删除CURDATE()。
class(name, strength ,room, section, curdate)