简体   繁体   中英

Data truncation: Truncated incorrect DOUBLE value:

I have been trying to solve this for hours and I have no idea why this is happening. I keep on getting this error when trying to update one of my "Employees" with my GUI that I made.

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'test'

Whenever I click an employee in my TableView, and then change the field of something, I would get that. Here is a picture of my GUI:

员工记录

Nothing in my data table is a DOUBLE, they all are like this:

mysql数据表

I have googled everywhere and it all says it relates to syntax for my SQL query but that looks fine so I do not know why.

Here is the code for my Update button:

public void handleUpdateEmployee(ActionEvent event) {
    String sql = "UPDATE employees set firstName = ?, lastName = ?, gender = ?, age = ?, position = ?, image = ? where employeeNum =?";
    try {
        Integer employeeNum = Integer.valueOf(employeeNumField.getText());
        String firstName = firstNameField.getText();
        String lastName = lastNameField.getText();
        String gender = genderField.getText();
        Integer age = Integer.valueOf(ageField.getText());
        String position = positionField.getText();
        String image = imageField.getText();

        pst = con.prepareStatement(sql);
        pst.setInt(1, employeeNum);
        pst.setString(2, firstName);
        pst.setString(3, lastName);
        pst.setString(4, gender);
        pst.setInt(5, age);
        pst.setString(6, position);
        pst.setString(7, image);

        pst.executeUpdate();
        loadEmployeeData();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

Would appreciate any help and if you need any more details, let me know!

You bound your query parameters in the wrong order. Look closely at the statement and then the updated code which follows:

String sql = "UPDATE employees set firstName = ?, lastName = ?, gender = ?, ";
sql += "age = ?, position = ?, image = ? where employeeNum =?";

pst.setString(1, firstName);  // first parameter (?) in statement
pst.setString(2, lastName);
pst.setString(3, gender);
pst.setInt(4, age);
pst.setString(5, position);
pst.setString(6, image);
pst.setInt(7, employeeNum);   // LAST parameter (?) in statement

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