我尝试使用 JDBC 将数据保存在 javadb 中。 它给出了一个错误。
Error Occured : The number of values assigned is not the same as the number of specified or implied columns.
这是我的 JDBC 代码:
try {
//loading driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//creating connection with the database
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
PreparedStatement ps = con.prepareStatement("insert into Student values(?,?,?,?,?)");
ps.setString(1, Title);
ps.setString(2, Artist);
ps.setString(3, Country);
ps.setString(4, Price);
ps.setString(5, Year);
int i = ps.executeUpdate();
if (i > 0) {
out.println("You are sucessfully register");
}
} catch (Exception se) {
out.println("Error Occured : \n" + se.getLocalizedMessage());
se.printStackTrace();
}
最佳答案
您没有指定尝试插入的列。然后 SQL 假设您可能想要添加为表 Student 定义的每一列。 我假设 Student 表中的列数比插入语句中的值多。这就是错误发生的地方。
定义您显式插入值的列。
insert into Student (Col1,Col2,Col3,Col4,Col5) values (?,?,?,?,?)
如果所有其他列都是可选的或自动生成的,它将以这种方式工作。否则您将收到另一个错误,告诉您缺少哪一列。
关于java - 分配的值的数量与指定或隐含的列的数量不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36349800/