Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.border.EmptyBorder;
  5.  
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. public class Login extends JFrame {
  11.  
  12. private JPanel p;
  13. private JLabel l;
  14. private JLabel l1;
  15. private JTextField t;
  16. private JTextField t1;
  17. private JButton loginB;
  18. private JButton registerB;
  19. private JButton quitB;
  20.  
  21. private Connection conn;
  22. private Statement st;
  23. private ResultSet rs;
  24.  
  25. public Login() {
  26. super("Login");
  27.  
  28. setSize(483, 212);
  29. setLocationRelativeTo(null);
  30. setResizable(false);
  31. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.  
  33. connect();
  34.  
  35. buildPanel();
  36. add(p);
  37.  
  38. setVisible(true);
  39.  
  40. } // end of constructor
  41.  
  42. private void connect() {
  43.  
  44. try {
  45. final String DB_URL = "jdbc:odbc:FinalDB1";
  46. conn = DriverManager.getConnection(DB_URL);
  47. st = conn.createStatement();
  48.  
  49. } catch (Exception e) {
  50. System.out.println(e.getMessage());
  51. }
  52.  
  53. }
  54.  
  55. private void buildPanel() {
  56.  
  57. p = new JPanel();
  58. p.setBorder(new EmptyBorder(5, 5, 5, 5));
  59. p.setLayout(null);
  60.  
  61. l = new JLabel("Username");
  62. l.setFont(new Font("Tahoma", Font.PLAIN, 14));
  63. l.setBounds(72, 23, 78, 30);
  64. p.add(l);
  65.  
  66. l1 = new JLabel("Password");
  67. l1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  68. l1.setBounds(72, 68, 78, 30);
  69. p.add(l1);
  70.  
  71. t = new JTextField(10);
  72. t.setBounds(198, 28, 137, 24);
  73. p.add(t);
  74.  
  75. t1 = new JTextField(10);
  76. t1.setBounds(198, 73, 137, 24);
  77. p.add(t1);
  78.  
  79. loginB = new JButton("Login");
  80. loginB.addActionListener(new LoginB());
  81. loginB.setBounds(54, 121, 89, 23);
  82. p.add(loginB);
  83.  
  84. registerB = new JButton("Register");
  85. registerB.addActionListener(new RegisterB());
  86. registerB.setBounds(199, 121, 89, 23);
  87. p.add(registerB);
  88.  
  89. quitB = new JButton("Quit");
  90. quitB.addActionListener(new QuitB());
  91. quitB.setBounds(337, 121, 89, 23);
  92. p.add(quitB);
  93.  
  94. }// end of BuildPanel method
  95.  
  96. private class LoginB implements ActionListener {
  97.  
  98. public void actionPerformed(ActionEvent e) {
  99. try {
  100. String user = t.getText().trim();
  101. String pass = t1.getText().trim();
  102. String sql = "SELECT Username, Password, AccountType FROM Accounts WHERE Username = '"
  103. + user + "' and Password = '" + pass + "' ";
  104. rs = st.executeQuery(sql);
  105.  
  106. int count = 0;
  107. while (rs.next()) {
  108. count = count + 1;
  109. }
  110.  
  111. if (count == 1) {
  112.  
  113. sql = "SELECT * FROM Accounts WHERE Username = '" + user
  114. + "' ";
  115. rs = st.executeQuery(sql);
  116.  
  117. String accountType = null;
  118. String fname = null;
  119. String lname = null;
  120. int id = 0;
  121.  
  122. while (rs.next()) {
  123. accountType = rs.getString("AccountType");
  124. fname = rs.getString("FirstName");
  125. lname = rs.getString("LastName");
  126. id = rs.getInt("ID");
  127. }
  128.  
  129. int accType = Integer.parseInt(accountType);
  130.  
  131. if (accType == 0) {
  132. new StudentMenu(fname, lname, id); // opens the menu
  133. // for students
  134.  
  135. conn.close();
  136. dispose();
  137.  
  138. } else if (accType == 1) {
  139. new ProfessorMenu(fname, lname, id); // opens the menu
  140. // for professors
  141. conn.close();
  142. dispose();
  143.  
  144. } else {
  145. JOptionPane.showMessageDialog(null,
  146. "Something went wrong!");
  147. }
  148.  
  149. // JOptionPane.showMessageDialog(null, "Access Granted"); //
  150. // NO
  151. // NEED
  152. // MORE
  153. // THIS
  154. // AFTER
  155. // MENU
  156. // IS
  157. // CREATED
  158. } else if (count == 0) {
  159. JOptionPane.showMessageDialog(null,
  160. "Access Denied, Wrong Username and/or Password).");
  161. } else {
  162. JOptionPane.showMessageDialog(null, "Something went wrong");
  163. }
  164.  
  165. //conn.close();
  166.  
  167. } catch (Exception ex) {
  168. System.out.println(ex.getMessage());
  169. }
  170. }
  171. } // end of inner class
  172.  
  173. private class RegisterB implements ActionListener {
  174.  
  175. public void actionPerformed(ActionEvent e) {
  176.  
  177. try {
  178. conn.close();
  179. } catch (SQLException e1) {
  180. // TODO Auto-generated catch block
  181. e1.printStackTrace();
  182. }
  183. new Register();
  184. dispose();
  185. }
  186. } // end of inner class
  187.  
  188. private class QuitB implements ActionListener {
  189.  
  190. public void actionPerformed(ActionEvent e) {
  191. try {
  192. conn.close();
  193. } catch (SQLException e1) {
  194. // TODO Auto-generated catch block
  195. e1.printStackTrace();
  196. }
  197. System.exit(0);
  198. }
  199. } // end of inner class
  200.  
  201. } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement