Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LoginLogic.java
- package sk.loginapp;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class LoginLogic {
- LoginUI lu;
- public LoginLogic() {
- lu = new LoginUI();
- }
- public LoginLogic(ResultSet rs) {
- process(rs);
- }
- private void process(ResultSet rs) {
- try {
- if (rs.next()) {
- lu.loginSuccess();
- } else {
- lu.loginFailed();
- }
- } catch (SQLException e) {
- // TODO: handle exception
- }
- }
- public static void main(String[] args) {
- new LoginLogic();
- }
- }
- LoginUI.java
- package sk.loginapp;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- public class LoginUI {
- // Container declarations
- JFrame frame;
- JPanel panel;
- // Component declarations
- JLabel lblUname, lblPwd;
- JTextField tfUname, tfPwd;
- JButton btnLogin, btnCancel;
- public LoginUI() {
- this.createFrame();
- }
- private void createFrame() {
- // set native OS UI
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException | UnsupportedLookAndFeelException | IllegalAccessException
- | InstantiationException e) {
- }
- frame = new JFrame("Login App");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- panel = new JPanel();
- frame.add(panel);
- this.placePanelComponents(panel);
- frame.setSize(300, 160);
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- private void placePanelComponents(JPanel panel) {
- panel.setLayout(null);
- lblUname = new JLabel("Username: ");
- lblUname.setBounds(50, 20, 120, 20);
- lblPwd = new JLabel("Password: ");
- lblPwd.setBounds(50, 50, 120, 20);
- tfUname = new JTextField(10);
- tfUname.setBounds(120, 20, 120, 20);
- tfPwd = new JTextField(10);
- tfPwd.setBounds(120, 50, 120, 20);
- btnLogin = new JButton("Login");
- btnLogin.setBounds(50, 85, 90, 25);
- btnCancel = new JButton("Cancel");
- btnCancel.setBounds(150, 85, 90, 25);
- panel.add(lblUname);
- panel.add(lblPwd);
- panel.add(tfUname);
- panel.add(tfPwd);
- panel.add(btnLogin);
- panel.add(btnCancel);
- btnLogin.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- String uname = tfUname.getText().trim();
- String pwd = tfPwd.getText().trim();
- new LoginDbConn(uname, pwd);//startDB after user presses the login button
- } catch (Exception ex) {
- // TODO: handle exception
- }
- }
- });
- btnCancel.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- System.exit(0);
- } catch (Exception ex) {
- // TODO: handle exception
- }
- }
- });
- }
- void loginSuccess() {
- JOptionPane.showMessageDialog(null, "Login Successful!");
- }
- void loginFailed() {
- JOptionPane.showMessageDialog(null, "Login Failed!");
- }
- }
- LoginDBConn.java
- package sk.loginapp;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- //import java.sql.Statement;
- public class LoginDbConn {
- /*public LoginDbConn() {
- // TODO Auto-generated constructor stub
- }*/
- public LoginDbConn(String uname, String pwd) {
- this.getConn(uname, pwd);
- }
- private void getConn(String uname, String pwd) {
- try {
- // 0. Register the JDBC drivers
- String driverClass = "oracle.jdbc.driver.OracleDriver";
- Class.forName(driverClass);
- // or DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
- // 1. Get a connection to the Database
- String dbUrl = "jdbc:oracle:thin:@localhost:1521:xe";
- String dbuname = "scott";
- String dbpwd = "tiger";
- Connection conn = DriverManager.getConnection(dbUrl, dbuname, dbpwd);
- // 2. Create a statement
- // String sql = "SELECT * FROM users WHERE name = '"+uname+"' and
- // password = '"+pwd+"'";
- // Statement st = conn.createStatement();
- String sql = "select * from users where name = ? and password = ?";
- PreparedStatement pst = conn.prepareStatement(sql);
- pst.setString(1, uname);
- pst.setString(2, pwd);
- // 3. Execute SQL query
- ResultSet rs = pst.executeQuery();
- // 4. Process the result set
- new LoginLogic(rs);
- // 5. Close Connection
- conn.close();
- } catch (SQLException | ClassNotFoundException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement