0

I am trying to get the result of SELECT 1 query using PreparedStatement. I am unable to get the result. Since the select 1 query result has no column name, so how can I retrieve the resultant column value.

Connection conn = DB.getconnnection();
PreparedStatement ps = conn.prepareStatement("select 1 from tbl_user where username = ? and password = ?");
ps.setString(1,txtUsername.getText());
ps.setString(2,String.valueOf(txtPassword.getPassword()));
ResultSet rs=ps.executeQuery();
String status=rs.getString("");
System.out.println(status);
conn.close();

I referred the code from java tutorial, I can not understand why is there ? in the query statement (select 1 from tbl_user where username = ? and password = ?). Please clarify.

nischalinn
  • 1,133
  • 2
  • 12
  • 38
  • @kayaman How is this question duplicate from the question you said. Better you read the question properly. Please remove this tag. – nischalinn Mar 23 '17 at 13:07
  • 1
    You should do if(rs.next()){ int val = rs.getInt(1); } – dsp_user Mar 23 '17 at 13:11
  • `?` is a placeholder that you replace with the required username in `ps.setString(1,txtUsername.getText());` More info here http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html You can retrieve a column result value using its index `rs.getString(1)`More info here https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html#retrieve_rs – RubioRic Mar 23 '17 at 13:11
  • 1
    I think [this](http://stackoverflow.com/questions/3727688/what-does-a-question-mark-represent-in-sql-queries) answers part of your question. – Rishi Latchmepersad Mar 23 '17 at 13:12
  • The duplicate explains very well what's the difference with a regular statement (i.e. a regular SQL query), and `PreparedStatement`. It even answers this question, so it's a duplicate. You can use `getString(1)` to get columns based on index instead of name, that's very basic stuff. – Kayaman Mar 23 '17 at 13:20

0 Answers0