开发者

JTable not showing output

Here's my code:

private void show(java.awt.event.ActionEvent evt) {
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "phone";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "school";
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url + dbName, userName, password);
        PreparedStatement pStmt = conn.prepareStatement("SELECT * FROM contacts");
        ResultSet rs = pStmt.executeQuery();
        JFrame frame1 = new JFrame();
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame1.setSize(300, 150);
        frame1.setVisible(true);
        while (rs.next() == true) {

            Object rowData[][] = {{"Name"},
                {"Phone"}};
            Object columnNames[] = {"Column One", "Column Two"};
            JTable table = new JTable(rowData, columnNames);
            JScrollPane scrollPane = new JScrollPane(table);
            frame1.add(sc开发者_如何学PythonrollPane, BorderLayout.CENTER);


        }
        rs.close();
        pStmt.close();
        conn.close();
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

I want to display the record in a separate window but its showing a blank screen. Any corrections suggested?


            Vector columnNames = new Vector();
            Vector data = new Vector();                 
            try{
            Connection conn = null;
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            conn = DriverManager.getConnection(
            "jdbc:oracle:thin:@localhost:1521:XE","yedal ","yedal121288");
            Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("");            
                ResultSetMetaData meta=rs.getMetaData();                
                int columns = meta.getColumnCount();
                for (int i = 1; i <= columns; i++) 
                {
                    columnNames.addElement( meta.getColumnName(i) );
                }
                while (rs.next()) 
                { 
                    Vector row = new Vector(columns);
                    for (int i = 1; i <= columns; i++)
                    {
                        row.addElement( rs.getObject(i) );
                    }   
                    data.addElement( row );
                }
                rs.close();
                stmt.close();
                } 
             catch (SQLException ex) { 
                ex.printStackTrace(); 
                } 
             t= new JTable(data, columnNames); t.setVisible(true);             
             TableColumn col; 
             for (int i = 0; i < t.getColumnCount(); i++) 
             {
                 col = t.getColumnModel().getColumn(i);
                 col.setMaxWidth(200);
             } 
             JScrollPane scrollPane = new JScrollPane(t);


You're creating multiple JTables in your while loop, I assume that's not what you're intending to do, and also you don't give them size. you must set the size of JTable with setPreferredSize or setSize method:

tblObj.setPreferredSize(new Dimension(300,400));

also you pass true to setFillsViewportHeight method for your table content to fill the view port.

Here's a link on how to use JTable:

How To Use JTable


I suppose you see an exception java.lang.ArrayIndexOutOfBoundsException if you look into your console. The JTable is populated with a table model which has two columns but you construct it with an array with data of only one.

Please note since you set the frame to visible before this exception occurs, the frame is shown, the component is added to it but when swing tries to paint it it will fail.

If you replace your test data with e.g.

Object rowData[][] = { { "Name", "Phone" }, { "Name2", "Phone2" } };

it'll work (unless rs.next() is never true).


it is better to use a library file rs2xml.jar . include it into ur library

first create a connection with the database using jdbc. import this --> import net.proteanit.sql.DbUtils;

 String query ="select * from employee"; //let
 pst = conn.prepareStatement(query);

  rs = pst.executeQuery();
  s.append (QueryArea.getText()).append("\n");
  jTable.setModel(DbUtils.resultSetToTableModel(rs));

There will be an exception thrown in this ,so handle it by a try catch statement. if any problem with this u can ask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜