2016-08-05 2 views
1

SQL Serverから取得したデータを挿入しようとしています(WamppServerを使用しました)、データが正しくてもすべてがJTableに表示されませんサーバーから取得します。SQL Serverからデータを正常に取得した後にJTableが空になった

私は問題がパネルの作成であり、データの挿入ではないと仮定しているため、コード全体を掲載しています。

import java.awt.*; 
import java.lang.*; 
import java.sql.*; 
import java.util.*; 
import javax.swing.*; 
import java.awt.event.*; 
import javax.swing.table.*; 


public class MyWindow { 

    private JFrame frame,frame2,frame3,frame4; 
    private JTextArea textArea_3,textArea_3_2; 
    public static Connection connection = null; 
    public static Statement statement = null; 
    public static String sql = null; 
    public static ResultSet rs = null; 
    public JTable table; 


    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        MyWindow window = new MyWindow(); 
        window.frame.setVisible(true); 
        connection = null; 
        statement = null; 
        try 
        { 
         System.out.println("conneting to Database..."); 
         Class.forName("com.mysql.jdbc.Driver");             // Register JDBC driver 
         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/efefe?autoReconnect=true&useSSL=false","root",""); // Open connection 
         System.out.println("Connection Successful"); 
        } 
        catch(ClassNotFoundException error) 
        { 
         System.out.println("Error:" + error.getMessage()); 
        } 

        catch(SQLException error) 
        { 
         System.out.println("Error:" + error.getMessage()); 
        } 
        finally 
        { 
         if (connection != null) 
          try { 
           connection.close(); 
           } 
         catch(SQLException ignore) 
         { 

         } 

         if (statement != null) 
          try { 
           statement.close(); 
           } 

         catch(SQLException ignore) 
         { 

         } 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     ); 
    } 

    public MyWindow() { 
     initialize(); 
    } 


    public void initialize() { 
     frame = new JFrame(); 
     frame3 = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JButton btnNewButton = new JButton("BUTTON2"); 
     btnNewButton.setBounds(133, 21, 121, 23); 
     frame.getContentPane().add(btnNewButton); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       frame3 = new JFrame(); // frame 3 is the new opening window 
       frame3.setVisible(true); 
       frame3.setBounds(200, 300, 550, 300); 
       frame3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // no need to close the whole app, just the new window 
       frame3.getContentPane().setLayout(null); 
       try 
       { 
         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/efefe?autoReconnect=true&useSSL=false","root",""); // Open connection 
         statement = connection.createStatement();    
         sql = "SELECT * FROM customer"; 
         rs = statement.executeQuery(sql);     
         while (rs.next()) { 
          int code = rs.getInt("code"); 
          String gender = rs.getString("gender"); 
          String birthday = rs.getString("birthday"); 
          String status = rs.getString("status"); 
          table = new JTable(); 
          table.setBounds(15, 123, 500, 120); 
          frame3.getContentPane().add(table); 
          table.setVisible(true); 
          ResultSetMetaData metaData = rs.getMetaData(); 
          Vector<String> columnNames = new Vector<String>(); 
          int columnCount = metaData.getColumnCount(); 
           for (int column = 1; column <= columnCount; column++) { 
             columnNames.add(metaData.getColumnName(column)); 
           } 
          Vector<Vector<Object>> data = new Vector<Vector<Object>>(); 
          System.out.println("DATA RETRIEVED FROM SERVER"); 
          while (rs.next()) { 
           Vector<Object> vector = new Vector<Object>(); 
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) { 
             vector.add(rs.getObject(columnIndex)); 
           } 
           data.add(vector); 
           System.out.println(vector); 
          } 
          System.out.println("JTable has " + table.getRowCount() + " rows!"); // 0 = empty 
          System.out.println("JTable has " + table.getColumnCount() + " columns!"); // 0 = empty 
         } 
         rs.close();      
         statement.close();    
         connection.close();    
       } 
       catch(SQLException error) 
       { 
         System.out.println("Error:" + error.getMessage()); 
       } 
      } 
     }); 

    } 
} 

アプリはボタン2が押されたときに新しいウィンドウを開いて、(新しいウィンドウ上の空白がJTableのあること)のJTableを表示するようになっています。私のPC上

実行結果:

conneting to Database... 
Connection Successful 
DATA RETRIEVED FROM SERVER 
[102, female, 1995-02-24, single] 
[103, male, 1985-12-04, married] 
[104, female, 1987-10-08, married] 
[105, female, 1977-09-01, married] 
[106, male, 1994-05-30, single] 
[107, female, 1990-06-30, single] 
[108, male, 1994-07-24, single] 
[109, female, 1991-05-10, single] 
[110, male, 2001-07-12, single] 
JTable has 0 rows! 
JTable has 0 columns 

EDIT:コードは、JTableの充填のために使用さはthis受け入れ答え

+0

テーブルにデータを挿入するのに「DefaultTableModel」を使用しないのはなぜですか?そのより簡単なイモ。 –

+0

これは以前の試みでしたが、JTableに何も出てこないことがわかりました –

+0

DefaultTableModelを使用して前回試したコードを表示できますか?完全なブロックである必要はありません。私はあなたがTableModelをどのようにテーブルに適用したか、TableModelにどのようにデータを追加したかに興味があります。 –

答えて

3

これは1列のみの大まかなスニペットですからコピーされました。それが機能すれば、それをすべての列に拡張することができます。 注:個人的にはテストされていません。

DefaultTableModel model = new DefaultTableModel(); 

Class.forName("com.mysql.jdbc.Driver"); 
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/efefe?autoReconnect=true&useSSL=false","root",""); 

ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM customer"); 

tableModel.addColumn("gender"); 

int row = 0; 

while (rs.next()) { 
    tableModel.addRow(new Object[] {}); 
    tableModel.setValueAt(rs.getString("gender"), row, 0); 
    row++; 
} 

table.setModel(tableModel); 
+0

は私にはうまくいくようです。 –

+0

あなたの問題を解決してくれてうれしいです。幸運:) –

+0

ありがとうたくさんの! –

関連する問題