Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Retrieving a specific value from mysql database based on parameters
- public void getUser(String username, String password)throws SQLException{
- String qry = "SELECT UserName From USER....";
- Connection con = null;
- PreparedStatement stmt = null;
- try{
- con = DriverManager.getConnection("URL");
- stmt = con.prepareStatement(qry);
- stmt.executeUpdate();
- Connection con = null;
- PreparedStatement stmt = null;
- ResultSet rs = null;
- String qry = "SELECT UserName From USER where username=? and password=?";
- try{
- con = DriverManager.getConnection("URL");
- stmt = con.prepareStatement(qry);
- stmt.setString(1, username);
- stmt.setString(2, password);
- rs = stmt.executeQuery();
- while (rs.next()) {
- System.out.println(rs.getString("UserName"));
- }
- ...
- Connection c = null;
- PreparedStatement s = null;
- try {
- c = DriverManager.getConnection("URL");
- s = c.prepareStatement("SELECT fullname FROM user WHERE username=? and password=?");
- s.setString(1, username);
- s.setString(2, password);
- ResultSet rs = s.executeQuery();
- if(rs.next())
- return rs.getString("fullname");
- return null; // no user found!
- } catch(SQLException e) {
- System.err.println(e.getMessage());
- } finally {
- // close s and c
- }
Add Comment
Please, Sign In to add comment