Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.52 KB | None | 0 0
  1. package View;
  2.  
  3. import Controller.Database_Connection;
  4. import java.awt.Toolkit;
  5. import java.awt.event.WindowEvent;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class Login extends javax.swing.JFrame
  9. {
  10. /*** Creates new form Login */
  11. public Login()
  12. {
  13. initComponents();
  14.  
  15. //static Connection Conn = null;
  16. //PreparedStatement Query = null;
  17. }
  18.  
  19. /**
  20. * This method is called from within the constructor to initialize the form.
  21. * WARNING: Do NOT modify this code. The content of this method is always
  22. * regenerated by the Form Editor.
  23. */
  24. @SuppressWarnings("unchecked")
  25. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  26. private void initComponents() {
  27.  
  28. jPanel1 = new javax.swing.JPanel();
  29. jLabel1 = new javax.swing.JLabel();
  30. jLabel2 = new javax.swing.JLabel();
  31. jLabel3 = new javax.swing.JLabel();
  32. form_username = new javax.swing.JTextField();
  33. form_password = new javax.swing.JPasswordField();
  34. btn_login_cancel = new javax.swing.JToggleButton();
  35. btn_login_enter = new javax.swing.JToggleButton();
  36.  
  37. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  38.  
  39. jLabel1.setText("LOGIN ");
  40.  
  41. jLabel2.setText("USERNAME: ");
  42.  
  43. jLabel3.setText("PASSWORD:");
  44.  
  45. btn_login_cancel.setText("CANCEL");
  46. btn_login_cancel.addActionListener(new java.awt.event.ActionListener() {
  47. public void actionPerformed(java.awt.event.ActionEvent evt) {
  48. btn_login_cancelActionPerformed(evt);
  49. }
  50. });
  51.  
  52. btn_login_enter.setText("ENTER");
  53. btn_login_enter.addActionListener(new java.awt.event.ActionListener() {
  54. public void actionPerformed(java.awt.event.ActionEvent evt) {
  55. btn_login_enterActionPerformed(evt);
  56. }
  57. });
  58.  
  59. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  60. jPanel1.setLayout(jPanel1Layout);
  61. jPanel1Layout.setHorizontalGroup(
  62. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  63. .addGroup(jPanel1Layout.createSequentialGroup()
  64. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  65. .addGroup(jPanel1Layout.createSequentialGroup()
  66. .addGap(200, 200, 200)
  67. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
  68. .addGroup(jPanel1Layout.createSequentialGroup()
  69. .addContainerGap()
  70. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  71. .addGroup(jPanel1Layout.createSequentialGroup()
  72. .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
  73. .addGap(18, 18, 18)
  74. .addComponent(form_username))
  75. .addGroup(jPanel1Layout.createSequentialGroup()
  76. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
  77. .addGap(18, 18, 18)
  78. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79. .addGroup(jPanel1Layout.createSequentialGroup()
  80. .addComponent(btn_login_cancel)
  81. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  82. .addComponent(btn_login_enter))
  83. .addComponent(form_password, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))))
  84. .addGap(84, 84, 84))
  85. );
  86. jPanel1Layout.setVerticalGroup(
  87. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  88. .addGroup(jPanel1Layout.createSequentialGroup()
  89. .addContainerGap()
  90. .addComponent(jLabel1)
  91. .addGap(11, 11, 11)
  92. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  93. .addComponent(jLabel2)
  94. .addComponent(form_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  95. .addGap(18, 18, 18)
  96. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  97. .addComponent(jLabel3)
  98. .addComponent(form_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  99. .addGap(18, 18, 18)
  100. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  101. .addComponent(btn_login_cancel)
  102. .addComponent(btn_login_enter))
  103. .addContainerGap(46, Short.MAX_VALUE))
  104. );
  105.  
  106. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  107. getContentPane().setLayout(layout);
  108. layout.setHorizontalGroup(
  109. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  110. .addGroup(layout.createSequentialGroup()
  111. .addContainerGap()
  112. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  113. .addContainerGap())
  114. );
  115. layout.setVerticalGroup(
  116. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  117. .addGroup(layout.createSequentialGroup()
  118. .addContainerGap()
  119. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  120. .addContainerGap())
  121. );
  122.  
  123. pack();
  124. }// </editor-fold>
  125.  
  126. private void btn_login_enterActionPerformed(java.awt.event.ActionEvent evt) {
  127. // TODO add your handling code here:
  128. try
  129. {
  130. //The code below is to capture the text input from the login frame input fields
  131. String username = form_username.getText();
  132. String password = form_password.getText();
  133.  
  134. //Flag to be used to validdate login credentials
  135. int correct_credentials;
  136.  
  137. //Make a connection to the database conncection class
  138. Database_Connection.ConnectDB();
  139.  
  140. //Perform the SQL Commands
  141. String Query1 = "SELECT Username,Password FROM Users WHERE Username ='"+username+"'AND Password ='"+password+"'";
  142.  
  143. PreparedStatement = connect.prepareStatement(Query1);
  144. }
  145. catch(Exception e)
  146. {
  147.  
  148. }
  149.  
  150. //Check if field equals to blank and throw error message
  151. if (username.equals("")|| password.equals(""))
  152. {
  153. JOptionPane.showMessageDialog(null," Name or Password is empty!! ","Error",JOptionPane.ERROR_MESSAGE);
  154. }
  155. else if (username.endsWith(Query1) && password.endsWith(Query1))
  156. {
  157. JOptionPane.showMessageDialog(null," ");
  158. }
  159. else
  160. {
  161. JOptionPane.showMessageDialog(null,"Your Login Credentials are Invalid!");
  162. }
  163.  
  164. }
  165.  
  166. private void btn_login_cancelActionPerformed(java.awt.event.ActionEvent evt) {
  167. // TODO add your handling code here:
  168. close();
  169. }
  170.  
  171. /**
  172. * @param args the command line arguments
  173. */
  174. public static void main(String args[])
  175. {
  176. //Conn = Sqlite_Database_Connection.ConnectDB();
  177. /* Set the Nimbus look and feel */
  178. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  179. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  180. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  181. */
  182. try {
  183. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  184. if ("Nimbus".equals(info.getName())) {
  185. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  186. break;
  187. }
  188. }
  189. } catch (ClassNotFoundException ex) {
  190. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  191. } catch (InstantiationException ex) {
  192. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  193. } catch (IllegalAccessException ex) {
  194. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  195. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  196. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  197. }
  198. //</editor-fold>
  199.  
  200. /* Create and display the form */
  201. java.awt.EventQueue.invokeLater(new Runnable()
  202. {
  203. @Override
  204. public void run()
  205. {
  206. new Login().setVisible(true);
  207. }
  208. });
  209. }
  210.  
  211. // Variables declaration - do not modify
  212. private javax.swing.JToggleButton btn_login_cancel;
  213. private javax.swing.JToggleButton btn_login_enter;
  214. private javax.swing.JPasswordField form_password;
  215. private javax.swing.JTextField form_username;
  216. private javax.swing.JLabel jLabel1;
  217. private javax.swing.JLabel jLabel2;
  218. private javax.swing.JLabel jLabel3;
  219. private javax.swing.JPanel jPanel1;
  220. // End of variables declaration
  221.  
  222. private void close()
  223. {
  224. //This is a statement to use to close the current window
  225. WindowEvent winClosing = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
  226. Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosing);
  227. }
  228.  
  229. }
  230.  
  231. package Controller;
  232.  
  233. import java.sql.*;
  234. import javax.swing.JOptionPane;
  235.  
  236. public class Database_Connection
  237. {
  238. private Connection connect;
  239.  
  240. public static Connection ConnectDB()
  241. {
  242. // The URL to the Java Derby Server
  243. String connectionURL = "jdbc:derby://localhost:1739`enter code here`/TEST_DB";
  244.  
  245.  
  246. try
  247. {
  248. Connection connect;
  249. Class.forName("org.apache.derby.jdbc.ClientDriver");
  250. connect = DriverManager.getConnection(connectionURL,"###","####");
  251. JOptionPane.showMessageDialog(null, "Database Connection Successful");
  252.  
  253. return connect;
  254. }
  255. catch(ClassNotFoundException | SQLException e)
  256. {
  257. JOptionPane.showMessageDialog(null, "Database Connection Not Successful");
  258. }
  259. return null;
  260. }
  261.  
  262. public Database_Connection() {
  263. this.connect = null;
  264. }
  265.  
  266. /**
  267. * @return the connect
  268. */
  269. public Connection getConnect()
  270. {
  271. return connect;
  272. }
  273.  
  274. /**
  275. * @param connect the connect to set
  276. */
  277. public void setConnect(Connection connect)
  278. {
  279. this.connect = connect;
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement