2017-10-08 2 views
-1

These are the exceptions. Nous avons créé un jtable, les données sont ajoutées à la table en ajoutant un bouton.Et elles sont envoyées à la base de données via un bouton submit.Mais des exceptions sont générées quand le bouton submit est cliqué.Peut-on me dire où le code a mal tourné ??Comment insérer une donnée Jtable dans la base de données Oracle 11g

btnSubmit.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e) { 

    try { 

     int count = table.getRowCount(); 
         String driver = "oracle.jdbc.driver.OracleDriver"; 
     Class.forName(driver); 
     Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott", "tiger"); 
     conn.setAutoCommit(false); 
     PreparedStatement pst = conn.prepareStatement("Insert into students(ROLL_NO,NAME,MID1,MID2,Marksinwords) values (?,?,?,?,?)"); 

     for(int row = 0; row<count; row++) 
     { 
      String roll = (String)table.getValueAt(row, 0); 
      String name = (String)table.getValueAt(row, 1); 


          String MARKS1 = (String)tableModel.getValueAt(row, 2); 
      String MARKS2 = (String)table.getValueAt(row, 3); 

      String marksinwords = (String)table.getValueAt(row, 4); 

      pst.setString(1, roll); 
       pst.executeUpdate(roll); 
       pst.setString(2, name); 
       pst.executeUpdate(name); 
      pst.setString(3,MARKS1); 
       pst.executeUpdate(MARKS1); 
      pst.setString(4,MARKS2); 
       pst.executeUpdate(MARKS2); 

       pst.setString(5, marksinwords); 

       pst.executeUpdate(marksinwords); 
       pst.addBatch(); 
     } 
     pst.executeBatch(); 
     conn.commit(); 

      } catch (Exception ex) { 

       Logger.getLogger(FacultyTableSubmit.class.getName()).log(Level.SEVERE, null, ex); 
      } 

    } 

}); 
+2

Quelle exception? – Marged

+0

J'ai ajouté la capture d'écran –

+0

S'il vous plaît ne jamais afficher une exception comme une capture d'écran, toujours présenter le texte. Si vous êtes chanceux, SO vous proposera automatiquement un post où vous pourrez trouver la solution si elle est capable de reconnaître une exception qui peut déjà être trouvée dans un article précédent. En outre que votre exception n'est pas montrée complètement, il a été coupé – Marged

Répondre

1

Votre séquence avec le PreparedStatement semble un peu étrange pour moi et vous n'avez pas besoin de fournir une valeur lors de la mise à jour et vous faire deux choses, vous l'ajoutez à la DB, puis de nouveau à un lot.

Essayez comme ceci:

for(int row = 0; row<count; row++) 
    { 
     String roll = (String)table.getValueAt(row, 0); 
     String name = (String)table.getValueAt(row, 1); 
     String MARKS1 = (String)tableModel.getValueAt(row, 2); 
     String MARKS2 = (String)table.getValueAt(row, 3); 
     String marksinwords = (String)table.getValueAt(row, 4); 

     pst.setString(1, roll);; 
     pst.setString(2, name); 
     pst.setString(3,MARKS1); 
     pst.setString(4,MARKS2); 
     pst.setString(5, marksinwords); 
     pst.addBatch(); 
    } 
    pst.executeBatch(); 
    conn.commit(); 
+0

J'ai changé le code.Continuez les mêmes exceptions @Fredy Fisher –