下面的函数将选择最高值,并根据 ID 将 place1 列(表 placeseen)中的值显示为输出。到目前为止,我只能获得最高值,而不能获得 place1 中的值。 我不知道我的编码有什么问题,因为输出总是显示为空。
private void pick_highest_value_here_and_display(ArrayList<Double> value) throws Exception {
// TODO Auto-generated method stub
double aa[]=value.stream().mapToDouble(v -> v.doubleValue()).toArray();
double highest=aa[0+1];
for(int i=0;i<aa.length;i++)
{
if(aa[i]>highest){
highest=aa[i];
String sql ="Select* from placeseen where ID =aa[i]";
DatabaseConnection db = new DatabaseConnection();
Connection conn =db.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next())
{
String aaa;
aaa=rs.getString("place1");
System.out.println(aaa);
}
ps.close();
rs.close();
conn.close();
}
}
System.out.println(highest);
}
最佳答案
代替
String sql ="Select * from placeseen where ID =aa[i]";//aa[i] taking a value
使用
String sql ="Select place1 from placeseen where ID =?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDouble(1, aa[i]);
传递 aa[i]
变量值。
关于java - 当 ID 为数组形式时,如何从 SQL 中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31739186/