java.lang.Exception:参数索引超出范围(2>参数个数,为1)

时间:2012-10-31 15:44:53

标签: java mysql prepared-statement

我很想知道为什么会出现这个错误:java.lang.Exception:参数索引超出范围(2>参数个数,即1)。

是的,我使用PreparedStatement

 SQL="SELECT pcsv("+servidor_virtual_nuestro+","+nivel_cuenta_receptora+","+codigo_sku[id_componente]+","+t_precio[tipo_precio]+",0) as p_unitario FROM servidores_virtuales t1"+
 "WHERE t1.id_servidor_virtual= ? ";


PreparedStatement pstm100 = this.conexion.prepareStatement(SQL);

pstm100.setInt(1, servidor_virtual_nuestro);

ResultSet rs66 =  pstm100.executeQuery();

pcsv是我创建的一个程序,所以我不知道为什么我有这个错误,你能帮助我吗?非常感谢

2 个答案:

答案 0 :(得分:3)

我没有看到什么会导致抛出异常,但我想这可能是你创建不正确的SQL这一事实的副作用。您的SQL字符串需要t1WHERE之间的空格。

或者它可能是某些变量中的一些额外问号。尝试在执行之前将SQL字符串打印到日志或stdout

答案 1 :(得分:-2)

public void ReturnUpdate() {
        String sql = "insert into returnbook (`Student Id`,`Student Name`,`Father Name`,Course,Branch,Year,Semester,`Book Id`,`Book Name`,Edition,Publisher,Price,Pages,`Date of Issue`,`Date of Return`) values ";
        try{
         pst = conn.prepareStatement(sql);
         pst.setString (1,  jTextField1.getText());
         pst.setString (2,  jTextField2.getText());
         pst.setString (3,  jTextField3.getText());
         pst.setString (4,  jTextField4.getText());
         pst.setString (5,  jTextField5.getText());
         pst.setString (6,  jTextField6.getText());
         pst.setString (7,  jTextField7.getText());
         pst.setString (8,  jTextField8.getText());
         pst.setString (9,  jTextField9.getText());
         pst.setString (10, jTextField10.getText());
         pst.setString (11, jTextField11.getText());
         pst.setString (12, jTextField12.getText());
         pst.setString (13, jTextField13.getText());
         pst.setString(15, ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText());
         pst.execute();
           JOptionPane.showMessageDialog(null,"Book Returned");
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }