Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1. package DIT215;
  2.  
  3.  
  4. import java.awt.Dimension;
  5.  
  6.  
  7. import java.awt.Font;
  8. import java.awt.GridBagLayout;
  9. import java.awt.LayoutManager;
  10. import java.awt.Point;
  11. import java.awt.Toolkit;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14.  
  15. import java.sql.Connection;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.List;
  20.  
  21. import javax.swing.BoxLayout;
  22. import javax.swing.JButton;
  23. import javax.swing.JDialog;
  24. import javax.swing.JFrame;
  25. import javax.swing.JLabel;
  26. import javax.swing.JOptionPane;
  27. import javax.swing.JPanel;
  28. import javax.swing.JPasswordField;
  29. import javax.swing.JTextField;
  30.  
  31. /**
  32.  * Creates a dialog (a pop-up window) when the "Sign Up" button is clicked
  33.  * Dialog contains fields to enter username, password and confirm password.
  34.  * password should be more than 6 characters.
  35.  * @author Boyan Dai
  36.  */
  37. public class Register extends JDialog {
  38.     static boolean successfulSignUp;
  39.    
  40.     private static final long serialVersionUID = 2491294229716316338L;
  41.  
  42.     private JPanel contentPane;
  43.     private JTextField usernameTextField;
  44.     private JPasswordField passwordField1;
  45.     private JPasswordField passwordField2;
  46.     private JLabel tipLabel = new JLabel();
  47.     static JFrame signUpFrame = new JFrame();
  48.  
  49.     public Register() {
  50.  
  51.        
  52.         signUpFrame.setTitle("Sign Up");
  53.  
  54.        
  55.         contentPane = new JPanel();
  56.         //setContentPane(contentPane);
  57.         contentPane.setLayout((LayoutManager) new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
  58.        
  59.        
  60.         JPanel usernamePanel = new JPanel();
  61.         contentPane.add(usernamePanel);
  62.         JLabel usernameLabel = new JLabel("Username: ");
  63.         usernameLabel.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  64.         usernamePanel.add(usernameLabel);
  65.         usernameTextField = new JTextField(20);
  66.         usernameTextField.setColumns(10);
  67.        
  68.         usernameTextField.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  69.         usernamePanel.add(usernameTextField);
  70.        
  71.  
  72.         JPanel passwordPanel1 = new JPanel();
  73.         contentPane.add(passwordPanel1);
  74.         JLabel passwordLabel1 = new JLabel("Password:  ");
  75.         passwordLabel1.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  76.         passwordPanel1.add(passwordLabel1);
  77.         passwordField1 = new JPasswordField(15);
  78.        
  79.         passwordField1.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  80.         passwordField1.setColumns(10);
  81.         passwordPanel1.add(passwordField1);
  82.        
  83.        
  84.         JPanel passwordPanel2 = new JPanel();
  85.         contentPane.add(passwordPanel2);
  86.         JLabel passwordLabel2 = new JLabel("Confirm:    ");
  87.         passwordLabel2.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  88.         passwordPanel2.add(passwordLabel2);
  89.         passwordField2 = new JPasswordField(15);
  90.         passwordField2.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  91.         passwordField2.setColumns(10);
  92.         passwordPanel2.add(passwordField2);
  93.        
  94.        
  95.  
  96.         JPanel buttonPanel = new JPanel();
  97.         contentPane.add(buttonPanel);
  98.         JButton submitButton = new JButton("Submit");
  99.         submitButton.addActionListener(new ActionListener() {
  100.             public void actionPerformed(ActionEvent e) {
  101.    
  102.                
  103.                 String passW = "";
  104.                 // or not
  105.                 String password = passwordField1.getText();
  106.                 String password2 = passwordField2.getText();
  107.                 String UserName = usernameTextField.getText();
  108.                 try{
  109.                     Connection conn = BarfectIO.getConnection();
  110.                     //Check to see if the username exist in the database
  111.                     String sql = "SELECT username, password, user_id FROM barfectionist WHERE username = '" + UserName+"'";
  112.                
  113.                     PreparedStatement prepedStmt = conn.prepareStatement(sql);
  114.                     //prepedStmt.setString(1, UserName);
  115.                     ResultSet myRs = prepedStmt.executeQuery();
  116.                     while (myRs.next()) {
  117.                         passW = myRs.getString("password");
  118.                     }
  119.                
  120.                     if(!exists(UserName)){
  121.        
  122.                         //if not exists, then register.
  123.                     if(isValid(password) && isValid(password2)) {
  124.                        
  125.                        
  126.                         if (password.equals(password2)) {
  127.                                
  128.                                 String sqlInsert = "INSERT INTO barfectionist (username, password) VALUES ('"+UserName + "', '" + password + "');";
  129.                        
  130.                                 PreparedStatement prepedStmtAdd = conn.prepareStatement(sqlInsert);
  131.  
  132.                                 prepedStmtAdd.executeUpdate();
  133.                                 signUpFrame.dispose();
  134.                                 JFrame s = new JFrame();
  135.                                 JOptionPane.showMessageDialog(s, "Sign Up Successful!", "Submitted!",
  136.                                 JOptionPane.INFORMATION_MESSAGE);
  137.                                 s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  138.                                 successfulSignUp = true;
  139.                             }
  140.                         else if(!(password.equals(password2))){
  141.                             JFrame s = new JFrame();
  142.                             JOptionPane.showMessageDialog(s, "Please check your imput!\nTwo password entries do not match each other!", "Check your input!",
  143.                                     JOptionPane.ERROR_MESSAGE);
  144.                         }
  145.                        
  146.                     }else if((!isValid(password)) || (!isValid(password2))){
  147.                         JFrame s = new JFrame();
  148.                         JOptionPane.showMessageDialog(s, "Please check your input Password!\nIt has to be logger than 6 digits!", "Check your input!",
  149.                                 JOptionPane.ERROR_MESSAGE);
  150.                     }
  151.                
  152.                     }
  153.                     else if(!UserName.isEmpty()){
  154.                     JFrame f = new JFrame();
  155.                     JOptionPane.showMessageDialog(f,"Sign Up failed! Pick another user name!" ,"Failed!",
  156.                     JOptionPane.ERROR_MESSAGE);
  157.                     }
  158.                
  159.                    
  160.                 } catch (Exception e1){
  161.                     e1.printStackTrace();
  162.                 }
  163.             }
  164.         });
  165.  
  166.         buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
  167.         tipLabel.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  168.         buttonPanel.add(tipLabel);
  169.         submitButton.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  170.         buttonPanel.add(submitButton);
  171.  
  172.         JButton cancelButton = new JButton("Cancel");
  173.         cancelButton.addActionListener(new ActionListener() {
  174.             public void actionPerformed(ActionEvent e) {
  175.                 signUpFrame.dispose();
  176.             }
  177.  
  178.         });
  179.         cancelButton.setFont(new Font("American Typewriter", Font.PLAIN, 15));
  180.         buttonPanel.add(cancelButton);
  181.  
  182.         signUpFrame.add(contentPane);
  183.         signUpFrame.setVisible(true);
  184.         signUpFrame.pack();// auto-size the window
  185.         // Auto locate the window to the center of the screen
  186.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // getting
  187.                                                                             // the
  188.                                                                             // screen
  189.                                                                             // size
  190.         Point middle = new Point(screenSize.width / 2, screenSize.height / 2); // getting
  191.                                                                                 // the
  192.                                                                                 // middle
  193.                                                                                 // point
  194.         Point newLocation = new Point(middle.x - (signUpFrame.getWidth() / 2), // set
  195.                                                                                 // a
  196.                                                                                 // new
  197.                                                                                 // middle
  198.                                                                                 // point
  199.                                                                                 // according
  200.                                                                                 // to
  201.                                                                                 // the
  202.                                                                                 // size
  203.                                                                                 // of
  204.                                                                                 // the
  205.                                                                                 // window
  206.                 middle.y - (signUpFrame.getHeight() / 2));
  207.         signUpFrame.setLocation(newLocation);// set the location
  208.  
  209.        
  210.     }
  211.  
  212.     public static boolean isValid(String password) {
  213.  
  214.         if (password.length() < 6 || password.length() > 15) {
  215.             return false;
  216.         } else if (password.matches("^[0-9]*$")) {
  217.             return true;
  218.         }
  219.         return true;
  220.     }
  221.     public static boolean exists(String username) {
  222.          String sql = "SELECT username FROM barfectionist WHERE username = ?;";
  223.          Connection conn = BarfectIO.getConnection();
  224.          try{
  225.              PreparedStatement stmt = conn.prepareStatement(sql);
  226.              stmt.setString(1, username);
  227.              ResultSet rs = stmt.executeQuery();
  228.              
  229.              while (rs.next()){
  230.                  
  231.                  if (rs.getString("username").equals(username)){         
  232.                      return true;
  233.                  }else{
  234.                      return false;
  235.                  }
  236.              }
  237.          } catch (SQLException e) {
  238.              e.printStackTrace();
  239.             }
  240.         return false;
  241.     }
  242.  
  243.  
  244.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement