我在网上找到了将查询到的数据存储到 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;
}