Advertisement
Guest User

Untitled

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