Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. String sql = "Select * from logindb";
  2. try {
  3. Class.forName("com.mysql.jdbc.Driver");
  4. Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/studentrecords", "root", "password");
  5. Statement stmt = con.createStatement();
  6. ResultSet rs = stmt.executeQuery(sql);
  7. String user = txtUsername.getText();
  8. String pwd = new String(txtPassword.getPassword());
  9.  
  10. while (rs.next()) {
  11. String uname = rs.getString("uname");
  12. //Username is the coloumn name in the database table
  13. String password = rs.getString("upass");
  14. if ((user.equals(uname)) && (pwd.equals(password))) {
  15.  
  16. MainPage mp = new MainPage();
  17. mp.setVisible(true);
  18. dispose();
  19.  
  20.  
  21. } else {
  22. lblError.setVisible(true);
  23. }
  24. }
  25. }
  26. catch (Exception e)
  27. {
  28. JOptionPane.showMessageDialog(this, e.getMessage());
  29.  
  30. }
  31.  
  32. String query = "Select * from studentinfo";
  33. try
  34. {
  35. Class.forName("com.mysql.jdbc.Driver");
  36. //connection
  37. Connection conn = (Connection)
  38. //root and username and password for access to the database
  39. DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb","root","");
  40.  
  41. //create the statement that will be used
  42. Statement stmt=conn.createStatement();
  43. ResultSet rs = stmt.executeQuery(query);
  44. String user = txtUsername.getText();
  45. String pwd = new String(txtPassword.getPassword());
  46. while(rs.next())
  47. {
  48. String uname = rs.getString("uname");
  49. //Username is the coloumn name in the database table
  50. String password = rs.getString("upass");
  51. if ((user.equals(uname)) && (pwd.equals(password))) {
  52.  
  53. MainPage mp = new MainPage();
  54. mp.setVisible(true);
  55. dispose();
  56. }
  57. else
  58. {
  59. lblError.setVisible(true);
  60. }
  61. }
  62. }
  63. catch(Exception e)
  64. {
  65. JOptionPane.showMessageDialog(null, "Username or password was incorrect, please try again.", "LOGIN ERROR", JOptionPane.ERROR_MESSAGE);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement