2017-05-31 27 views
1

私はプログラムを作成していますが、今回は何をしようとしていますが、JTableと他のものの内容をコピーしてPostgreSQLのテーブルに挿入することです。たとえば、最後の行が 'Cat'と表示され、5行がある場合、PostgreSQLのテーブルに5行の 'Cat'という単語が挿入され、その行のデータは無視されます。最後の行の前に他のもの。最後の行を複製するバッチ

try{ 
       Connection conn = null; 
       PreparedStatement pst = null; 
       Calendar calendario = GregorianCalendar.getInstance(); // creates a new calendar instance 
       String hora = Integer.toString(calendario.get(Calendar.HOUR_OF_DAY)); 
       int minutos = calendario.get(Calendar.MINUTE); 

       int rows=table.getModel().getRowCount(); 
       Class.forName("org.postgresql.Driver").newInstance(); 
       conn = DriverManager 
         .getConnection("jdbc:postgresql://localhost:5432/ruleta", 
         "postgres", "123456"); 
       conn.setAutoCommit(false); 
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
       String date = sdf.format(new Date()); 
       String queryco = "Insert into tickets(no_ticket,animal,hora,fecha,apuestabsf,ruleta, pagado, premio) values (?,?,?,?,?,?,?,?)"; 
       pst = conn.prepareStatement(queryco); 
       for(int row = 0; row<rows; row++) 
       { 

        String jugada = (String)table.getValueAt(row, 0); 
        String ruleta = (String)table.getValueAt(row, 1); 
        String apuesta = (String)table.getValueAt(row, 2); 

        pst.setInt(1, variable.ticket); 
        pst.setString(2, variable.animal); 
        pst.setString(3, hora + ":" + minutos); 
        pst.setString(4, date); 
        pst.setInt(5, Integer.parseInt(textField_1.getText())); 
        if(chckbxNewCheckBox.isSelected() == true) pst.setString(6, selec1); 
        if(chckbxNewCheckBox_1.isSelected() == true) pst.setString(6, selec2); 
        if(chckbxNewCheckBox_2.isSelected() == true) pst.setString(6, selec3); 
        if(chckbxNewCheckBox_3.isSelected() == true) pst.setString(6, selec4); 
        if(chckbxNewCheckBox_4.isSelected() == true) pst.setString(6, selec5); 
        if(chckbxNewCheckBox_5.isSelected() == true) pst.setString(6, selec6); 
        if(chckbxNewCheckBox_6.isSelected() == true) pst.setString(6, selec7); 
        if(chckbxNewCheckBox_7.isSelected() == true) pst.setString(6, selec8); 
        pst.setString(7, "No decidido"); 
        pst.setInt(8, Integer.parseInt(textField_1.getText()) * 30); 

        pst.addBatch(); 

       } 
       pst.executeBatch(); 

       conn.commit(); 
       } 

       catch(Exception e){ 
        System.err.println(e); 
       } 
+0

あなたはPreparedStatementでjugada、ruleta、qpuestaを扱っていませんか? – Jan

+0

イエス・キリスト、私は昨日何が起こったのか分かりません、ごめんなさい、脳のおならがあったに違いない。 –

+0

あなたの質問は今解決されたと考えてください(その場合は削除したいかもしれません)か、まだ助けが必要なものがありますか? – Jan

答えて

0

あなたは、テーブルからいくつかのデータを取得:

String jugada = (String)table.getValueAt(row, 0); 
String ruleta = (String)table.getValueAt(row, 1); 
String apuesta = (String)table.getValueAt(row, 2); 

でもどこでもデータベースにそれらを挿入するために失敗します。

関連する問題