Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DIT215;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.GridBagLayout;
- import java.awt.LayoutManager;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
- import javax.swing.BoxLayout;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- /**
- * Creates a dialog (a pop-up window) when the "Sign Up" button is clicked
- * Dialog contains fields to enter username, password and confirm password.
- * password should be more than 6 characters.
- * @author Boyan Dai
- */
- public class Register extends JDialog {
- static boolean successfulSignUp;
- private static final long serialVersionUID = 2491294229716316338L;
- private JPanel contentPane;
- private JTextField usernameTextField;
- private JPasswordField passwordField1;
- private JPasswordField passwordField2;
- private JLabel tipLabel = new JLabel();
- static JFrame signUpFrame = new JFrame();
- public Register() {
- signUpFrame.setTitle("Sign Up");
- contentPane = new JPanel();
- //setContentPane(contentPane);
- contentPane.setLayout((LayoutManager) new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
- JPanel usernamePanel = new JPanel();
- contentPane.add(usernamePanel);
- JLabel usernameLabel = new JLabel("Username: ");
- usernameLabel.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- usernamePanel.add(usernameLabel);
- usernameTextField = new JTextField(20);
- usernameTextField.setColumns(10);
- usernameTextField.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- usernamePanel.add(usernameTextField);
- JPanel passwordPanel1 = new JPanel();
- contentPane.add(passwordPanel1);
- JLabel passwordLabel1 = new JLabel("Password: ");
- passwordLabel1.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- passwordPanel1.add(passwordLabel1);
- passwordField1 = new JPasswordField(15);
- passwordField1.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- passwordField1.setColumns(10);
- passwordPanel1.add(passwordField1);
- JPanel passwordPanel2 = new JPanel();
- contentPane.add(passwordPanel2);
- JLabel passwordLabel2 = new JLabel("Confirm: ");
- passwordLabel2.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- passwordPanel2.add(passwordLabel2);
- passwordField2 = new JPasswordField(15);
- passwordField2.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- passwordField2.setColumns(10);
- passwordPanel2.add(passwordField2);
- JPanel buttonPanel = new JPanel();
- contentPane.add(buttonPanel);
- JButton submitButton = new JButton("Submit");
- submitButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String passW = "";
- // or not
- String password = passwordField1.getText();
- String password2 = passwordField2.getText();
- String UserName = usernameTextField.getText();
- try{
- Connection conn = BarfectIO.getConnection();
- //Check to see if the username exist in the database
- String sql = "SELECT username, password, user_id FROM barfectionist WHERE username = '" + UserName+"'";
- PreparedStatement prepedStmt = conn.prepareStatement(sql);
- //prepedStmt.setString(1, UserName);
- ResultSet myRs = prepedStmt.executeQuery();
- while (myRs.next()) {
- passW = myRs.getString("password");
- }
- if(!exists(UserName)){
- //if not exists, then register.
- if(isValid(password) && isValid(password2)) {
- if (password.equals(password2)) {
- String sqlInsert = "INSERT INTO barfectionist (username, password) VALUES ('"+UserName + "', '" + password + "');";
- PreparedStatement prepedStmtAdd = conn.prepareStatement(sqlInsert);
- prepedStmtAdd.executeUpdate();
- signUpFrame.dispose();
- JFrame s = new JFrame();
- JOptionPane.showMessageDialog(s, "Sign Up Successful!", "Submitted!",
- JOptionPane.INFORMATION_MESSAGE);
- s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- successfulSignUp = true;
- }
- else if(!(password.equals(password2))){
- JFrame s = new JFrame();
- JOptionPane.showMessageDialog(s, "Please check your imput!\nTwo password entries do not match each other!", "Check your input!",
- JOptionPane.ERROR_MESSAGE);
- }
- }else if((!isValid(password)) || (!isValid(password2))){
- JFrame s = new JFrame();
- JOptionPane.showMessageDialog(s, "Please check your input Password!\nIt has to be logger than 6 digits!", "Check your input!",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- else if(!UserName.isEmpty()){
- JFrame f = new JFrame();
- JOptionPane.showMessageDialog(f,"Sign Up failed! Pick another user name!" ,"Failed!",
- JOptionPane.ERROR_MESSAGE);
- }
- } catch (Exception e1){
- e1.printStackTrace();
- }
- }
- });
- buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
- tipLabel.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- buttonPanel.add(tipLabel);
- submitButton.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- buttonPanel.add(submitButton);
- JButton cancelButton = new JButton("Cancel");
- cancelButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- signUpFrame.dispose();
- }
- });
- cancelButton.setFont(new Font("American Typewriter", Font.PLAIN, 15));
- buttonPanel.add(cancelButton);
- signUpFrame.add(contentPane);
- signUpFrame.setVisible(true);
- signUpFrame.pack();// auto-size the window
- // Auto locate the window to the center of the screen
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // getting
- // the
- // screen
- // size
- Point middle = new Point(screenSize.width / 2, screenSize.height / 2); // getting
- // the
- // middle
- // point
- Point newLocation = new Point(middle.x - (signUpFrame.getWidth() / 2), // set
- // a
- // new
- // middle
- // point
- // according
- // to
- // the
- // size
- // of
- // the
- // window
- middle.y - (signUpFrame.getHeight() / 2));
- signUpFrame.setLocation(newLocation);// set the location
- }
- public static boolean isValid(String password) {
- if (password.length() < 6 || password.length() > 15) {
- return false;
- } else if (password.matches("^[0-9]*$")) {
- return true;
- }
- return true;
- }
- public static boolean exists(String username) {
- String sql = "SELECT username FROM barfectionist WHERE username = ?;";
- Connection conn = BarfectIO.getConnection();
- try{
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setString(1, username);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()){
- if (rs.getString("username").equals(username)){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement