java setString()不起作用 [英] java setString() not working

查看:92
本文介绍了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");
        }

所以我想将这些变量插入到准备好的语句中,但是却出现了错误.在添加了这些JOptionPanes进行调试之后,程序将显示"5",然后显示"4",然后显示"error1".我认为这意味着线 ps.setString(2,gender);执行失败.但是,我找不到在哪里弄错了.有人可以帮忙吗?

So I want to insert these variables to the prepared statement, however I'm getting an error. After adding those JOptionPanes to debug, the program shows "5", and then "4" and then "error1". Which I think means that the line ps.setString(2,gender); failed to execute. However, I cannot find where did I get it wrong. Could anyone help?

推荐答案

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

Remove quotes around question marks. Otherwise, '?' would be interpreted as a string literal with a single question mark in it:

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

当前,JDBC认为您的查询具有单个参数,该参数对应于末尾的问号.尝试设置第二个参数会导致异常.

Currently, JDBC thinks that your query has a single parameter, corresponding to the question mark at the end. Trying to set parameter number two causes the exception.

这篇关于java setString()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆