2017-08-08 57 views
-1

在我的代碼..我希望它保存PDF文件,其中包含員工 的細節,但它不保存的PDF文件,只是顯示顯示錯誤「java.sql.SQLException中:結果集的開始之前」

「值java.sql.SQLException:在結果的開始時設置」

需要幫助....我不是understanding..what與此代碼的問題。 我認爲撤銷結果集和preparedstatement有一些問題。

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

      } 
    } 
    } 
}   
+0

是您的結果集爲空? – CBredlow

+0

@schtever你是對的,還有另一個需要提升的部分。 – CBredlow

回答

0

你已經得到了處理結果在代碼的前面部分設置正確的想法,但不能在其他地方使用它。在jButton1ActionPerformed部分,您執行查詢,但不要執行rs.next()實際移至結果集中的第一項。在你嘗試和操縱結果集之前一定要這樣做,否則你會不斷得到這個異常。

+0

我已糾正我的兩個錯誤,但仍顯示「結果集開始之前」異常... –

0

不確定這是否是問題,但是您不一致地處理'id'列;這可能會導致JDBC驅動程序中的怪異現象。在PreparedStatement的,你把它當作與

pst.setString(1, ...) 

一個字符串,但在你把它當作一個int結果集與

rs.getInt("id") 
相關問題