创建一个方法来选择和显示单个表数据库中的所有数据

时间:2011-05-02 17:32:45

标签: java

我需要创建一个方法,从名为Accounts的单个表数据库中选择所有数据 - 正如您所看到的,我的努力只是部分完成,因为我不确定如何继续该方法(我是Java的新手) ,任何建议。我需要做些什么来使它成为一个完整的独立方法?

String select = "Select * from Accounts";


                results = statement.executeQuery(select);
                    }
                    catch(SQLException e){
                       System.out.println("Cannot execute query");
                       e.printStackTrace();
                       System.exit(1);
                          }

                    try{
                 while (results.next()){
                    System.out.println("Name:    " + results.getInt(1));
                    System.out.println("Address: " + results.getString(2) + " " + 
                    results.getString(3));
                         System.out.println("Salary:        " + results.getString(4));
                         System.out.println("Grade:        " + results.getString(5));
                         System.out.println("Year:        " + results.getInt(6));
                         System.out.println("Tax Rate:        " + results.getDouble(7));
                         System.out.println();
                     }
                    }
                    catch(SQLException e){
                         System.out.println("Error retrieving data");
                        e.printStackTrace();
                       System.exit(1);
                    }

1 个答案:

答案 0 :(得分:0)

这是你在看什么?

 public static Object[][] results() {
    int count = 0;
    String select = "Select * from Accounts";        
    results = statement.executeQuery(select);
    try {
        while (results.next()) {
            count++;
        }
        results = statement.executeQuery(select);


    } catch (SQLException e) {
        System.out.println("Cannot execute query");
        e.printStackTrace();
        System.exit(1);
    }
    Object data[][] = new Object[count][7];
    try {
        int i = 0;
        while (results.next()) {
            data[i][0] = results.getInt(1);
            data[i][1] = results.getString(2);
            data[i][2] = results.getString(3);
            data[i][3] = results.getString(4);
            data[i][4] = results.getString(5);
            data[i][5] = results.getString(6);
            data[i][6] = results.getString(7);
            i++;
            /*
            System.out.println("Name:    " + results.getInt(1));
            System.out.println("Address: " + results.getString(2) + " " + 
            results.getString(3));
            System.out.println("Salary:        " + results.getString(4));
            System.out.println("Grade:        " + results.getString(5));
            System.out.println("Year:        " + results.getInt(6));
            System.out.println("Tax Rate:        " + results.getDouble(7));
            System.out.println();
             */
        }
    } catch (SQLException e) {
        System.out.println("Error retrieving data");
        e.printStackTrace();
        System.exit(1);
    }
}

我没有对此进行测试,但如果你正在寻找它,你可以使用它。

- EDIT--
您可以调用此函数并使用返回的结果来构造JTable。我猜你知道如何构建JTable,如果不看link