显示错误" java.sql.SQLException:在结果集开始之前"

时间:2017-08-08 17:41:40

标签: java

在我的代码中..我希望它保存包含员工详细信息的pdf文件 但它没有保存pdf文件而只显示

  

" java.sql.SQLException:在开始结果集之前"

需要帮助......我不明白......这段代码有什么问题。 我认为撤销结果集和准备声明存在一些问题。

    private void txt_searchKeyReleased(java.awt.event.KeyEvent evt) {                                       
        // TODO add your handling code here:
        try{

            String sql ="select * from staff_info where id=? ";

            pst=conn.prepareStatement(sql);
            pst.setString(1,txt_search.getText());
            rs=pst.executeQuery();
            while(rs.next())
            {

            int add1 =rs.getInt("id");
            txt_id.setText(add1+"");

            String add2 =rs.getString("first_name");
            txt_firstname.setText(add2);

            String add3 =rs.getString("surname");
            txt_surname.setText(add3);

            String add4 =rs.getString("Dob");
            txt_dob.setText(add4);

            String add5 =rs.getString("Department");
            txt_dep.setText(add5);

            String add7 =rs.getString("Salary");
            txt_salary.setText(add7);

            String add8 =rs.getString("Status");
            txt_status.setText(add8);

            String add9 =rs.getString("Date_hired");
            txt_doj.setText(add9);

            String add10 =rs.getString("job_title");
            txt_job.setText(add10);

            String add17 =rs.getString("Designation");
            txt_desig.setText(add17);

            }
        }catch(Exception e){
          JOptionPane.showMessageDialog(null, "No Data");
        }
        finally {

            try{

               /* rs.close();
                pst.close();*/

            }
            catch(Exception e){

            }
        }
    }                                      

    private void txt_firstnameActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void txt_jobActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void txt_salaryActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        // TODO add your handling code here:

                 String value = txt_firstname.getText();
            String value0 = txt_surname.getText();
            String value1 = txt_id.getText();
            String value2 = txt_desig.getText();
            String value3 = txt_desig.getText();


            JFileChooser dialog = new JFileChooser();
            dialog.setSelectedFile(new File(value +" "+ value0+"-Salary Slip"+".pdf"));
            int dialogResult = dialog.showSaveDialog(null);
            if (dialogResult==JFileChooser.APPROVE_OPTION){
            String filePath = dialog.getSelectedFile().getPath();

        try {
            // TODO add your handling code here:

          for (int i=0; i<1000; i++) {
            String sql ="select * from deductions where emp_id = '"+value1+"'";
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery(); 
            String val = rs.getString(5);
            String reason = rs.getString(6);

           rs.close();
            pst.close();

            String sq ="select * from allowance where emp_id = '"+value1+"'";
            pst=conn.prepareStatement(sq);
            rs=pst.executeQuery();


           int calcTotal = Integer.parseInt(txt_salary.getText());
           float x = Float.valueOf(rs.getString(9));
           int v = Integer.parseInt(val);
           float total = calcTotal+ x -v;

           Document myDocument = new Document();
           PdfWriter myWriter = PdfWriter.getInstance(myDocument, new FileOutputStream(filePath));
           myDocument.open();

           myDocument.add(new Paragraph("PAY SLIP",FontFactory.getFont(FontFactory.TIMES_BOLD,20,Font.BOLD )));
           myDocument.add(new Paragraph(new Date().toString()));
           myDocument.add(new Paragraph("-------------------------------------------------------------------------------------------"));
           myDocument.add((new Paragraph("EMPLOYEE DETAILS",FontFactory.getFont(FontFactory.TIMES_ROMAN,15,Font.BOLD))));
           myDocument.add((new Paragraph("Name of Employee: " +value + " "+value0,FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN))));
           myDocument.add((new Paragraph("Designation: "+value2,FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN))));
           myDocument.add((new Paragraph("Department: "+value3,FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN))));
           myDocument.add(new Paragraph("-------------------------------------------------------------------------------------------"));
           myDocument.add(new Paragraph("SALARY",FontFactory.getFont(FontFactory.TIMES_ROMAN,15,Font.BOLD)));
           myDocument.add(new Paragraph("Basic Salary: $"+calcTotal,FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("Overtime: "+rs.getString(2)+" Hours",FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("Medical: $" +rs.getString(3),FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("Bonus: $"+rs.getString(4),FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("Other: $"+rs.getString(5),FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("-------------------------------------------------------------------------------------------"));
           myDocument.add(new Paragraph("DEDUCTION",FontFactory.getFont(FontFactory.TIMES_ROMAN,15,Font.BOLD)));
           myDocument.add(new Paragraph("Deduction Details: "+reason,FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("Total Deductions : $"+val ,FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("-------------------------------------------------------------------------------------------"));
           myDocument.add(new Paragraph("TOTAL PAYMENT",FontFactory.getFont(FontFactory.TIMES_ROMAN,15,Font.BOLD)));
           myDocument.add(new Paragraph("Total Earnings: "+rs.getString(9),FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("Net Pay : " +total,FontFactory.getFont(FontFactory.TIMES_ROMAN,10,Font.PLAIN)));
           myDocument.add(new Paragraph("-------------------------------------------------------------------------------------------"));


           myDocument.newPage();
           myDocument.close();  
           JOptionPane.showMessageDialog(null,"Report was successfully generated");
          }

     }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);


     }
     finally {

            try{
               rs.close();
               pst.close();

            }
            catch(Exception e){
            JOptionPane.showMessageDialog(null,e);

            }
     }
   }
 }        

2 个答案:

答案 0 :(得分:0)

您在代码的前一部分处理结果集时有正确的想法,但未能在其他地方使用它。在jButton1ActionPerformed的部分中,您执行查询,但不要rs.next()实际移动到结果集中的第一个项目。在尝试操作结果集之前一定要这样做,否则你将继续获得该异常。

答案 1 :(得分:0)

不确定这是否是问题,但是您不一致地处理'id'列;这可能会导致JDBC驱动程序出现奇怪现象。在preparedStatement中,您将其视为带有

的字符串
pst.setString(1, ...)

但是在结果集中,您将其视为带有

的int
rs.getInt("id")