Advertisement
Guest User

Login App Java

a guest
Oct 30th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. LoginLogic.java
  2.  
  3. package sk.loginapp;
  4.  
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class LoginLogic {
  9.  
  10.     LoginUI lu;
  11.  
  12.     public LoginLogic() {
  13.         lu = new LoginUI();
  14.  
  15.     }
  16.  
  17.     public LoginLogic(ResultSet rs) {
  18.         process(rs);
  19.     }
  20.  
  21.     private void process(ResultSet rs) {
  22.         try {
  23.             if (rs.next()) {
  24.                 lu.loginSuccess();
  25.  
  26.             } else {
  27.                 lu.loginFailed();
  28.             }
  29.         } catch (SQLException e) {
  30.             // TODO: handle exception
  31.         }
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.  
  36.         new LoginLogic();
  37.  
  38.     }
  39.  
  40. }
  41.  
  42. LoginUI.java
  43.  
  44. package sk.loginapp;
  45.  
  46. import java.awt.event.ActionEvent;
  47. import java.awt.event.ActionListener;
  48.  
  49. import javax.swing.JButton;
  50. import javax.swing.JFrame;
  51. import javax.swing.JLabel;
  52. import javax.swing.JOptionPane;
  53. import javax.swing.JPanel;
  54. import javax.swing.JTextField;
  55. import javax.swing.UIManager;
  56. import javax.swing.UnsupportedLookAndFeelException;
  57.  
  58. public class LoginUI {
  59.  
  60.     // Container declarations
  61.     JFrame frame;
  62.     JPanel panel;
  63.    
  64.     // Component declarations
  65.     JLabel lblUname, lblPwd;
  66.     JTextField tfUname, tfPwd;
  67.     JButton btnLogin, btnCancel;
  68.    
  69.     public LoginUI() {
  70.         this.createFrame();
  71.     }
  72.  
  73.     private void createFrame() {
  74.  
  75.         // set native OS UI
  76.         try {
  77.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  78.         } catch (ClassNotFoundException | UnsupportedLookAndFeelException | IllegalAccessException
  79.                 | InstantiationException e) {
  80.  
  81.         }
  82.  
  83.         frame = new JFrame("Login App");
  84.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85.  
  86.         panel = new JPanel();
  87.         frame.add(panel);
  88.         this.placePanelComponents(panel);
  89.  
  90.         frame.setSize(300, 160);
  91.         frame.setResizable(false);
  92.         frame.setLocationRelativeTo(null);
  93.         frame.setVisible(true);
  94.  
  95.     }
  96.  
  97.     private void placePanelComponents(JPanel panel) {
  98.  
  99.         panel.setLayout(null);
  100.  
  101.         lblUname = new JLabel("Username: ");
  102.         lblUname.setBounds(50, 20, 120, 20);
  103.         lblPwd = new JLabel("Password: ");
  104.         lblPwd.setBounds(50, 50, 120, 20);
  105.  
  106.         tfUname = new JTextField(10);
  107.         tfUname.setBounds(120, 20, 120, 20);
  108.         tfPwd = new JTextField(10);
  109.         tfPwd.setBounds(120, 50, 120, 20);
  110.  
  111.         btnLogin = new JButton("Login");
  112.         btnLogin.setBounds(50, 85, 90, 25);
  113.         btnCancel = new JButton("Cancel");
  114.         btnCancel.setBounds(150, 85, 90, 25);
  115.  
  116.         panel.add(lblUname);
  117.         panel.add(lblPwd);
  118.         panel.add(tfUname);
  119.         panel.add(tfPwd);
  120.         panel.add(btnLogin);
  121.         panel.add(btnCancel);
  122.  
  123.         btnLogin.addActionListener(new ActionListener() {
  124.  
  125.             @Override
  126.             public void actionPerformed(ActionEvent e) {
  127.  
  128.                 try {
  129.                     String uname = tfUname.getText().trim();
  130.                     String pwd = tfPwd.getText().trim();
  131.                     new LoginDbConn(uname, pwd);//startDB after user presses the login button
  132.                 } catch (Exception ex) {
  133.                     // TODO: handle exception
  134.                 }
  135.  
  136.             }
  137.         });
  138.        
  139.         btnCancel.addActionListener(new ActionListener() {
  140.            
  141.             @Override
  142.             public void actionPerformed(ActionEvent e) {
  143.                 try {
  144.                     System.exit(0);
  145.                 } catch (Exception ex) {
  146.                     // TODO: handle exception
  147.                 }
  148.                
  149.             }
  150.         });
  151.  
  152.     }
  153.    
  154.     void loginSuccess() {
  155.         JOptionPane.showMessageDialog(null, "Login Successful!");
  156.     }
  157.    
  158.     void loginFailed() {
  159.         JOptionPane.showMessageDialog(null, "Login Failed!");
  160.     }
  161.  
  162. }
  163.  
  164.  
  165. LoginDBConn.java
  166.  
  167. package sk.loginapp;
  168.  
  169. import java.sql.Connection;
  170. import java.sql.DriverManager;
  171. import java.sql.PreparedStatement;
  172. import java.sql.ResultSet;
  173. import java.sql.SQLException;
  174. //import java.sql.Statement;
  175.  
  176. public class LoginDbConn {
  177.  
  178.     /*public LoginDbConn() {
  179.         // TODO Auto-generated constructor stub
  180.     }*/
  181.  
  182.     public LoginDbConn(String uname, String pwd) {
  183.         this.getConn(uname, pwd);
  184.     }
  185.  
  186.     private void getConn(String uname, String pwd) {
  187.         try {
  188.             // 0. Register the JDBC drivers
  189.             String driverClass = "oracle.jdbc.driver.OracleDriver";
  190.             Class.forName(driverClass);
  191.             // or DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  192.  
  193.             // 1. Get a connection to the Database
  194.             String dbUrl = "jdbc:oracle:thin:@localhost:1521:xe";
  195.             String dbuname = "scott";
  196.             String dbpwd = "tiger";
  197.             Connection conn = DriverManager.getConnection(dbUrl, dbuname, dbpwd);
  198.  
  199.             // 2. Create a statement
  200.             // String sql = "SELECT * FROM users WHERE name = '"+uname+"' and
  201.             // password = '"+pwd+"'";
  202.             // Statement st = conn.createStatement();
  203.             String sql = "select * from users where name = ? and password = ?";
  204.             PreparedStatement pst = conn.prepareStatement(sql);
  205.  
  206.             pst.setString(1, uname);
  207.             pst.setString(2, pwd);
  208.  
  209.             // 3. Execute SQL query
  210.             ResultSet rs = pst.executeQuery();
  211.  
  212.             // 4. Process the result set
  213.             new LoginLogic(rs);
  214.  
  215.             // 5. Close Connection
  216.             conn.close();
  217.         } catch (SQLException | ClassNotFoundException e) {
  218.  
  219.         }
  220.  
  221.     }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement