java setString()无效

时间:2017-08-19 10:04:33

标签: java jdbc

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

所以我想将这些变量插入到预准备语句中,但是我收到了一个错误。将这些JOptionPanes添加到调试后,程序显示“5”,然后显示“4”,然后显示“error1”。我认为这意味着行 ps.setString(2,性别);未能执行。但是,我找不到哪里弄错了。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:4)

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

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

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