gpt4 book ai didi

java.sql.SQLException : Field 'Cust_LastName' doesn't have a default value

转载 作者:行者123 更新时间:2023-11-29 22:32:38 25 4
gpt4 key购买 nike

该错误表明,如果我的知识正确,我不会为 Cust_LastName 提供值,但我正在尝试使用下面的代码。

整个集合用于我的数据库中的单个新行

// Accessing driver from JAR file
try {
Class.forName("com.mysql.jdbc.Driver");

// Creating a variable for the connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/schbank", "root", "s201431s");

// SQL statements to insert the information into the table
PreparedStatement statementFName = con.prepareStatement("insert into tblcustomer(Cust_FirstName)" + "values(?)");
PreparedStatement statementLName = con.prepareStatement("insert into tblcustomer(Cust_LastName)" + "values(?)");
PreparedStatement statementAddress = con.prepareStatement("insert into tblcustomer(Cust_Address)" + "values(?)");
PreparedStatement statementPhone = con.prepareStatement("insert into tblcustomer(Cust_PhoneNumber)" + "values(?)");
PreparedStatement statementSSN = con.prepareStatement("insert into tblcustomer(Cust_SSN)" + "values(?)");
PreparedStatement statementEmail = con.prepareStatement("insert into tblcustomer(Cust_Email)" + "values(?)");
PreparedStatement statementUsername = con.prepareStatement("insert into tblcustomer(Cust_Username)" + "values(?)");
PreparedStatement statementPassword = con.prepareStatement("insert into tblcustomer(Cust_Password)" + "values(?)");

// Sets the values
statementFName.setString(1, strFname);
statementLName.setString(1, strLname);
statementAddress.setString(1, strAddress);
statementPhone.setString(1, strPhone);
statementSSN.setString(1, strSSN);
statementEmail.setString(1, strEmail);
statementUsername.setString(1, strUserName);
statementPassword.setString(1, strPassword);

// Other code removed for brevity
} catch (SQLException ex) {
ex.printStackTrace(System.err);
}

最佳答案

试试这个:

PreparedStatement stmt = con.prepareStatement(
"INSERT INTO tblcustomer (Cust_FirstName, Cust_LastName, Cust_Address, Cust_PhoneNumber, Cust_SSN, Cust_Email, Cust_Username, Cust_Password)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)");

stmt.setString(1, strFname);
stmt.setString(2, strLname);
stmt.setString(3, strAddress);
stmt.setString(4, strPhone);
stmt.setString(5, strSSN);
stmt.setString(6, strEmail);
stmt.setString(7, strUserName);
stmt.setString(8, strPassword);

按照您现在的方式,您尝试在表中执行 8 次单独的插入,您需要一次插入 8 个值。

关于java.sql.SQLException : Field 'Cust_LastName' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29681800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com