不断收到错误:当前语句中至少有一个参数未初始化

时间:2017-10-09 14:26:01

标签: java swing netbeans derby

这是我正在执行的代码。在这段代码中,我正在尝试使用我从组合框中获取的详细信息来更新数据库..

 private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {                                       
    Connection connection;
    try {
        int flag=1;
       try {
           Class.forName("com.mysql.jdbc.Driver");
       } catch (Exception ex) {
           System.out.println("Error"+ex);
       }
        connection  = DriverManager.getConnection("jdbc:derby://localhost:1527/loginDB", "login_user" ,"root");
        String query = "INSERT into FACULTY_DETAILS (FACULTYNAME,LECTUREQUALITY,COMSKILLS,KNOWLEDGE,BEHAVIOUR,NOTESPROVIDED,PUNCTUALITY,SUGGESTION) values(?,?,?,?,?,?,?,?)";
        PreparedStatement pt = connection.prepareStatement(query);
        //Statement pt = connection.createStatement();
        pt.executeUpdate();
        ResultSet rs = pt.executeQuery("SELECT * FROM LOGIN_USER.FACULTY_DETAILS");
        while(rs.next()){
        if(rs.getString(1).equals(kapil.getText())){
        pt.setString(2, (String) javalec.getSelectedItem());
        pt.setString(3,(String)javacomm.getSelectedItem());
        pt.setString(4,(String)javaknow.getSelectedItem());
        pt.setString(5, (String) javabeh.getSelectedItem());
        pt.setString(6, (String) javanot.getSelectedItem());
        pt.setString(7, (String) javapun.getSelectedItem());
        pt.setString(8,javatext.getText());
        pt.executeUpdate(); flag=0;}
         if(rs.getString(1).equals(ziaul.getText())){
        pt.setString(2, (String) oslec.getSelectedItem());
        pt.setString(3,(String)oscomm.getSelectedItem());
        pt.setString(4,(String)osknow.getSelectedItem());
        pt.setString(5, (String) osbeha.getSelectedItem());
        pt.setString(6, (String) osnote.getSelectedItem());
        pt.setString(7, (String) ospun.getSelectedItem());
        pt.setString(8,ostext.getText());
        pt.executeUpdate();
          flag=0;}
        if(rs.getString(1).equals(saurabh.getText())){
        pt.setString(2, (String) daalec.getSelectedItem());
        pt.setString(3,(String)daacomm.getSelectedItem());
        pt.setString(4,(String)daakno.getSelectedItem());
        pt.setString(5, (String) daabeh.getSelectedItem());
        pt.setString(6, (String) daanot.getSelectedItem());
        pt.setString(7, (String) daapun.getSelectedItem());
        pt.setString(8,daatext.getText());
        pt.executeUpdate();
          flag=0;}
        if(rs.getString(1).equals(gayatri.getText())){
        pt.setString(2, (String) ecolec.getSelectedItem());
        pt.setString(3,(String)ecocomm.getSelectedItem());
        pt.setString(4,(String)ecokno.getSelectedItem());
        pt.setString(5, (String) ecobeh.getSelectedItem());
        pt.setString(6, (String) econot.getSelectedItem());
        pt.setString(7, (String) ecopun.getSelectedItem());
        pt.setString(8,ecotext.getText());
        pt.executeUpdate();
          flag=0;}
         if(rs.getString(1).equals(tasleem.getText())){
        pt.setString(2, (String) clec.getSelectedItem());
        pt.setString(3,(String)ccomm.getSelectedItem());
        pt.setString(4,(String)cknow.getSelectedItem());
        pt.setString(5, (String) cbeh.getSelectedItem());
        pt.setString(6, (String) cnote.getSelectedItem());
        pt.setString(7, (String) cpun.getSelectedItem());
        pt.setString(8,ctext.getText());
        pt.executeUpdate();
          flag=0;}
         if(rs.getString(1).equals(saurabhs.getText())){
        pt.setString(2, (String)graplec.getSelectedItem());
        pt.setString(3,(String)grapcomm.getSelectedItem());
        pt.setString(4,(String)grapknow.getSelectedItem());
        pt.setString(5, (String) grapbeh.getSelectedItem());
        pt.setString(6, (String) grapnot.getSelectedItem());
        pt.setString(7, (String) grappun.getSelectedItem());
        pt.setString(8,graphtext.getText());
        pt.executeUpdate();
          flag=0;}
         if(rs.getString(1).equals(sanjay.getText())){
        pt.setString(2, (String) softlec.getSelectedItem());
        pt.setString(3,(String)  softcomm.getSelectedItem());
        pt.setString(4,(String)  softknow.getSelectedItem());
        pt.setString(5, (String) softbeh.getSelectedItem());
        pt.setString(6, (String) softnot.getSelectedItem());
        pt.setString(7, (String) softpun.getSelectedItem());
        pt.setString(8,softtext.getText());
        pt.executeUpdate();
         flag=0; 
        }
        if(flag==0){
        JOptionPane.showMessageDialog(null,"Feedback Submitted Successfully");
        }
        }

    } catch (SQLException ex) {
        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
    }                                   
}                                      

这是我正在使用的代码请帮助我.. 我正在尝试执行我给出的代码,但它一直给我错误"当前语句中至少有一个参数未初始化。"所以请帮助......

2 个答案:

答案 0 :(得分:1)

预准备语句中的SQL具有参数标记:

    String query = "INSERT into FACULTY_DETAILS (FACULTYNAME,LECTUREQUALITY,COMSKILLS,KNOWLEDGE,BEHAVIOUR,NOTESPROVIDED,PUNCTUALITY,SUGGESTION) values(?,?,?,?,?,?,?,?)";

在执行SQL之前需要设置这些参数(如果没有值代替?,它就没有意义)。

但是,您尝试在不设置参数的情况下执行语句:

    PreparedStatement pt = connection.prepareStatement(query);
    pt.executeUpdate();

会导致您描述的错误。目前执行该声明并不清楚你打算做什么。

此外,您尝试对查询和更新使用相同的语句对象。我不相信这会奏效。为查询创建单独的语句:

PreparedStatement queryStatement = connection.prepareStatement("SELECT * FROM LOGIN_USER.FACULTY_DETAILS");
ResultSet rs = queryStatement.executeQuery();

最后,请注意,pt.executeUpdate()块中if的后续调用也会在未设置所有参数的情况下发生,因为在任何这些情况下都不会设置参数编号1。您还需要将该参数设置为适当的值。

答案 1 :(得分:1)

这里的问题是,在设置了您需要的详细信息之后,您正在准备一份将在以后使用的声明。 然后,您尝试执行executeUpdate(),期望设置值。

解决方案:在获取executeUpdate()之前移除ResultSet并设置PreparedStatement的值。

 private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {
  Connection connection;
  PreparedStatement pt;
  Statement st;
  ResultSet rs;
  try {
   int flag = 1;
   try {
    Class.forName("com.mysql.jdbc.Driver");
   } catch (Exception ex) {
    System.out.println("Error" + ex);
   }
   connection = DriverManager.getConnection("jdbc:derby://localhost:1527/loginDB", "login_user", "root");
   String query = "INSERT into FACULTY_DETAILS (FACULTYNAME,LECTUREQUALITY,COMSKILLS,KNOWLEDGE,BEHAVIOUR,NOTESPROVIDED,PUNCTUALITY,SUGGESTION) values(?,?,?,?,?,?,?,?)";
   pt = connection.prepareStatement(query);
   st = connection.createStatement();
   rs = st.executeQuery("SELECT * FROM LOGIN_USER.FACULTY_DETAILS");
   while (rs.next()) {
    if (rs.getString(1).equals(kapil.getText())) {
     pt.setString(2, (String) javalec.getSelectedItem());
     pt.setString(3, (String) javacomm.getSelectedItem());
     pt.setString(4, (String) javaknow.getSelectedItem());
     pt.setString(5, (String) javabeh.getSelectedItem());
     pt.setString(6, (String) javanot.getSelectedItem());
     pt.setString(7, (String) javapun.getSelectedItem());
     pt.setString(8, javatext.getText());
     pt.executeUpdate();
     flag = 0;
    } else if (rs.getString(1).equals(ziaul.getText())) {
     pt.setString(2, (String) oslec.getSelectedItem());
     pt.setString(3, (String) oscomm.getSelectedItem());
     pt.setString(4, (String) osknow.getSelectedItem());
     pt.setString(5, (String) osbeha.getSelectedItem());
     pt.setString(6, (String) osnote.getSelectedItem());
     pt.setString(7, (String) ospun.getSelectedItem());
     pt.setString(8, ostext.getText());
     pt.executeUpdate();
     flag = 0;
    } else if (rs.getString(1).equals(saurabh.getText())) {
     pt.setString(2, (String) daalec.getSelectedItem());
     pt.setString(3, (String) daacomm.getSelectedItem());
     pt.setString(4, (String) daakno.getSelectedItem());
     pt.setString(5, (String) daabeh.getSelectedItem());
     pt.setString(6, (String) daanot.getSelectedItem());
     pt.setString(7, (String) daapun.getSelectedItem());
     pt.setString(8, daatext.getText());
     pt.executeUpdate();
     flag = 0;
    } else if (rs.getString(1).equals(gayatri.getText())) {
     pt.setString(2, (String) ecolec.getSelectedItem());
     pt.setString(3, (String) ecocomm.getSelectedItem());
     pt.setString(4, (String) ecokno.getSelectedItem());
     pt.setString(5, (String) ecobeh.getSelectedItem());
     pt.setString(6, (String) econot.getSelectedItem());
     pt.setString(7, (String) ecopun.getSelectedItem());
     pt.setString(8, ecotext.getText());
     pt.executeUpdate();
     flag = 0;
    } else if (rs.getString(1).equals(tasleem.getText())) {
     pt.setString(2, (String) clec.getSelectedItem());
     pt.setString(3, (String) ccomm.getSelectedItem());
     pt.setString(4, (String) cknow.getSelectedItem());
     pt.setString(5, (String) cbeh.getSelectedItem());
     pt.setString(6, (String) cnote.getSelectedItem());
     pt.setString(7, (String) cpun.getSelectedItem());
     pt.setString(8, ctext.getText());
     pt.executeUpdate();
     flag = 0;
    } else if (rs.getString(1).equals(saurabhs.getText())) {
     pt.setString(2, (String) graplec.getSelectedItem());
     pt.setString(3, (String) grapcomm.getSelectedItem());
     pt.setString(4, (String) grapknow.getSelectedItem());
     pt.setString(5, (String) grapbeh.getSelectedItem());
     pt.setString(6, (String) grapnot.getSelectedItem());
     pt.setString(7, (String) grappun.getSelectedItem());
     pt.setString(8, graphtext.getText());
     pt.executeUpdate();
     flag = 0;
    } else if (rs.getString(1).equals(sanjay.getText())) {
     pt.setString(2, (String) softlec.getSelectedItem());
     pt.setString(3, (String) softcomm.getSelectedItem());
     pt.setString(4, (String) softknow.getSelectedItem());
     pt.setString(5, (String) softbeh.getSelectedItem());
     pt.setString(6, (String) softnot.getSelectedItem());
     pt.setString(7, (String) softpun.getSelectedItem());
     pt.setString(8, softtext.getText());
     pt.executeUpdate();
     flag = 0;
    }
    if (flag == 0) {
     JOptionPane.showMessageDialog(null, "Feedback Submitted Successfully");
    }
   }

  } catch (SQLException ex) {
   Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  } finally {
   if (rs != null) {
    rs.close();
   }
   if (st != null) {
    st.close();
   }
   if (pt != null) {
    pt.close();
   }
   if (connection != null) {
    connection.close();
   }
  }
 }

注意:我在else if的位置使用if以避免重复的条件检查。并且,最后阻止以避免内存泄漏