1

我试图在 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)
    {

    }

请帮忙

4

2 回答 2

1

首先,您缺少本地主机的端口号。改变

"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 方法的参数;

IEjtable.setmodel(PlayList());

于 2013-02-17T15:08:01.290 回答
0

使用 rs2xml 第三方 Jar 文件显示查询结果。这很有帮助

于 2013-11-27T17:58:56.233 回答