结果集到数组并返回方法

时间:2013-06-25 20:01:37

标签: java arrays methods resultset

  1. 这是我的数据库类方法的代码,用于获取结果集中的数据并返回一个通用数组,我在数据库中只有一个字段。
  2. 问题:我无法获取数组abc []及其内容,当我返回它时也说arrayIndexoutOfBond错误“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException:1

    public String[] getTableContents(String tableName) {
        ResultSet results = null;
        String[] abc = null;
        int a = 0;
        try {
            System.out.println(conn);
            stmt = conn.createStatement();
            results = stmt.executeQuery("select * from " + tableName);
            ResultSetMetaData rsmd = results.getMetaData();
            // int numberCols = rsmd.getColumnCount();
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                // print Column Names
                System.out.print(rsmd.getColumnLabel(i) + "\t\t");
            }
            System.out.println("\n----------------------------------------");
    
            while (results.next()) {
                System.out.println(results.getString(2) + " 1");
                String em = (results.getString(2));
                System.out.println(em + " 2");
                abc = em.split(" ");
                System.out.println(abc + " 3");
            }
            results.close();
            stmt.close();
    
        } catch (SQLException suresh) {
            System.out.println(suresh);
        }
        System.out.println(abc + " 4");
    
        return abc;
    }
    

0 个答案:

没有答案