gpt4 book ai didi

java - 将 nvarchar 值 '....' 转换为数据类型 int 时转换失败

转载 作者:行者123 更新时间:2023-12-02 01:10:19 26 4
gpt4 key购买 nike

我正在尝试在 JavaFx 中为我的表创建更新语句,但收到以下错误:

Conversion failed when converting the nvarchar value 'Ana' to data type int.

我在 SQL Server 中创建了表。在我的数据库中,只有年龄来自 int 类型,其他所有内容都是 String。

这是我的代码:

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

关于java - 将 nvarchar 值 '....' 转换为数据类型 int 时转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59494635/

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