Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a single value back from query vs resultset

I have a JSP file that runs a select statement against an Oracle database.

All the examples I have seen use something like:

Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from data");
 while(rs.next(){
String name=rs.getString("name");
String add=rs.getString("address");
out.println(name+" "+add);
 }

I will never have more than one row coming back is there an alternative to ResultSet and a while loop to get at my returning single row of data?

like image 409
Baxter Avatar asked Apr 27 '12 19:04

Baxter


1 Answers

I have used similar kind of thing to validate user login.

    String sql = "SELECT * FROM login WHERE username=? AND password=?";
    try {
          PreparedStatement statement;
          statement = connection.prepareStatement(sql);

          statement.setString(1, "hardik"); // set input parameter 1
          statement.setString(2, "welcome"); // set input parameter 2
        ResultSet rs = statement.executeQuery();
        if(rs.next()){
            // fetch data from resultset
        }
    }catch(SQLException sqle){
        sqle.printStackTrace();
    }
like image 58
Hardik Mishra Avatar answered Sep 30 '22 03:09

Hardik Mishra