如何将MySQL查询存储到Java数组中?

时间:2022-04-25 06:28:59

I've found suggestions online to store the queried data into an ArrayList and then to convert the ArrayList into an Array. Below is the code that I have and it appears that I'm not doing this correctly. The SQL syntax is correct (I tested in my MySQL). Any suggestions on how to correct my code would be helpful, thanks!

我在网上找到了将查询数据存储到ArrayList然后将ArrayList转换为Array的建议。下面是我的代码,似乎我没有正确地执行此操作。 SQL语法是正确的(我在MySQL中测试过)。有关如何更正我的代码的任何建议都会有所帮助,谢谢!

public static void dxNameExerciseID(){
    //String dxName = name;
    //String result = null;

    try{
        con = DriverManager.getConnection(url, user, password);  
        pst = con.prepareStatement("SELECT * FROM exercise,condition_exercise,diagnosis WHERE exercise.exercise_id = condition_exercise.exercise_id_fk AND condition_exercise.diagnosis_id_fk = diagnosis.diagnosis_id AND diagnosis.diagnosis_name = 'Adductor Strain';");
        rs = pst.executeQuery();  

        ArrayList<String> list= new ArrayList<String>();
        while (rs.next()) {
            list.add(rs.getString("exercise_id"));

            String[] result = new String[list.size()];
            result = list.toArray(result);

            for(int i =0; i<result.length; i++){
                System.out.println(result[i]);
            }   
        }   

    }catch(SQLException ex){
    }finally {
        try {
            if (rs != null){
                rs.close();
            }
            if (pst != null){
                pst.close();
            }
            if (con != null){
                con.close();
            }
        }catch(SQLException ex){
        }
    }

//return result;  

}

4 个解决方案

#1


2  

This should work better. See how you first create your ArrayList of String by iterating over your ResultSet, and once your list is complete you can create the Array of Strings.

这应该更好。通过迭代ResultSet,了解如何首次创建String的ArrayList,一旦列表完成,您就可以创建字符串数组。

ArrayList<String> list= new ArrayList<String>();
while (rs.next()) {
    list.add(rs.getString("exercise_id"));   
} 

String[] result = new String[list.size()];
result = list.toArray(result);

for(int i =0; i<result.length; i++){
    System.out.println(result[i]);
}

BTW: your finally block is unsafe. If rs.close() fails you won't close your connection.

顺便说一句:你的最后一块是不安全的。如果rs.close()失败,则不会关闭连接。

#2


0  

Poor code in every way. Catch blocks should never be empty. You close your resources incorrectly. If something goes wrong, how will you know? Pass the connection in, don't make the method responsible for getting it. Use PreparedStatement. This code should be thrown away so you can start again.

各方面的代码都很糟糕。 Catch块永远不应为空。您错误地关闭了资源。如果出现问题,你怎么知道?传递连接,不要让方法负责获取它。使用PreparedStatement。应删除此代码,以便您可以重新开始。

#3


0  

This should work - You were creating Array on each iteration which was the reason of your problem

这应该工作 - 你在每次迭代时创建数组,这是你的问题的原因

while (rs.next()) {
       list.add(rs.getString("exercise_id")); 
}   

String[] result = new String[list.size()];
result = list.toArray(result);
for(int i =0; i<result.length; i++){
      System.out.println(result[i]);
} 

#4


0  

Some of your code should be refactored

您的一些代码应该重构

    ArrayList<String> list= new ArrayList<String>();
    while (rs.next()) {
        list.add(rs.getString("exercise_id"));


    }  
     String[] result = new String[list.size()];
        result = list.toArray(result);

        for(int i =0; i<result.length; i++){
            System.out.println(result[i]);
        }   

#1


2  

This should work better. See how you first create your ArrayList of String by iterating over your ResultSet, and once your list is complete you can create the Array of Strings.

这应该更好。通过迭代ResultSet,了解如何首次创建String的ArrayList,一旦列表完成,您就可以创建字符串数组。

ArrayList<String> list= new ArrayList<String>();
while (rs.next()) {
    list.add(rs.getString("exercise_id"));   
} 

String[] result = new String[list.size()];
result = list.toArray(result);

for(int i =0; i<result.length; i++){
    System.out.println(result[i]);
}

BTW: your finally block is unsafe. If rs.close() fails you won't close your connection.

顺便说一句:你的最后一块是不安全的。如果rs.close()失败,则不会关闭连接。

#2


0  

Poor code in every way. Catch blocks should never be empty. You close your resources incorrectly. If something goes wrong, how will you know? Pass the connection in, don't make the method responsible for getting it. Use PreparedStatement. This code should be thrown away so you can start again.

各方面的代码都很糟糕。 Catch块永远不应为空。您错误地关闭了资源。如果出现问题,你怎么知道?传递连接,不要让方法负责获取它。使用PreparedStatement。应删除此代码,以便您可以重新开始。

#3


0  

This should work - You were creating Array on each iteration which was the reason of your problem

这应该工作 - 你在每次迭代时创建数组,这是你的问题的原因

while (rs.next()) {
       list.add(rs.getString("exercise_id")); 
}   

String[] result = new String[list.size()];
result = list.toArray(result);
for(int i =0; i<result.length; i++){
      System.out.println(result[i]);
} 

#4


0  

Some of your code should be refactored

您的一些代码应该重构

    ArrayList<String> list= new ArrayList<String>();
    while (rs.next()) {
        list.add(rs.getString("exercise_id"));


    }  
     String[] result = new String[list.size()];
        result = list.toArray(result);

        for(int i =0; i<result.length; i++){
            System.out.println(result[i]);
        }