在JAVA swing中向JTable显示数据库记录

时间:2013-02-04 13:13:10

标签: mysql swing jdbc jtable

我试图在Jtable中显示数据库记录,但我没有得到正确的代码。我使用IDE netbeans和数据库是mysql。我可以看到面板和滚动窗格,但不显示该表。我觉得表属性有问题或者不知道它是不可见的。 我的代码如下:

try{        
    panel_paylist.setVisible(true);
    String dbUrl = "jdbc:mysql://localhost/hostel";
    String dbClass = "com.mysql.jdbc.Driver";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection(dbUrl,"root","17121990");
    System.out.println("Connected!!!!");
    MainScreen obj = new MainScreen(conn); 

    String[] columnNames = {"First Name",
                    "Last Name",
                    "Amount Recvd.",
                    "Date","Cheque/cash","cheque no","Balance Amt.","Total Amt.",
                    "Vegetarian"};

    ResultSet rs = null;
    Statement sql= null;
    ArrayList<Object[]> data = new ArrayList<>();
    String query="SELECT firstname,lastname, amountreceivd,dte,creditcashcheque,cheque_no,balance_amt, totalamount,Remark FROM payment;";
    sql = con.createStatement();
    sql.executeQuery(query);
    rs = sql.getResultSet();

    while(rs.next()){
                Object[] row = new Object[]{rs.getString(1), 
                rs.getString(2), 
                rs.getInt(3),
                rs.getString(4),
                rs.getString(5),
                rs.getString(6),
                rs.getInt(7),
                rs.getInt(8),
                rs.getString(9)};
                data.add(row);
            }
    Object[][] realData = data.toArray(new Object[data.size()][]);
    table_paylist= new JTable(realData, columnNames);
    scroll_paylist= new JScrollPane(table_paylist);        
    table_paylist.setPreferredScrollableViewportSize(new Dimension(800, 200));
    table_paylist.setFillsViewportHeight(true);
    panel_paylist.setLayout(new BorderLayout());
    panel_paylist.add(scroll_paylist, BorderLayout.CENTER); 
    }
    catch(Exception e)
    {

    }

请帮助

2 个答案:

答案 0 :(得分:1)

首先,您缺少localhost的端口号。 改变

"jdbc:mysql://localhost/hostel"

"jdbc:mysql://localhost:3306/hostel";

第二点不要在sql字符串的末尾使用分号(;)。一世。即从

中删除分号

"SELECT firstname,lastname, amountreceivd,dte,creditcashcheque,cheque_no,balance_amt, totalamount,Remark FROM payment;"

你也可以尝试这个

public DefaultTableModel PlayList() throws ClassNotFoundException,SQLException,ParseException
    {

        String[] columnNames = {"First Name","Last Name","Amount Recvd.","Date","Cheque/cash","cheque no","Balance Amt.","Total Amt.","Vegetarian"};

        DefaultTableModel dtm = new DefaultTableModel(columnNames , 0);

        dtm.setColumnCount(9);

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/hostel","root","17121990");
        PreparedStatement ps1 = null;
        ResultSet rs1 = null;

        try
            {
                Class.forName("com.mysql.jdbc.Driver");
                ps1=conn.prepareStatement("SELECT firstname,lastname,mountreceivd,dte,creditcashcheque,cheque_no,balance_amt,totalamount,Remark FROM payment");
                rs1 = ps1.executeQuery();
                while(rs1.next())
                {
                   dtm.addRow(new Object[]{rs1.getString(1)
                                            rs.getString(2), 
                                            rs.getInt(3),
                                            rs.getString(4),
                                            rs.getString(5),
                                            rs.getString(6),
                                            rs.getInt(7),
                                            rs.getInt(8),
                                            rs.getString(9)});
                }
            }
        finally
            {
                rs1.close();
                ps1.close();
                conn.close();
            }
        return dtm;
    }

现在使用PlayList()方法作为jtable的setmodel方法的参数;

我。即jtable.setmodel(PlayList());

答案 1 :(得分:0)

使用rs2xml第三方Jar文件显示查询结果。这非常有用