我正在尝试为JavaFx中的表创建更新语句,并且收到以下错误消息:
将nvarchar值“ Ana”转换为数据类型int时,转换失败。
我在SQL Server中创建了表。在我的数据库中,年龄仅来自int类型,其他所有内容均为字符串。
这是我的代码:
public void handleUpdateAction(ActionEvent event) {
String sql = "update users set name =?, age = ?, department=?, job=?, contact = ? where userNo =?" ;
try {
String userNo = txt_userNo.getText();
String name = txt_name.getText();
double age = Double.valueOf(txt_age.getText());
String department = txt_department.getText();
String job = txt_job.getText();
String contact = txt_contact.getText();
p_stmt = con.prepareStatement(sql);
p_stmt.setString(1, userNo);
p_stmt.setString(2, name);
p_stmt.setDouble(3, age);
p_stmt.setString(4, department);
p_stmt.setString(5, job);
p_stmt.setString(6, contact);
int i = p_stmt.executeUpdate();
if (i == 1) {
}System.out.println("Data Updated Successfully");
loadDataFromDatabase();
}catch (Exception e) {
e.printStackTrace();
}
}
将parameterIndex用于Statement.setString指示参数标记的词汇顺序?在查询中。所以userNo应该在最后。例如:
public void handleUpdateAction(ActionEvent event) {
String sql = "update users set name =?, age = ?, department=?, job=?, contact = ? where userNo =?" ;
try {
String userNo = txt_userNo.getText();
String name = txt_name.getText();
double age = Double.valueOf(txt_age.getText());
String department = txt_department.getText();
String job = txt_job.getText();
String contact = txt_contact.getText();
p_stmt = con.prepareStatement(sql);
p_stmt.setString(1, name);
p_stmt.setDouble(2, age);
p_stmt.setString(3, department);
p_stmt.setString(4, job);
p_stmt.setString(5, contact);
p_stmt.setString(6, userNo);
int i = p_stmt.executeUpdate();
if (i == 1) {
}System.out.println("Data Updated Successfully");
loadDataFromDatabase();
}catch (Exception e) {
e.printStackTrace();
throw e;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。