1

The function below will pick the highest value and it will display value which are in column place1(in table placeseen) as output based on the ID.So far I only can get the highest value but not the value in place1. I don't know what's wrong with my coding because the output is always shows empty.

   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);
        }
2
  • use try catch you have an sql error select* and also a[i] should be outside the double inverted commas Commented Jul 31, 2015 at 6:12
  • I have tried with your method but still can't get the aaa value.. Commented Jul 31, 2015 at 6:23

2 Answers 2

1

instead of

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

use

  String sql ="Select place1 from placeseen where ID =?";
  PreparedStatement ps = conn.prepareStatement(sql);
  ps.setDouble(1, aa[i]); 

passing aa[i] variable value .

Avoid sql injection

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

2 Comments

is the given condition is true? @user5156075
The placeseen table has 5 column, which are ID, place1,place2,place3 and place4..I want display place1 only –
0

You can try this

// 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

8 Comments

Do one thing, just sysout your preparedStatement after setting values, you will get query and they try to execute in mysql directly. See my updated answer.
I still getting the same output..which is the highest value only
did you get the query in your console?
I get this "com.mysql.jdbc.JDBC4PreparedStatement@151376dc: Select Place1 from placeseen where ID = 0.9805806756909202"....I have changed * to Place1
did you try to execute it in mysql directly?
|

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.