/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package computinggraphtransformations; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.UnsupportedEncodingException; import javax.swing.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.sql.*; /** * * @author Joey */ public final class NewsqlLogin extends JFrame{ private boolean success = false; public static String username; //public static String password; ///** //* @param args the command line arguments //*/ //public static void Main(String[] args) { // TODO code application logic here //new NewsqlLogin(); //Shows the GUI //} // public final class PasswordJDialog extends JDialog { // private final JPanel backgroundPanel; // } public NewsqlLogin() { Components(); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } private void Components() { Container c = getContentPane(); //super "Login Window"; //Create the Java components JLabel lblUsername = new JLabel("Username: "); lblUsername.setHorizontalAlignment(JLabel.CENTER); final JTextField txtUsername = new JTextField(10); JLabel lblPassword = new JLabel("Password: "); lblPassword.setHorizontalAlignment(JLabel.CENTER); final JPasswordField txtPassword = new JPasswordField(10); txtPassword.setEchoChar('*'); JButton btnSubmit = new JButton("Submit"); JButton btnCancel = new JButton("Cancel"); //btnSubmit.setSize(300, 300); btnSubmit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { username = txtUsername.getText().trim(); String password = null; password = new String(txtPassword.getPassword()).trim(); success = submit(username, password); if(success) { JOptionPane.showMessageDialog(null, "Successful login"); System.out.println("logged set to true"); String id[] = new String[1]; id[0]=username; Main.mainUsername=username; Main.logged = true; Main.main(id); Main.execLoop = true; System.out.println("Logged, Username, Exec Loop"); System.out.println(Main.logged); System.out.println(Main.mainUsername); System.out.println(Main.execLoop); System.out.println("New main"); new Main(); System.out.println("New values"); System.out.println(Main.logged); System.out.println(Main.mainUsername); System.out.println(Main.execLoop); System.out.println("Main class started"); close(); } else { JOptionPane.showMessageDialog(null, "Incorrect username or password"); } } }); btnCancel.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { close(); } }); //Add the components to the JFrame JPanel cp = new JPanel(); cp.setPreferredSize(new Dimension(300,100)); cp.setOpaque(false); cp.setLayout(new GridLayout(3, 3, 5, 5)); cp.add(lblUsername); //lblUsername.setSize(300, 300); cp.add(txtUsername); cp.add(lblPassword); cp.add(txtPassword); cp.add(btnSubmit); cp.add(btnCancel); c.add(cp) ; pack(); setSize( 250, 150 ); //show(); } protected boolean submit(String username, String password) { if(username.length() < 1 || password.length() < 1) return false; return hashAndVerify(username, password); } private boolean hashAndVerify(String username, String password) { String query = "SELECT Password FROM userDB WHERE Username = ?"; PreparedStatement stmt = DatabaseConnection.getStatement(query); ResultSet rs = null; try { stmt.setString(1, username); rs = stmt.executeQuery(); while (rs.next()) { String arr = rs.getString(password); if (password.equals(arr)) { return true; } } } catch (SQLException e1) { e1.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); DatabaseConnection.closeConnection(); } catch (SQLException e) { e.printStackTrace(); } } return false; } /* private byte[] hash(String password) { MessageDigest sha1 = null; try { sha1 = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } if (sha1 == null) return null; sha1.update(password); return sha1.digest(); }*/ protected void close() { this.dispose(); } public static void NewsqlLogin() { //new NewsqlLogin(); } }