Java 使用从MySQL数据库读取列名的方法返回的数组中充满了;空";

Java 使用从MySQL数据库读取列名的方法返回的数组中充满了;空";,java,mysql,database,arrays,null,Java,Mysql,Database,Arrays,Null,我正在编写一个Web服务,使用Java与我在本地主机上运行的MySQL服务器上设置的数据库进行通信。我的方法ListColumns(),用于在指定表中查找列的名称,并将它们从ResultSet转换为数组,然后返回该数组;正在返回一个大小正确的数组,但该数组是完整的“null”值 我需要columnsList()返回包含列名字符串的数组。 编辑:我已经确认数组columnsList[]实际上正在使用for循环和println接收列名。只是不明白为什么返回值仍然充满空值 还添加了我正在使用的所有代码

我正在编写一个Web服务,使用Java与我在本地主机上运行的MySQL服务器上设置的数据库进行通信。我的方法ListColumns(),用于在指定表中查找列的名称,并将它们从ResultSet转换为数组,然后返回该数组;正在返回一个大小正确的数组,但该数组是完整的“null”值

我需要columnsList()返回包含列名字符串的数组。 编辑:我已经确认数组columnsList[]实际上正在使用for循环和println接收列名。只是不明白为什么返回值仍然充满空值

还添加了我正在使用的所有代码。ListTables()方法非常相似,也有相同的问题,但我想如果我修复了一个,另一个也会修复


提前感谢您的帮助

我没有看到任何明显的错误。我将调试代码并检查while(rs.next())循环中的结果集

下面的教程有一个“列表表”的代码示例

您的代码有两个不同之处:

  • 结果集列通过索引访问
  • columnPattern作为null传递
可能“COLUMN_NAME”不是mysql使用的列名

public static String[] listColumns(){
    String[] columnsList = new String[2];
    try{    

        columnsList[0] = "0";

        if (true) throw new RuntimeException();

        columnsList[1] = "1";

        return columnsList;         
    } catch(Exception ex) {
        //a println to check if the catch is filling up the array with null
        System.out.println("i screwed up");
        System.out.println(ex);         
        return columnsList;
    }
}

public static void main(String[] args) {
    System.out.println(Arrays.asList(listColumns()));
}
此代码打印:

  i screwed up 
  java.lang.RuntimeException 
  [0, null] 

周末后我开始工作,代码最终决定工作

我已经检查了while(rs.next())循环,数组columnsList[]在离开该方法之前被正确的信息填满,但是实际返回的数组中正好充满了null'sHow,您检查了吗?(可能不使用main方法中的println,因为这将只打印数组对象id。)使用println的for循环是否打印列名?我使用println与for循环进行了检查,以打印该数组的内容,并且所有列都正确打印,但是返回的数组仍然是完整的空数组。我认为空数组是catch(exception ex)中异常处理的结果吗?说搞砸了的印刷品永远不会被印刷出来
public static String[] listColumns(){
    String[] columnsList = new String[2];
    try{    

        columnsList[0] = "0";

        if (true) throw new RuntimeException();

        columnsList[1] = "1";

        return columnsList;         
    } catch(Exception ex) {
        //a println to check if the catch is filling up the array with null
        System.out.println("i screwed up");
        System.out.println(ex);         
        return columnsList;
    }
}

public static void main(String[] args) {
    System.out.println(Arrays.asList(listColumns()));
}
  i screwed up 
  java.lang.RuntimeException 
  [0, null]