/*
* Test_Login.java
*/
package basics;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Test_Login extends JFrame {
public Test_Login() {
setTitle("Test");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,300);
setLocationRelativeTo(null);
LoginManager lg = new LoginManager(this,true);
lg.setVisible(true);
setVisible(true);
username = lg.getUsername();
setTitle( getTitle() + " '" + username + "'" );
}
public static void main(String args[]) {
new Test_Login();
}
private String username;
}
/*
* LoginManager
*/
class LoginManager extends JDialog {
public LoginManager(Frame parent, boolean modal) {
super(parent, modal);
setTitle("Login");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setSize(260,190);
setLocationRelativeTo(null);
loginPanel = new JPanel();
nameL = new JLabel();
nameField = new JTextField();
pwdL = new JLabel();
passwordField = new JPasswordField();
enterB = new JButton();
exitB = new JButton();
loginPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
nameL.setFont(new Font("Microsoft Sans Serif", 0, 14));
nameL.setText("Login Name:");
nameL.setPreferredSize(new Dimension(85, 20));
loginPanel.add(nameL);
nameField.setPreferredSize(new Dimension(100, 25));
loginPanel.add(nameField);
pwdL.setFont(new Font("Microsoft Sans Serif", 0, 14));
pwdL.setText("Password:");
pwdL.setPreferredSize(new Dimension(85, 20));
loginPanel.add(pwdL);
passwordField.setEchoChar('#');
passwordField.setPreferredSize(new Dimension(100, 25));
loginPanel.add(passwordField);
enterB.setForeground(new Color(0, 153, 0));
enterB.setText("Enter");
enterB.setPreferredSize(new Dimension(70, 25));
enterB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
enterBActionPerformed(evt);
}
});
loginPanel.add(enterB);
getRootPane().setDefaultButton(enterB);
exitB.setForeground(new Color(255, 0, 0));
exitB.setText("Exit");
exitB.setPreferredSize(new Dimension(70, 25));
exitB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
exitBActionPerformed(evt);
}
});
loginPanel.add(exitB);
getContentPane().add(loginPanel, BorderLayout.CENTER);
}
private void enterBActionPerformed(ActionEvent evt) {
username = nameField.getText();
char[] input = passwordField.getPassword();
boolean isCorrect = false;
try{
isCorrect = isPasswordCorrect(username, input);
}catch(SQLException ex){
JOptionPane.showMessageDialog(this,ex.getMessage());
}
if ( isCorrect ) {
setVisible(false);
dispose();
} else {
JOptionPane.showMessageDialog(this,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
passwordField.setText("");
passwordField.requestFocusInWindow();
}
}
private void exitBActionPerformed(ActionEvent evt) {
System.exit(0);
}
private static boolean isPasswordCorrect(String username, char[] input) throws SQLException{
// boolean isCorrect = false;
// Statement statement = null;
// ResultSet rs = null;
// Connection conn = null;
// try{
// DriverManager.registerDriver( new sun.jdbc.odbc.JdbcOdbcDriver() );
// conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, USER_PASSWORD);
// if(conn!=null){
// statement = conn.createStatement();
// rs = statement.executeQuery("select Name from "+TABLE_NAME
// +" where Name='"+username+"' and Password='"+String.valueOf(input)+"'");
// isCorrect = rs.next();
// }
// }finally{
// if(rs!=null)rs.close();
// if(statement!=null)statement.close();
// if(conn!=null)conn.close();
// }
// //Zero out the password, for security.
// for (int i = 0; i < input.length; i++) {
// input[i] = 0;
// }
// return isCorrect;
return true;
}
public String getUsername(){
return username;
}
private JButton enterB, exitB;
private JPanel loginPanel;
private JTextField nameField;
private JLabel nameL, pwdL;
private JPasswordField passwordField;
private String username;
private static final String DATABASE_NAME = "meinedb";
private static final String DATABASE_URL = "jdbc:odbc:" + DATABASE_NAME;
private static final String TABLE_NAME = "testuhrand";
private static final String DATABASE_USER = "test";
private static final String USER_PASSWORD = "";
}