簡體   English   中英

java.sql.SQLException:在開始結果集之前...如何刪除此錯誤

[英]java.sql.SQLException: Before start of result set… how remove this error

我使用java和mysql開發了員工薪資管理..我想生成一個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);

        }

     }
      }

} 

在使用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游標最初位於第一行之前 ; 對方法的第一次調用使得第一行成為當前行; 第二個調用使第二行成為當前行,依此類推。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM