Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package security;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- import javax.swing.*;
- public class JavaDB extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- @SuppressWarnings("unused")
- private static Statement stmt;
- private static Connection connect;
- public static String dbUrl = "jdbc:derby:\KeithDB;create=false";
- @SuppressWarnings("unused")
- private static String tableName = "userPasswordTable";
- private static JLabel welcomeLabel;
- private static JTextField usernameField;
- private static JPasswordField passwordField;
- private static JLabel usernameLabel;
- private static JLabel passwordLabel;
- private static JButton submit;
- @SuppressWarnings({ "unused", "deprecation" })
- public static void main(String args[]) throws SQLException{
- createConnection();
- boolean firstTime = firstTime();
- if(firstTime = true) {
- JavaDB db = new JavaDB("");
- db.createAccount();
- connect = DriverManager.getConnection("jdbc:derby:\KeithDB;shutdown=true");
- try {
- Thread th = new Thread();
- th.start();
- th.wait();
- th.stop();
- } catch (InterruptedException e1) {
- System.out.print("");
- }
- catch(IllegalMonitorStateException e3) {
- }
- }
- if(firstTime = false) {
- UserInput();
- }
- JavaDB window = new JavaDB("");
- window.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- }
- public JavaDB(String str){
- super("Please type in your username and password");
- Container c = getContentPane();
- c.setLayout(new FlowLayout());
- welcomeLabel = new JLabel("Please type in your username and password:n");
- c.add(welcomeLabel);
- usernameLabel = new JLabel("Username:");
- c.add(usernameLabel);
- usernameField = new JTextField(20);
- c.add(usernameField);
- passwordLabel = new JLabel("nPassword:");
- c.add(passwordLabel);
- passwordField = new JPasswordField(20);
- c.add(passwordField);
- submit = new JButton("Submit");
- c.add(submit);
- setSize(325,200);
- setVisible(false);
- }
- public JavaDB(){
- super("Please type in your username and password");
- Container c = getContentPane();
- c.setLayout(new FlowLayout());
- welcomeLabel = new JLabel("Please type in your username and password:n");
- c.add(welcomeLabel);
- usernameLabel = new JLabel("Username:");
- c.add(usernameLabel);
- usernameField = new JTextField(20);
- c.add(usernameField);
- passwordLabel = new JLabel("nPassword:");
- c.add(passwordLabel);
- passwordField = new JPasswordField(20);
- c.add(passwordField);
- submit = new JButton("Submit");
- c.add(submit);
- setSize(325,200);
- setVisible(true);
- }
- private static void createConnection()
- {
- try
- {
- Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
- //Get a connection
- connect = DriverManager.getConnection(dbUrl);
- }
- catch (Exception except)
- {
- except.printStackTrace();
- }
- }
- public static boolean firstTime() {
- int userBoolean =JOptionPane.showConfirmDialog(null, "Do you want to make a new" +
- " account?","Create new account?",JOptionPane.YES_NO_OPTION);
- if(userBoolean == JOptionPane.YES_OPTION) {
- return true;
- }
- else {
- return false;
- }
- }
- @SuppressWarnings("unused")
- public static void UserInput(){
- try
- {
- stmt = connect.createStatement();
- JavaDB jdb = new JavaDB();
- }
- catch (SQLException sqlExcept)
- {
- sqlExcept.printStackTrace();
- }
- }
- public void createAccount() {
- NewAccount account = new NewAccount();
- account.action();
- }
- }
- package security;
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.*;
- import java.sql.*;
- public class NewAccount extends JFrame {
- private static final long serialVersionUID = 1L;
- private static JLabel userName;
- private static JLabel passWord;
- private static JLabel email;
- private static JButton submit;
- private static JTextField newUsername;
- private static JTextField newPassword, newEmail;
- private static Statement stmnt = null;
- private static Connection connect;
- @SuppressWarnings("unused")
- private static String error = "Error. Passwords don't match";
- private static void createConnection() {
- try {
- Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
- //Get a connection
- connect = DriverManager.getConnection(JavaDB.dbUrl);
- } catch (Exception except) {
- except.printStackTrace();
- }
- }
- public NewAccount() {
- super("New Account");
- }
- public void frame() {
- Container c = getContentPane();
- c.setLayout(new FlowLayout());
- userName = new JLabel("Username:");
- c.add(userName);
- newUsername = new JTextField(20);
- c.add(newUsername);
- passWord = new JLabel("nPassword:");
- c.add(passWord);
- newPassword = new JTextField(20);
- c.add(newPassword);
- email = new JLabel(" nConfirm:");
- c.add(email);
- newEmail = new JTextField(20);
- c.add(newEmail);
- submit = new JButton("Submit");
- c.add(submit);
- this.setLocationRelativeTo(getRootPane());
- setSize(325, 150);
- setVisible(true);
- }
- public void insert() throws SQLException {
- createConnection();
- stmnt = connect.createStatement();
- stmnt.execute("Insert into table userPasswordTable values('" + newUsername
- + "','" + newPassword + "')");
- JOptionPane.showMessageDialog(null, "Thank you. "
- + "Please restart the program");
- System.exit(0);
- }
- public void action() {
- frame();
- NewAccount window = new NewAccount();
- window.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- submit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (e.getSource().equals(submit)) {
- try {
- insert();
- } catch (SQLException e1) {
- }
- }
- }
- });
- }
- }
- try {
- insert();
- } catch (SQLException e1) {
- }
- try {
- insert();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- if(firstTime = true)
Add Comment
Please, Sign In to add comment