【问题标题】:java.sql.SQLException: Before start of result set... how remove this errorjava.sql.SQLException:在结果集开始之前...如何消除此错误
【发布时间】:2017-02-09 09:20:12
【问题描述】:

我使用 java 和 mysql 开发了 Employee Payroll Management。 我想生成一个PDF单... 但是当我点击生成工资单按钮时..然后发生此错误... 我的生成滑动按钮的代码在这里:

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

    String value = txt_firstname.getText();
    String value0 = txt_surname.getText();
    String value1 = txt_id.getText();
    String value2 = txt_desig.getText(); 
     String value3 = txt_dep.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:


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

        }

     }
      }

} 

【问题讨论】:

  • 错误显示在哪一行?

标签: java mysql netbeans-8


【解决方案1】:

您需要在使用 getString 等之前调用ResultSet#next():

    String sql ="select * from Deductions where emp_id = '"+value1+"'";
    pst=conn.prepareStatement(sql);
    rs=pst.executeQuery(); 
    if(rs.next()) {              // here
      String val = rs.getString(5);
      String reason = rs.getString(6);
    }

next() 调用每次将光标向前移动一行。 ResultSet 游标最初位于第一行之前;第一次调用 next 方法使第一行成为当前行;第二次调用使第二行成为当前行,依此类推。

【讨论】:

  • 您是在我最近更新答案之后还是之前尝试过?你现在可以试试吗?
猜你喜欢
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 2013-05-01
  • 2013-10-01
  • 1970-01-01
  • 2011-04-12
  • 2011-01-08
相关资源
最近更新 更多