Guest User

Untitled

a guest
Jul 25th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. package security;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. import javax.swing.*;
  6.  
  7. public class JavaDB extends JFrame {
  8.  
  9. /**
  10. *
  11. */
  12. private static final long serialVersionUID = 1L;
  13. @SuppressWarnings("unused")
  14. private static Statement stmt;
  15. private static Connection connect;
  16. public static String dbUrl = "jdbc:derby:\KeithDB;create=false";
  17. @SuppressWarnings("unused")
  18. private static String tableName = "userPasswordTable";
  19. private static JLabel welcomeLabel;
  20. private static JTextField usernameField;
  21. private static JPasswordField passwordField;
  22. private static JLabel usernameLabel;
  23. private static JLabel passwordLabel;
  24. private static JButton submit;
  25. @SuppressWarnings({ "unused", "deprecation" })
  26. public static void main(String args[]) throws SQLException{
  27. createConnection();
  28. boolean firstTime = firstTime();
  29. if(firstTime = true) {
  30. JavaDB db = new JavaDB("");
  31. db.createAccount();
  32. connect = DriverManager.getConnection("jdbc:derby:\KeithDB;shutdown=true");
  33. try {
  34. Thread th = new Thread();
  35. th.start();
  36. th.wait();
  37. th.stop();
  38. } catch (InterruptedException e1) {
  39. System.out.print("");
  40. }
  41. catch(IllegalMonitorStateException e3) {
  42.  
  43. }
  44. }
  45. if(firstTime = false) {
  46. UserInput();
  47. }
  48. JavaDB window = new JavaDB("");
  49. window.addWindowListener(new WindowAdapter(){
  50. public void windowClosing(WindowEvent e){
  51. System.exit(0);
  52. }
  53. });
  54. }
  55. public JavaDB(String str){
  56. super("Please type in your username and password");
  57. Container c = getContentPane();
  58. c.setLayout(new FlowLayout());
  59. welcomeLabel = new JLabel("Please type in your username and password:n");
  60. c.add(welcomeLabel);
  61. usernameLabel = new JLabel("Username:");
  62. c.add(usernameLabel);
  63. usernameField = new JTextField(20);
  64. c.add(usernameField);
  65. passwordLabel = new JLabel("nPassword:");
  66. c.add(passwordLabel);
  67. passwordField = new JPasswordField(20);
  68. c.add(passwordField);
  69. submit = new JButton("Submit");
  70. c.add(submit);
  71. setSize(325,200);
  72. setVisible(false);
  73. }
  74. public JavaDB(){
  75. super("Please type in your username and password");
  76. Container c = getContentPane();
  77. c.setLayout(new FlowLayout());
  78. welcomeLabel = new JLabel("Please type in your username and password:n");
  79. c.add(welcomeLabel);
  80. usernameLabel = new JLabel("Username:");
  81. c.add(usernameLabel);
  82. usernameField = new JTextField(20);
  83. c.add(usernameField);
  84. passwordLabel = new JLabel("nPassword:");
  85. c.add(passwordLabel);
  86. passwordField = new JPasswordField(20);
  87. c.add(passwordField);
  88. submit = new JButton("Submit");
  89. c.add(submit);
  90. setSize(325,200);
  91. setVisible(true);
  92. }
  93. private static void createConnection()
  94. {
  95. try
  96. {
  97. Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
  98. //Get a connection
  99. connect = DriverManager.getConnection(dbUrl);
  100. }
  101. catch (Exception except)
  102. {
  103. except.printStackTrace();
  104. }
  105. }
  106. public static boolean firstTime() {
  107. int userBoolean =JOptionPane.showConfirmDialog(null, "Do you want to make a new" +
  108. " account?","Create new account?",JOptionPane.YES_NO_OPTION);
  109. if(userBoolean == JOptionPane.YES_OPTION) {
  110. return true;
  111. }
  112. else {
  113. return false;
  114. }
  115.  
  116. }
  117. @SuppressWarnings("unused")
  118. public static void UserInput(){
  119. try
  120. {
  121. stmt = connect.createStatement();
  122. JavaDB jdb = new JavaDB();
  123.  
  124. }
  125. catch (SQLException sqlExcept)
  126. {
  127. sqlExcept.printStackTrace();
  128. }
  129.  
  130. }
  131.  
  132.  
  133.  
  134. public void createAccount() {
  135. NewAccount account = new NewAccount();
  136. account.action();
  137. }
  138. }
  139.  
  140. package security;
  141.  
  142. import java.awt.event.*;
  143. import javax.swing.*;
  144.  
  145. import java.awt.*;
  146. import java.sql.*;
  147.  
  148. public class NewAccount extends JFrame {
  149.  
  150. private static final long serialVersionUID = 1L;
  151. private static JLabel userName;
  152. private static JLabel passWord;
  153. private static JLabel email;
  154. private static JButton submit;
  155. private static JTextField newUsername;
  156. private static JTextField newPassword, newEmail;
  157. private static Statement stmnt = null;
  158. private static Connection connect;
  159. @SuppressWarnings("unused")
  160. private static String error = "Error. Passwords don't match";
  161.  
  162. private static void createConnection() {
  163. try {
  164. Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
  165. //Get a connection
  166. connect = DriverManager.getConnection(JavaDB.dbUrl);
  167. } catch (Exception except) {
  168. except.printStackTrace();
  169. }
  170. }
  171.  
  172. public NewAccount() {
  173. super("New Account");
  174.  
  175. }
  176.  
  177. public void frame() {
  178. Container c = getContentPane();
  179. c.setLayout(new FlowLayout());
  180. userName = new JLabel("Username:");
  181. c.add(userName);
  182. newUsername = new JTextField(20);
  183. c.add(newUsername);
  184. passWord = new JLabel("nPassword:");
  185. c.add(passWord);
  186. newPassword = new JTextField(20);
  187. c.add(newPassword);
  188. email = new JLabel(" nConfirm:");
  189. c.add(email);
  190. newEmail = new JTextField(20);
  191. c.add(newEmail);
  192.  
  193. submit = new JButton("Submit");
  194. c.add(submit);
  195.  
  196. this.setLocationRelativeTo(getRootPane());
  197. setSize(325, 150);
  198. setVisible(true);
  199. }
  200.  
  201. public void insert() throws SQLException {
  202. createConnection();
  203. stmnt = connect.createStatement();
  204. stmnt.execute("Insert into table userPasswordTable values('" + newUsername
  205. + "','" + newPassword + "')");
  206. JOptionPane.showMessageDialog(null, "Thank you. "
  207. + "Please restart the program");
  208. System.exit(0);
  209. }
  210.  
  211. public void action() {
  212.  
  213. frame();
  214. NewAccount window = new NewAccount();
  215. window.addWindowListener(new WindowAdapter() {
  216.  
  217. public void windowClosing(WindowEvent e) {
  218. System.exit(0);
  219. }
  220. });
  221. submit.addActionListener(new ActionListener() {
  222.  
  223. public void actionPerformed(ActionEvent e) {
  224. if (e.getSource().equals(submit)) {
  225. try {
  226. insert();
  227. } catch (SQLException e1) {
  228. }
  229. }
  230. }
  231. });
  232. }
  233. }
  234.  
  235. try {
  236. insert();
  237. } catch (SQLException e1) {
  238. }
  239.  
  240. try {
  241. insert();
  242. } catch (SQLException e1) {
  243. e1.printStackTrace();
  244. }
  245.  
  246. if(firstTime = true)
Add Comment
Please, Sign In to add comment