简体   繁体   中英

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

The error says I'm not giving Cust_LastName a value if my knowledge is correct, but I am attempting to with the code below.

The entire set is for a single new row in my Database

// 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);
}

Try this instead:

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);

The way you are doing it now, you are trying to do 8 separate inserts into the table, you want a single insert with 8 values.

Each of your statements is a separate statement. Each statement, when executed, will insert a separate row. That row will have only the one column you filled through the statement, and all the other columns will be filled by their default values.

In the first statement, you are only giving a value to Cust_Firstname . So you are not giving a value to Cust_LastName , and your database is telling you that it cannot execute that statement for that reason.

You probably want one row with all the column values, not eight rows, each with a single column set. For that, you have to prepare a single statement and fill all the values in it.

When you write individual insert statement, it means it try to enter individual rows for each statement in database. This error due to when first insert statement execute, It expect Cust_LastName value as this field does not have default value.

Either you can use single insert statement with all columns, you can go with parameter insert queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM