Java 当ID为数组形式时,如何从SQL检索值

Java 当ID为数组形式时,如何从SQL检索值,java,mysql,sql,arrays,oracle,Java,Mysql,Sql,Arrays,Oracle,下面的函数将选择最高值,并根据ID将列place1(表placeseen)中的值显示为输出。到目前为止,我只能获得最高值,但无法获得place1中的值。 我不知道我的编码有什么问题,因为输出总是空的 private void pick_highest_value_here_and_display(ArrayList<Double> value) throws Exception { // TODO Auto-generated method stub

下面的函数将选择最高值,并根据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);
        }
private void pick_highest_value_here_和_display(ArrayList value)引发异常{
//TODO自动生成的方法存根
double aa[]=value.stream().mapToDouble(v->v.doubleValue()).toArray();
最高双倍=aa[0+1];
对于(int i=0;i最高){
最高=aa[i];
String sql=“Select*from placeseen,其中ID=aa[i]”;
DatabaseConnection db=新建DatabaseConnection();
连接conn=db.getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
结果集rs=ps.executeQuery();
如果(rs.next())
{  
串aaa;
aaa=rs.getString(“place1”);
系统输出打印项次(aaa);
}
ps.close();
rs.close();
康涅狄格州关闭();
}
}
系统输出打印项次(最高);
}
而不是

  String sql ="Select * from placeseen where ID =aa[i]";//aa[i] taking a value
使用

传递
aa[i]
变量值

你可以试试这个

// as you are using preparedStatement you can use ? and then set value for it to prevent sql injection
String sql = "Select * from placeseen where ID = ?";
DatabaseConnection db = new DatabaseConnection();
Connection conn = db.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDouble(1, aa[i]);  // 1 represent first attribute represented by ?

System.out.println(ps); // this will print query in console

ResultSet rs = ps.executeQuery();
if (rs.next()) {
    System.out.println("Inside rs.next()");  // for debug purpose
    String aaa;
    aaa=rs.getString("place1");
    System.out.println(aaa);
}
// remaining code

使用try-catch如果出现sql错误
select*
,并且
a[i]
应该在双倒逗号之外,我已尝试使用您的方法,但仍然无法获得aaa值。给定的条件是否为真@User5156075 placeseen表有5列,分别是ID、place1、place2、place3和place4。我只想显示place1–做一件事,在设置值后,只需sysout您的preparedStatement,您将得到查询,它们将直接在mysql中执行。查看我的更新答案。我仍然得到相同的输出..哪一个是最高值?您是否在控制台中得到查询?我得到这个“com.mysql.jdbc”。JDBC4PreparedStatement@151376dc:从placeseen中选择Place1,其中ID=0.9805806756909202“…我已将*更改为Place1did您是否尝试直接在mysql中执行它?
// as you are using preparedStatement you can use ? and then set value for it to prevent sql injection
String sql = "Select * from placeseen where ID = ?";
DatabaseConnection db = new DatabaseConnection();
Connection conn = db.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDouble(1, aa[i]);  // 1 represent first attribute represented by ?

System.out.println(ps); // this will print query in console

ResultSet rs = ps.executeQuery();
if (rs.next()) {
    System.out.println("Inside rs.next()");  // for debug purpose
    String aaa;
    aaa=rs.getString("place1");
    System.out.println(aaa);
}
// remaining code