After ResultSet rs = stmt.executeQuery(query); , the code does not execute. Thus, the variables do not get the values from the result set . Can anybody help me resolve this ?
public User storeTempDetails(String s1){
Statement stmt = null;
String query = "select username, account_no,name from accounts where username = ?";
try {
connection = DriverManager.getConnection(DBurl, DBusername, DBpassword);
System.out.println("Database connected!");
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("this line is not printed");
while (rs.next()) {
userName = rs.getString("username");
accountNo = rs.getString("account_no");
name = rs.getString("name");System.out.println("ss"+accountNo);
}
} catch (SQLException e ) {
throw new RuntimeException("Cannot connect the database!", e);
} finally {
User userObj=new User(userName,accountNo,name);
System.out.println("Closing the connection.");
if (connection != null) try { connection.close(); }
catch (SQLException ignore) {}
return userObj;
}
}
Start with something like this. These are utility classes that will make your life easier when you’re starting off with JDBC:
Given those utilities, here’s how I might write it. I’d probably put
Userin amodelpackage, andUserDaoImplwould be a public class in its own .java file. I’m just being lazy: