Login
conn = MY_DB.ConnectDb();
frame = new JFrame("Login");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(width,height);
Util.centreWindow(frame);
frame.getContentPane().setBackground(Color.ORANGE);
ImageIcon icon = new ImageIcon("ic_lock_black.png");
frame.setIconImage(icon.getImage());
frame.setLayout(null);
frame.setResizable(false);
checkLogin();
After creation of window, we will add a AWT background.
Container mainContainer=frame.getContentPane();
int panelWidth=width/3;
int panelHeight=height*7/10;
JPanel panel = new JPanel();
panel.setSize(new Dimension(panelWidth,panelHeight));
panel.setLocation(width/2-width/6,height/8);
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));
panel.setLayout(new FlowLayout());
ImageIcon image = new ImageIcon("ic_lock_black.png");
JLabel label=new JLabel("LOGIN",image,JLabel.CENTER);
label.setVerticalAlignment(JLabel.TOP);
label.setHorizontalAlignment(JLabel.HORIZONTAL);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.HORIZONTAL);
label.setText("Login");
label.setBackground(Color.red);
label.setForeground(Color.black);
label.setSize(new Dimension(panelWidth/4,panelHeight/50));
label.setFont(new Font("Lucida",Font.PLAIN,30));
label.setAlignmentX(JLabel.HORIZONTAL);
label.setLocation(0, 200);
panel.add(label);
So we are done with all frame and background UI. Now we will create Loging UI.
Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder usernametittle = BorderFactory.createTitledBorder(loweredetched, "User Name");
usernametittle.setTitleJustification(TitledBorder.LEFT);
TitledBorder passwordTittled = BorderFactory.createTitledBorder(loweredetched, "Password");
passwordTittled.setTitleJustification(TitledBorder.LEFT);
myTextField = new JTextField();
myTextField.setBounds(width/2-(panelWidth*3/4)/2,panelHeight*60/100, panelWidth*3/4, 50);
myTextField.setBorder(usernametittle);
frame.add(myTextField);
myPasswordField = new JPasswordField();
myPasswordField.setBounds(width/2-(panelWidth*3/4)/2,panelHeight*80/100, panelWidth*3/4, 50);
panel.add(myPasswordField,BorderLayout.CENTER);
myPasswordField.setBorder(passwordTittled);
frame.add(myPasswordField);
RoundedButton loginButton=new RoundedButton("Login");
loginButton.setBackground(Color.ORANGE);
loginButton.setBounds(width/2-75,panelHeight*100/100,150,50);
frame.add(loginButton);
Here is logic to login admin on click "Login" Button :
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String idLogin = null ;
String password = null;
Statement stmt = null;
ResultSet rs = null;
String sql_id = "SELECT value from system_setting where key is 'ADMIN_ID'";
String sql_pw = "SELECT value from system_setting where key is 'ADMIN_PW'";
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql_id);
// loop through the result set
while (rs.next()) {
idLogin=rs.getString("value");
}
System.out.println(idLogin+" hhh"+myTextField.getText());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
//PASSWORD
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql_pw);
// loop through the result set
while (rs.next()) {
password=rs.getString("value");
}
System.out.println(idLogin+" hhh"+myPasswordField.getText());
} catch (SQLException e) {
System.out.println(e.getMessage());
}finally {
if(rs != null){
try{
rs.close();
} catch(Exception e){
e.printStackTrace();
}
}
if(stmt != null){
try{
stmt.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
if(idLogin!=null && password!=null){
if(idLogin.matches(myTextField.getText()) && password.matches(myPasswordField.getText())){
updateisLoginValue("1");
JOptionPane.showMessageDialog(frame, "Username: " + myTextField.getText()+"Password"+myPasswordField.getText());
}
new Dashboard(width,height);
frame.dispose();
}
else
JOptionPane.showMessageDialog(frame, "Unmatched");
}
else{
JOptionPane.showMessageDialog(frame, "Unmatched");
}
}
});
mainContainer.add(panel);
frame.setVisible(true);
}
Here's logic for update login value.
public void updateisLoginValue(String updatValue) {
Connection conn=MY_DB.ConnectDb();
boolean issuccess=false;
Statement stmt = null;
int action;
String newValue = updatValue;
String idToMatch = "IS_LOGGED_IN";
String sqlUpdate = "UPDATE system_setting "
+ "SET value = ? "
+ "WHERE key = ?";
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = conn.prepareStatement(sqlUpdate);
pst.setString(1, newValue);
pst.setString(2, idToMatch);
pst.executeUpdate();
action = pst.executeUpdate();
if(action>0){
issuccess= true;
}else{
issuccess= false;
}
} catch (Exception a) {
a.printStackTrace();
} finally {
if(rs != null){
try{
rs.close();
} catch(Exception e){
e.printStackTrace();
}
}
if(pst != null){
try{
pst.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}
Here's logic for check Loged or Not.
private void checkLogin() {
// TODO Auto-generated method stub
Connection conn=MY_DB.ConnectDb();
String response = null;
Statement pst = null;
ResultSet rs = null;
String sql = "SELECT value from system_setting where key Is 'IS_LOGGED_IN'";
try {
pst = conn.createStatement();
rs= pst.executeQuery(sql);
// loop through the result set
response=rs.getString(1);
if(response.equals("1")) {
new Dashboard(width,height);;
}
else {
getLoginView(width,height);
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs != null){
try{
rs.close();
} catch(Exception e){
e.printStackTrace();
}
}
if(pst != null){
try{
pst.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}
Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2021 by Programmer Mitra. All Rights Reserved.