JTable没有从MySQL填充数据

时间:2013-01-11 19:29:51

标签: java mysql netbeans jtable swig

我正在尝试将存储在我的数据库中的数据填充到JTable中。我有一个println语句"记录已添加到db"并将其打印18次,以便db中的行数与打印的语句数相匹配。但是我的JTable没有显示任何内容,我也不知道错误在哪里。

代码:

try {
        conn = SQLConnect.ConnectDb();
        String sql = "SELECT * FROM CriminalRecords WHERE FName = ? and Sname = ?";

        pst = conn.prepareStatement(sql);
        pst.setString(1, NameSearch.getText());
        pst.setString(2, SurnameSearch.getText());
        rs = pst.executeQuery();

            //display data from db into table hmm  
            ResultSetMetaData md = rs.getMetaData();
            int columnNumber = md.getColumnCount();
            Vector columns = new Vector(columnNumber);

            //  Get column names
            //store column names
            for(int i=1; i<=columnNumber; i++) 
            {
                columns.add(md.getColumnName(i));
            } 

            Vector data = new Vector();
            Vector row;

        //store row data
        while(rs.next())
        {
            row = new Vector(columnNumber);
            for(int i=1; i<=columnNumber; i++)
            {
                row.add(rs.getObject(i));
                System.out.println("Records added to Table");
            }
            data.add(row);

        }
        JTable table1 = new JTable(data, columns);
        this.add(table1);
        rs.close();
        pst.close();
        conn.close();

JTable代码

/---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {" ", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                },
                new String[] {
                    "First Name", "Middle Name", "Surname", "Date of Birth", "Address", "Home Phone", "Business Phone", "Mobile Phone", "Resident Status", "Sex", "Race", "Incident Location", "Zone", "Premise Type", "Date Recorded", "Time Recorded", "Weapons", "Crime Offences"
                }
            ));
            table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            scrollPane1.setViewportView(table1);

1 个答案:

答案 0 :(得分:0)

所以解决方案虽然不是最好的但它有效并且现在对我有用。

import net.proteanit.sql.DbUtils;
//
//
//
try {
        conn = SQLConnect.ConnectDb();
        String sql = "SELECT * FROM CriminalRecords WHERE FName = ? and Sname = ?";

        pst = conn.prepareStatement(sql);
        pst.setString(1, NameSearch.getText());
        pst.setString(2, SurnameSearch.getText());
        rs = pst.executeQuery();

        table1.setModel(DbUtils.resultSetToTableModel(rs));

} catch (SQLException ex) {
        Logger.getLogger(DatabaseP.class.getName()).log(Level.SEVERE, null, ex);
    }