Java 将结果集从JDBC加载到二维表中

Java 将结果集从JDBC加载到二维表中,java,arrays,object,jdbc,resultset,Java,Arrays,Object,Jdbc,Resultset,我想将来自JDBCSQL查询的结果添加到一个二维对象数组中,以便将来在表GUI中使用。 不幸的是,上面的代码不起作用,我也不知道为什么——我在将结果保存到数据对象时出错 我得到的错误类型是NullPointerException。我想您有这样一个对象: public void selectStudent() throws SQLException{ try{ con= this.getConnection(); if(con!=null){

我想将来自JDBCSQL查询的结果添加到一个二维对象数组中,以便将来在表GUI中使用。 不幸的是,上面的代码不起作用,我也不知道为什么——我在将结果保存到数据对象时出错


我得到的错误类型是NullPointerException。

我想您有这样一个对象:

public void selectStudent() throws SQLException{
    try{
        con= this.getConnection();
        if(con!=null){
            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT * FROM dbo.Student";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);

            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                for(int i=0;i<=5;i++){
                      data[rs.getRow()][i]=rs.getString(i+1);
                }
            }
            closeConnection();
        }else System.out.println("Error: No active Connection");
   }catch(Exception e){
        e.printStackTrace();
   }
}
Object[][] data; 
public class TypeObject {

    private String s1;
    private String s2;
    private String s3;
    private String i1;
    private boolean b1;

    public TypeObject(String s1, String s2, String s3, String i1, boolean b1) {
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
        this.i1 = i1;
        this.b1 = b1;
    }

    //getters and setters
}
因此,您会得到一个错误NullPointerException,因为它没有初始化,所以在将结果放入此数组之前,您应该像这样初始化它:

public void selectStudent() throws SQLException{
    try{
        con= this.getConnection();
        if(con!=null){
            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT * FROM dbo.Student";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);

            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                for(int i=0;i<=5;i++){
                      data[rs.getRow()][i]=rs.getString(i+1);
                }
            }
            closeConnection();
        }else System.out.println("Error: No active Connection");
   }catch(Exception e){
        e.printStackTrace();
   }
}
Object[][] data; 
public class TypeObject {

    private String s1;
    private String s2;
    private String s3;
    private String i1;
    private boolean b1;

    public TypeObject(String s1, String s2, String s3, String i1, boolean b1) {
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
        this.i1 = i1;
        this.b1 = b1;
    }

    //getters and setters
}
这会让您越来越多地定义属性的类型,因此我建议您创建一个新类型,例如:

Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false)}, 
            {"John", "Doe", "Rowing", new Integer(3), new Boolean(true)}};
因此,如果要使用和对象[][],请使用TypeObject[],您可以轻松设置或获取如下属性:

public void selectStudent() throws SQLException{
    try{
        con= this.getConnection();
        if(con!=null){
            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT * FROM dbo.Student";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);

            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                for(int i=0;i<=5;i++){
                      data[rs.getRow()][i]=rs.getString(i+1);
                }
            }
            closeConnection();
        }else System.out.println("Error: No active Connection");
   }catch(Exception e){
        e.printStackTrace();
   }
}
Object[][] data; 
public class TypeObject {

    private String s1;
    private String s2;
    private String s3;
    private String i1;
    private boolean b1;

    public TypeObject(String s1, String s2, String s3, String i1, boolean b1) {
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
        this.i1 = i1;
        this.b1 = b1;
    }

    //getters and setters
}
因此,这种方式的优点是:

您可以在表中添加新列,这样就可以轻松地添加新列 类型对象类中的字段 您可以根据需要创建构造函数。 及
什么类型的数据,您已经初始化了吗?我们基本上刚刚创建了一个简单的对象[]数据。它没有初始化。我们使用对象[],因为swingUI表接受它作为它们的数据。我以为对象是一种大学类型?因为在表格教程中,它是按如下方式完成的:对象[][]数据={{Kathy,Smith,滑雪板,新整数5,新布尔False},{John,Doe,Rowing,新整数3,新布尔True};我们需要在数组中存储多个数据类型。是的,您可以先将对象强制转换为您正在使用的类型,例如:对象[][]数据=新字符串[6][6];如果我们需要多种数据类型,就像我们的示例中那样,该怎么办?此表可以同时存储字符串、整数和布尔值。然后您需要创建自己的对象。您在这里要做的是获取字符串值data[rs.getRow][i]=rs.getStringi+1;