Guest User

Untitled

a guest
Sep 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. Retrieving a specific value from mysql database based on parameters
  2. public void getUser(String username, String password)throws SQLException{
  3. String qry = "SELECT UserName From USER....";
  4.  
  5. Connection con = null;
  6. PreparedStatement stmt = null;
  7.  
  8. try{
  9.  
  10. con = DriverManager.getConnection("URL");
  11. stmt = con.prepareStatement(qry);
  12. stmt.executeUpdate();
  13.  
  14. Connection con = null;
  15. PreparedStatement stmt = null;
  16. ResultSet rs = null;
  17. String qry = "SELECT UserName From USER where username=? and password=?";
  18. try{
  19.  
  20. con = DriverManager.getConnection("URL");
  21. stmt = con.prepareStatement(qry);
  22. stmt.setString(1, username);
  23. stmt.setString(2, password);
  24. rs = stmt.executeQuery();
  25. while (rs.next()) {
  26. System.out.println(rs.getString("UserName"));
  27. }
  28. ...
  29.  
  30. Connection c = null;
  31. PreparedStatement s = null;
  32. try {
  33. c = DriverManager.getConnection("URL");
  34. s = c.prepareStatement("SELECT fullname FROM user WHERE username=? and password=?");
  35. s.setString(1, username);
  36. s.setString(2, password);
  37.  
  38. ResultSet rs = s.executeQuery();
  39. if(rs.next())
  40. return rs.getString("fullname");
  41.  
  42. return null; // no user found!
  43. } catch(SQLException e) {
  44. System.err.println(e.getMessage());
  45. } finally {
  46. // close s and c
  47. }
Add Comment
Please, Sign In to add comment