daily pastebin goal
25%
SHARE
TWEET

DB Hash Example

syedislam Dec 6th, 2016 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.sql.*;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class SwingControlDemo {
  8.     Connection conn = null;
  9.  
  10.     private JTextField usernameField;
  11.     private JPasswordField passwordField;
  12.     private JFrame mainFrame;
  13.  
  14.     public static void main(String[] args) {
  15.         SwingControlDemo swingControlDemo = new SwingControlDemo();
  16.         swingControlDemo.startGUI();
  17.         swingControlDemo.addControlButtons();
  18.  
  19.     }
  20.  
  21.     private void startGUI() {
  22.         mainFrame = new JFrame("Java SWING Examples");
  23.         mainFrame.setSize(300, 120);
  24.         mainFrame.setLocation(300, 300);
  25.         mainFrame.setLayout(new GridLayout(3, 1));
  26.         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         JLabel usernameLabel = new JLabel("Username:");
  28.         usernameField = new JTextField(10);
  29.         passwordField = new JPasswordField(10);
  30.         JLabel passwordLabel = new JLabel("Password:");
  31.  
  32.         JPanel usernamePanel = new JPanel();
  33.         usernamePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  34.         usernamePanel.add(usernameLabel);
  35.         usernamePanel.add(usernameField);
  36.  
  37.         JPanel passwordPanel = new JPanel();
  38.         passwordPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  39.         passwordPanel.add(passwordLabel);
  40.         passwordPanel.add(passwordField);
  41.  
  42.         mainFrame.add(usernamePanel);
  43.         mainFrame.add(passwordPanel);
  44.  
  45.         mainFrame.setVisible(true);
  46.     }
  47.  
  48.     private void addControlButtons() {
  49.         JButton loginButtong = new JButton("Login");
  50.         JButton registerButton = new JButton("Register");
  51.  
  52.         JPanel controlPanel = new JPanel();
  53.  
  54.         controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  55.         controlPanel.add(loginButtong);
  56.         controlPanel.add(registerButton);
  57.  
  58.         loginButtong.setActionCommand("Login");
  59.         registerButton.setActionCommand("Register");
  60.         loginButtong.addActionListener(new ButtonClickListener());
  61.         registerButton.addActionListener(new ButtonClickListener());
  62.         mainFrame.add(controlPanel);
  63.         mainFrame.setVisible(true);
  64.     }
  65.  
  66.     private class ButtonClickListener implements ActionListener {
  67.         public void actionPerformed(ActionEvent e) {
  68.             String command = e.getActionCommand();
  69.             if (command.equals("Login")) {
  70.                 // task 5 start
  71.                 try {
  72.                     Class.forName("org.h2.Driver");
  73.                     Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
  74.                     Statement st = conn.createStatement();
  75.                     String tmpusername = usernameField.getText();
  76.                     String SALT;
  77.                     SALT = "$2a$12$lJFY15CtsjiK9TE5as8qKO";
  78.                     String generatedPassword = BCrypt.hashpw(passwordField.getText(), SALT);
  79.                     String query = "SELECT * FROM Users WHERE username= '" + tmpusername + "' and password='"+ generatedPassword +"';";
  80.                     ResultSet rs = st.executeQuery(query);
  81.                     if (rs.next()) {
  82.                         JOptionPane.showMessageDialog(null, "Username and Password are correct");
  83.                     } else {
  84.                         JOptionPane.showMessageDialog(null, "Username and Password are incorrect, try again!");
  85.                     }
  86.                     rs.close();
  87.                     conn.close();
  88.                 } catch (ClassNotFoundException e1) {
  89.                     // TODO Auto-generated catch block
  90.                     e1.printStackTrace();
  91.                 } catch (SQLException e1) {
  92.                     // TODO Auto-generated catch block
  93.                     e1.printStackTrace();
  94.                 }
  95.             } else if (command.equals("Register")) {
  96.  
  97.                 try {
  98.                     Class.forName("org.h2.Driver");
  99.                     Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
  100.                     Statement st = conn.createStatement();
  101.                     // We want to check if the user already exists.
  102.                     String tmpusername = usernameField.getText();
  103.                     String checkquery = "SELECT * FROM Users WHERE username='" + tmpusername + "';";
  104.                     ResultSet rs = st.executeQuery(checkquery);
  105.                     if (rs.next() == true) {
  106.                         JOptionPane.showMessageDialog(null, "Username exists, try another name");
  107.                     } else {
  108.                         String SALT = "$2a$12$lJFY15CtsjiK9TE5as8qKO";
  109.                         String generatedPassword = BCrypt.hashpw(passwordField.getText(), SALT);
  110.                         String insertQuery = "INSERT INTO Users (username, password) VALUES('" + tmpusername + "', '"
  111.                                 + generatedPassword + "');";
  112.                         int n = st.executeUpdate(insertQuery);
  113.                         if (n == 1) {
  114.                             JOptionPane.showMessageDialog(null, "You have been successfully registered!!");
  115.                         } else {
  116.                             JOptionPane.showMessageDialog(null, "Oops something went wrong");
  117.                         }
  118.                     }
  119.                     rs.close();
  120.                     st.close();
  121.                     conn.close();
  122.                 } catch (ClassNotFoundException e1) {
  123.                     // TODO Auto-generated catch block
  124.                     e1.printStackTrace();
  125.                 } catch (SQLException e1) {
  126.                     // TODO Auto-generated catch block
  127.                     e1.printStackTrace();
  128.                 }
  129.  
  130.             }
  131.  
  132.         }
  133.     }
  134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top