Conversion failed when converting the nvarchar value 'XX' to data type int. im having these error and i dont know what to do with it. please help me. THANKS. :D
在将nvarchar值“XX”转换为数据类型时,转换失败了。请帮助我。谢谢。:D
btnNewButton_3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String value1=textField_7.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionURL = "jdbc:sqlserver://;Database='';user='';password='';";
Connection con = DriverManager.getConnection(connectionURL);
PreparedStatement pst =null;
//Statement stmt=con.createStatement();
//PreparedStatement pst=con.prepareStatement("select * from inventory where Id=? or Name=? ");
String sql ="select * from inventory where Id=? or Name=? ";
pst=con.prepareStatement(sql);
pst.setString(1, textField_7.getText());
pst.setString(2, textField_7.getText());
ResultSet rs = pst.executeQuery();
if(value1.equals("")){
reloadData();
model.fireTableDataChanged();
JOptionPane.showMessageDialog(null,"Please Enter a Name or Id of the Item","ERROR",JOptionPane.ERROR_MESSAGE);
}else{
table.setModel(DbUtils.resultSetToTableModel(rs));
}
}
catch(Exception e1){e1.printStackTrace();}
}
});
2 个解决方案
#1
4
Try changing the SQL Statement from
尝试修改SQL语句
select * from inventory where Id=? or Name=?
to
来
select * from inventory where CAST(Id AS NVARCHAR(50))=? or Name=?
The problem is that you are using the same text field to compare to both Id
and Name
, so if you were to enter Rohan into the text field it would try to convert it to an INT
to compare to Id
, in which case the statement would fail.
问题是您正在使用相同的文本字段来与Id和名称进行比较,所以如果您将Rohan输入到文本字段,它将尝试将其转换为INT以与Id进行比较,在这种情况下,语句将失败。
#2
0
Assuming that your Id Column is integral, you're getting this error because you are trying to assign it a string. If the field is integral, parse the textField to an int and then use setInt
假设你的Id列是积分的,你会得到这个错误因为你想给它分配一个字符串。如果字段是整数,则将textField解析为int,然后使用setInt
pst.setInt(1, parsedInt);
#1
4
Try changing the SQL Statement from
尝试修改SQL语句
select * from inventory where Id=? or Name=?
to
来
select * from inventory where CAST(Id AS NVARCHAR(50))=? or Name=?
The problem is that you are using the same text field to compare to both Id
and Name
, so if you were to enter Rohan into the text field it would try to convert it to an INT
to compare to Id
, in which case the statement would fail.
问题是您正在使用相同的文本字段来与Id和名称进行比较,所以如果您将Rohan输入到文本字段,它将尝试将其转换为INT以与Id进行比较,在这种情况下,语句将失败。
#2
0
Assuming that your Id Column is integral, you're getting this error because you are trying to assign it a string. If the field is integral, parse the textField to an int and then use setInt
假设你的Id列是积分的,你会得到这个错误因为你想给它分配一个字符串。如果字段是整数,则将textField解析为int,然后使用setInt
pst.setInt(1, parsedInt);