Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.58 KB | None | 0 0
  1. package cabsystem;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import javax.swing.JOptionPane;
  8.  
  9.  
  10. public class log extends javax.swing.JFrame {
  11.  
  12. /**
  13. * Creates new form log
  14. */
  15. public log(java.awt.Frame parent, boolean modal) {
  16. initComponents();
  17. }
  18.  
  19. log() {
  20. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  21. }
  22.  
  23. /**
  24. * This method is called from within the constructor to initialize the form.
  25. * WARNING: Do NOT modify this code. The content of this method is always
  26. * regenerated by the Form Editor.
  27. */
  28. @SuppressWarnings("unchecked")
  29. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  30. private void initComponents() {
  31.  
  32. jPanel1 = new javax.swing.JPanel();
  33. jLabel1 = new javax.swing.JLabel();
  34. jLabel2 = new javax.swing.JLabel();
  35. jLabel3 = new javax.swing.JLabel();
  36. un = new javax.swing.JTextField();
  37. pw = new javax.swing.JPasswordField();
  38. jButton1 = new javax.swing.JButton();
  39. jButton2 = new javax.swing.JButton();
  40.  
  41. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  42. setTitle("Login");
  43.  
  44. jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 24)); // NOI18N
  45. jLabel1.setText("Log In");
  46.  
  47. jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
  48. jLabel2.setText("Password");
  49.  
  50. jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
  51. jLabel3.setText("User Name");
  52.  
  53. pw.addActionListener(new java.awt.event.ActionListener() {
  54. public void actionPerformed(java.awt.event.ActionEvent evt) {
  55. pwActionPerformed(evt);
  56. }
  57. });
  58.  
  59. jButton1.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
  60. jButton1.setText("Cancel");
  61. jButton1.addActionListener(new java.awt.event.ActionListener() {
  62. public void actionPerformed(java.awt.event.ActionEvent evt) {
  63. jButton1ActionPerformed(evt);
  64. }
  65. });
  66.  
  67. jButton2.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
  68. jButton2.setText("Log In");
  69. jButton2.addActionListener(new java.awt.event.ActionListener() {
  70. public void actionPerformed(java.awt.event.ActionEvent evt) {
  71. jButton2ActionPerformed(evt);
  72. }
  73. });
  74.  
  75. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  76. jPanel1.setLayout(jPanel1Layout);
  77. jPanel1Layout.setHorizontalGroup(
  78. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  80. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  81. .addGroup(jPanel1Layout.createSequentialGroup()
  82. .addGap(37, 37, 37)
  83. .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
  84. .addGap(41, 41, 41)
  85. .addComponent(pw, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
  86. .addGroup(jPanel1Layout.createSequentialGroup()
  87. .addGap(0, 0, Short.MAX_VALUE)
  88. .addComponent(un, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))
  89. .addGroup(jPanel1Layout.createSequentialGroup()
  90. .addGap(85, 85, 85)
  91. .addComponent(jButton2)
  92. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  93. .addComponent(jButton1)
  94. .addGap(51, 51, 51)))
  95. .addGap(45, 45, 45))
  96. .addGroup(jPanel1Layout.createSequentialGroup()
  97. .addGap(159, 159, 159)
  98. .addComponent(jLabel1)
  99. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  100. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  101. .addGroup(jPanel1Layout.createSequentialGroup()
  102. .addGap(41, 41, 41)
  103. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
  104. .addContainerGap(236, Short.MAX_VALUE)))
  105. );
  106. jPanel1Layout.setVerticalGroup(
  107. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  108. .addGroup(jPanel1Layout.createSequentialGroup()
  109. .addGap(26, 26, 26)
  110. .addComponent(jLabel1)
  111. .addGap(48, 48, 48)
  112. .addComponent(un, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
  113. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  114. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  115. .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  116. .addComponent(pw, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
  117. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
  118. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  119. .addComponent(jButton1)
  120. .addComponent(jButton2))
  121. .addGap(37, 37, 37))
  122. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  123. .addGroup(jPanel1Layout.createSequentialGroup()
  124. .addGap(101, 101, 101)
  125. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  126. .addContainerGap(159, Short.MAX_VALUE)))
  127. );
  128.  
  129. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  130. getContentPane().setLayout(layout);
  131. layout.setHorizontalGroup(
  132. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  133. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  134. );
  135. layout.setVerticalGroup(
  136. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  137. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  138. );
  139.  
  140. pack();
  141. }// </editor-fold>
  142.  
  143. private void pwActionPerformed(java.awt.event.ActionEvent evt) {
  144. // TODO add your handling code here:
  145. }
  146.  
  147. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  148. // TODO add your handling code here:
  149. }
  150.  
  151. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  152.  
  153. ConnectDB c =new ConnectDB();
  154. ResultSet rs;
  155. String sql;
  156. String name;
  157. String password;
  158. String pword = null;
  159. String tp = null;
  160. name=un.getText();
  161. password=pw.getText();
  162. sql = "SELECT * FROM USER WHERE UNAME IS '"+name+"';";
  163. rs = c.conne(sql);
  164. try {
  165. while(rs.next()){
  166. name = rs.getString("UNAME");
  167. pword = rs.getString("PWORD");
  168. tp = rs.getString("UTYPE");
  169. }
  170. } catch (SQLException ex) {
  171. Logger.getLogger(log.class.getName()).log(Level.SEVERE, null, ex);}
  172. if(password.equals(pword)){
  173. if(tp.equals("admin")){
  174. new mpage().setVisible(true);
  175. this.setVisible(false);
  176. }
  177. else{
  178. new umpage().setVisible(true);
  179. this.setVisible(false);
  180. }
  181. }
  182. else{
  183. JOptionPane.showMessageDialog(null, "Invalid login details");
  184. }
  185.  
  186.  
  187.  
  188. }
  189.  
  190. /**
  191. * @param args the command line arguments
  192. */
  193. public static void main(String args[]) {
  194. /* Set the Nimbus look and feel */
  195. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  196. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  197. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  198. */
  199. try {
  200. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  201. if ("Nimbus".equals(info.getName())) {
  202. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  203. break;package cabsystem;
  204.  
  205. import java.sql.ResultSet;
  206. import java.sql.SQLException;
  207. import java.util.logging.Level;
  208. import java.util.logging.Logger;
  209. import javax.swing.JOptionPane;
  210.  
  211.  
  212. public class log extends javax.swing.JFrame {
  213.  
  214. /**
  215. * Creates new form log
  216. */
  217. public log(java.awt.Frame parent, boolean modal) {
  218. initComponents();
  219. }
  220.  
  221. log() {
  222. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  223. }
  224.  
  225. /**
  226. * This method is called from within the constructor to initialize the form.
  227. * WARNING: Do NOT modify this code. The content of this method is always
  228. * regenerated by the Form Editor.
  229. */
  230. @SuppressWarnings("unchecked")
  231. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  232. private void initComponents() {
  233.  
  234. jPanel1 = new javax.swing.JPanel();
  235. jLabel1 = new javax.swing.JLabel();
  236. jLabel2 = new javax.swing.JLabel();
  237. jLabel3 = new javax.swing.JLabel();
  238. un = new javax.swing.JTextField();
  239. pw = new javax.swing.JPasswordField();
  240. jButton1 = new javax.swing.JButton();
  241. jButton2 = new javax.swing.JButton();
  242.  
  243. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  244. setTitle("Login");
  245.  
  246. jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 24)); // NOI18N
  247. jLabel1.setText("Log In");
  248.  
  249. jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
  250. jLabel2.setText("Password");
  251.  
  252. jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
  253. jLabel3.setText("User Name");
  254.  
  255. pw.addActionListener(new java.awt.event.ActionListener() {
  256. public void actionPerformed(java.awt.event.ActionEvent evt) {
  257. pwActionPerformed(evt);
  258. }
  259. });
  260.  
  261. jButton1.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
  262. jButton1.setText("Cancel");
  263. jButton1.addActionListener(new java.awt.event.ActionListener() {
  264. public void actionPerformed(java.awt.event.ActionEvent evt) {
  265. jButton1ActionPerformed(evt);
  266. }
  267. });
  268.  
  269. jButton2.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
  270. jButton2.setText("Log In");
  271. jButton2.addActionListener(new java.awt.event.ActionListener() {
  272. public void actionPerformed(java.awt.event.ActionEvent evt) {
  273. jButton2ActionPerformed(evt);
  274. }
  275. });
  276.  
  277. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  278. jPanel1.setLayout(jPanel1Layout);
  279. jPanel1Layout.setHorizontalGroup(
  280. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  281. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  282. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  283. .addGroup(jPanel1Layout.createSequentialGroup()
  284. .addGap(37, 37, 37)
  285. .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
  286. .addGap(41, 41, 41)
  287. .addComponent(pw, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
  288. .addGroup(jPanel1Layout.createSequentialGroup()
  289. .addGap(0, 0, Short.MAX_VALUE)
  290. .addComponent(un, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))
  291. .addGroup(jPanel1Layout.createSequentialGroup()
  292. .addGap(85, 85, 85)
  293. .addComponent(jButton2)
  294. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  295. .addComponent(jButton1)
  296. .addGap(51, 51, 51)))
  297. .addGap(45, 45, 45))
  298. .addGroup(jPanel1Layout.createSequentialGroup()
  299. .addGap(159, 159, 159)
  300. .addComponent(jLabel1)
  301. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  302. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  303. .addGroup(jPanel1Layout.createSequentialGroup()
  304. .addGap(41, 41, 41)
  305. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
  306. .addContainerGap(236, Short.MAX_VALUE)))
  307. );
  308. jPanel1Layout.setVerticalGroup(
  309. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  310. .addGroup(jPanel1Layout.createSequentialGroup()
  311. .addGap(26, 26, 26)
  312. .addComponent(jLabel1)
  313. .addGap(48, 48, 48)
  314. .addComponent(un, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
  315. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  316. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  317. .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  318. .addComponent(pw, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
  319. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
  320. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  321. .addComponent(jButton1)
  322. .addComponent(jButton2))
  323. .addGap(37, 37, 37))
  324. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  325. .addGroup(jPanel1Layout.createSequentialGroup()
  326. .addGap(101, 101, 101)
  327. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  328. .addContainerGap(159, Short.MAX_VALUE)))
  329. );
  330.  
  331. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  332. getContentPane().setLayout(layout);
  333. layout.setHorizontalGroup(
  334. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  335. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  336. );
  337. layout.setVerticalGroup(
  338. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  339. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  340. );
  341.  
  342. pack();
  343. }// </editor-fold>
  344.  
  345. private void pwActionPerformed(java.awt.event.ActionEvent evt) {
  346. // TODO add your handling code here:
  347. }
  348.  
  349. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  350. // TODO add your handling code here:
  351. }
  352.  
  353. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  354.  
  355. ConnectDB c =new ConnectDB();
  356. ResultSet rs;
  357. String sql;
  358. String name;
  359. String password;
  360. String pword = null;
  361. String tp = null;
  362. name=un.getText();
  363. password=pw.getText();
  364. sql = "SELECT * FROM USER WHERE UNAME IS '"+name+"';";
  365. rs = c.conne(sql);
  366. try {
  367. while(rs.next()){
  368. name = rs.getString("UNAME");
  369. pword = rs.getString("PWORD");
  370. tp = rs.getString("UTYPE");
  371. }
  372. } catch (SQLException ex) {
  373. Logger.getLogger(log.class.getName()).log(Level.SEVERE, null, ex);}
  374. if(password.equals(pword)){
  375. if(tp.equals("admin")){
  376. new mpage().setVisible(true);
  377. this.setVisible(false);
  378. }
  379. else{
  380. new umpage().setVisible(true);
  381. this.setVisible(false);
  382. }
  383. }
  384. else{
  385. JOptionPane.showMessageDialog(null, "Invalid login details");
  386. }
  387.  
  388.  
  389.  
  390. }
  391.  
  392. /**
  393. * @param args the command line arguments
  394. */
  395. public static void main(String args[]) {
  396. /* Set the Nimbus look and feel */
  397. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  398. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  399. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  400. */
  401. try {
  402. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  403. if ("Nimbus".equals(info.getName())) {
  404. javax.swing.UIManager.set
  405. }
  406. }
  407. } catch (ClassNotFoundException ex) {
  408. java.util.logging.Logger.getLogger(log.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  409. } catch (InstantiationException ex) {
  410. java.util.logging.Logger.getLogger(log.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  411. } catch (IllegalAccessException ex) {
  412. java.util.logging.Logger.getLogger(log.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  413. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  414. java.util.logging.Logger.getLogger(log.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  415. }
  416. //</editor-fold>
  417.  
  418. /* Create and display the form */
  419. java.awt.EventQueue.invokeLater(new Runnable() {
  420. public void run() {
  421. new log().setVisible(true);
  422. }
  423. });
  424. }
  425.  
  426. // Variables declaration - do not modify
  427. private javax.swing.JButton jButton1;
  428. private javax.swing.JButton jButton2;
  429. private javax.swing.JLabel jLabel1;
  430. private javax.swing.JLabel jLabel2;
  431. private javax.swing.JLabel jLabel3;
  432. private javax.swing.JPanel jPanel1;
  433. private javax.swing.JPasswordField pw;
  434. private javax.swing.JTextField un;
  435. // End of variables declaration
  436. }
  437.  
  438. package cabsystem;
  439.  
  440.  
  441. import java.sql.Connection;
  442. import java.sql.DriverManager;
  443. import java.sql.ResultSet;
  444. import java.sql.SQLException;
  445. import java.sql.Statement;
  446. import java.util.logging.Level;
  447. import java.util.logging.Logger;
  448.  
  449.  
  450.  
  451. public class ConnectDB {
  452.  
  453.  
  454. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  455. static final String DB_URL = "jdbc:mysql://localhost:3306/cabsystem";
  456.  
  457. static final String USER = "root";
  458. static final String PASS = "";
  459.  
  460. static Connection conn = null;
  461. static Statement stmt = null;
  462. static ResultSet rs;
  463. /**
  464. *
  465. * @param sql
  466. * @return
  467. */
  468. public static ResultSet conne(String sql) {
  469. try {
  470. Class.forName("com.mysql.jdbc.Driver");
  471. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  472. stmt = conn.createStatement();
  473. rs = stmt.executeQuery(sql);
  474.  
  475.  
  476. } catch (ClassNotFoundException | SQLException ex) {
  477. Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
  478. }
  479. return rs;
  480.  
  481. }
  482. public static void connu(String sql) {
  483. try {
  484. Class.forName("com.mysql.jdbc.Driver");
  485. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  486. stmt = conn.createStatement();
  487. stmt.executeUpdate(sql);
  488.  
  489.  
  490. } catch (ClassNotFoundException | SQLException ex) {
  491. Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
  492. }
  493. }
  494.  
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement