0
  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;
    }
    
4

0 回答 0