简体   繁体   English

无法从结果集Java填充JTable

[英]Can't fill jtable from resultset java

With this code the table is filled successfully but without the message dialog nothing inserted to the jtable ! 使用此代码,表可以成功填充,但是没有消息对话框,则jtable不会插入任何内容!

String sql2 = "SELECT type, amount FROM lasttransactions where cust_id = ?";
PreparedStatement ps2=conn.prepareStatement(sql2);
ps2.setInt(1,Integer.parseInt(_userid));
ResultSet rs2=ps2.executeQuery();


DefaultTableModel model = (DefaultTableModel) transtable.getModel();
Object[] row; String type; double amount ;
while(rs2.next()) {
     type = rs2.getString("type");
     amount = rs2.getDouble("amount");

    row = new Object []{ type, amount };            


    model.addRow(row);
    JOptionPane.showMessageDialog(null,"added !");   


    transtable.setModel(model);
}

How i did it.May it will help you to solve your problem. 我是怎么做到的。愿它将帮助您解决您的问题。

    ResultSet rs = null;
    try{
        connection = DriverManager.getConnection("jdbc:mysql://localhost/myDB", "root", "password");
        Class.forName("com.mysql.jdbc.Driver");
        Statement stmt = connection.createStatement();
        String query = "select * from somewhere;";
        rs = stmt.executeQuery(query);          
    } catch (Exception e) {
        e.printStackTrace();
    }  finally {
        rs.close();
    }
    table.setModel(DbUtils.resultSetToTableModel(rs));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM