3

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!

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

4 Answers 4

3

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! How do I secure my "finally" block? I'm still a beginner at this.
You need to wrap each xxx.close() in its own try/catch to make sure that if one fails it won't stop the execution and let all the other resources open. Have a look at this : stackoverflow.com/questions/4507440/…
Thank you, that link was a great explanation.
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.

Comments

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]);
} 

1 Comment

was I just missing "}" so that everything was iterating in the "while" loop? I'm still a beginner, thanks!
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]);
        }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.