【问题标题】:Displaying Database records to JTable in JAVA swing在 JAVA swing 中将数据库记录显示到 JTable
【发布时间】:2013-01-19 04:45:50
【问题描述】:

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

    }

请帮忙

【问题讨论】:

    标签: mysql swing jdbc jtable


    【解决方案1】:

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

    "jdbc:mysql://localhost/hostel"

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

    第二点不要在 sql 字符串的末尾使用分号(;)。一世。 e.删除分号

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

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

    【讨论】:

    • 我试过了,但还是不行。我想我犯了一些错误。不管怎样,谢谢你的帮助
    • 非常感谢我得到的输出在代码中犯了愚蠢的错误。
    【解决方案2】:

    使用rs2xml第三方Jar文件显示查询结果,非常有帮助

    【讨论】:

      猜你喜欢
      • 2012-08-28
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多