Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. //Login button
  2. JButton btnNewButton = new JButton("Login");
  3. btnNewButton.addActionListener(new ActionListener() {
  4. public void actionPerformed(ActionEvent arg0)
  5. {
  6. try {
  7.  
  8. // connection to databse
  9. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myusers", "root", "root");
  10.  
  11. String query = "select * from brugere where username=? and password=?";
  12. PreparedStatement myStnt = myConn.prepareStatement(query);
  13. myStnt.setString(1, textField.getText());
  14. myStnt.setString(2, passwordField.getText());
  15.  
  16. //// Create statement
  17. ResultSet myRs = myStnt.executeQuery();
  18. //
  19. int count = 0;
  20. while (myRs.next()) {
  21. count = count + 1;
  22. }
  23.  
  24. if (count == 1) {
  25. myRs.close();
  26. myStnt.close();
  27. myConn.close();
  28.  
  29. MainMenu mm = new MainMenu();
  30. mm.setVisible(true);
  31. frame.dispose();
  32.  
  33. frame.dispose();
  34. }
  35. else if (count > 1)
  36. {
  37. JOptionPane.showMessageDialog(null, "Duplicate Username and password");
  38. }
  39. else
  40. {
  41. JOptionPane.showMessageDialog(null, "Sorry wrong username or password");
  42.  
  43. }
  44.  
  45. }catch (Exception e){
  46. JOptionPane.showMessageDialog(null, e);
  47. }
  48. }
  49. });
  50. btnNewButton.setBounds(288, 96, 89, 67);
  51. frame.getContentPane().add(btnNewButton);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement