Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- import org.apache.commons.dbutils.DbUtils;
- import org.apache.commons.dbutils.QueryRunner;
- import org.apache.commons.dbutils.ResultSetHandler;
- import org.apache.commons.dbutils.handlers.ColumnListHandler;
- public class Register extends JFrame {
- static boolean successfulSignUp;
- public Register() {
- }
- private static final long serialVersionUID = 2491294229716316338L;
- private JPanel contentPane;
- private JTextField usernameTextField;
- private JPasswordField passwordField1;
- private JPasswordField passwordField2;
- private JLabel tipLabel = new JLabel();
- public void initialize() {
- JFrame signUpFrame = new JFrame();
- signUpFrame.setTitle("Sign Up");
- JPanel signUpPanel = new JPanel(new GridBagLayout());
- signUpFrame.getContentPane().add(signUpPanel);
- 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);
- String UserName = usernameTextField.getText();
- 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();
- 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 = ?";
- 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)) {
- //TODO Check if the logic part of the SQL Queries are correct!
- if (password.equals(password2)) {
- //TODO Check if the logic part of the SQL Queries are correct!
- String sqlInsert = "INSERT INTO `users`( `username`, `password`) VALUES (["+UserName+"],["+password+"]);";
- PreparedStatement prepedStmtAdd = conn.prepareStatement(sql);
- ResultSet myRsInsert = prepedStmt.executeQuery();
- //TODO check SQL query is successfully executed
- if( save(UserName, password)){
- 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 {
- JFrame s = new JFrame();
- JOptionPane.showMessageDialog(s, "Failed to register", "Failed!",
- JOptionPane.ERROR_MESSAGE);
- s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- successfulSignUp = false;
- }
- }
- 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!" ,"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.getContentPane().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) {
- QueryRunner runner = new QueryRunner();
- String sql = "select id from users where username = '" + username + "';";
- Connection conn = BarfectIO.getConnection();
- ResultSetHandler<List<Object>> rsh = new ColumnListHandler();
- try {
- List<Object> result = runner.query(conn, sql, rsh);
- if (result.size() > 0) {// if exist
- return true;// return true
- } else {// if not exist
- return false;// return false
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- DbUtils.closeQuietly(conn);// close connection
- }
- return false;
- }
- public static boolean save(String username, String password) {
- QueryRunner runner = new QueryRunner();
- String sql = "insert into users (username, password) values (?, ?);";
- Connection conn = BarfectIO.getConnection();
- try {
- int result = runner.update(conn, sql);// save user
- if (result > 0) {// return true if success
- return true;
- } else {// return false if not
- return false;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- DbUtils.closeQuietly(conn);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement