java setString() 不起作用


try{is = new FileInputStream(new File(s));
PreparedStatement ps; 
ps= cn.prepareStatement("Update instructor set name ='?' ,gender ='?', image ='?' where instructorID =?");
JOptionPane.showMessageDialog(null, "5");
ps.setString(1,name);
JOptionPane.showMessageDialog(null, "4");
ps.setString(2,gender);
JOptionPane.showMessageDialog(null, "3");
ps.setBlob(3, is);
JOptionPane.showMessageDialog(null, "2");
ps.setString(4, iden);
JOptionPane.showMessageDialog(null, "1");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "successfully updated");
}catch(Exception e ){
JOptionPane.showMessage(null,"error1");
}

所以我想将这些变量插入到预准备语句中,但是我收到错误。将这些 JOptionPane 添加到调试后,程序显示"5",然后是"4",然后是"error1"。我认为这意味着这条线 ps.setString(2,gender(;执行失败。但是,我找不到我哪里弄错了。谁能帮忙?

删除问号两边的引号。否则,'?'将被解释为带有单个问号的字符串文本:

ps= cn.prepareStatement("Update instructor set name =? ,gender =?, image =? where instructorID =?");

目前,JDBC 认为您的查询只有一个参数,对应于末尾的问号。尝试设置第二个参数会导致异常。

最新更新