Login
this.totalWinWidth = totalWinWidth;
this.totalWinHeight = totalWinHeight;
winWidth=totalWinWidth-totalWinWidth/20;
winHeight=totalWinHeight;
frame = new JFrame("Login");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
Utils.centeredFrame(frame, winWidth,winHeight, "Login");
frame.setVisible(true);
frame.setResizable(false);
Container mainContainer=frame.getContentPane();
mainContainer.setBackground(Color.gray);
checkLogin();
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setOpaque(false);
panel.setBackground(Color.BLACK);
panel.setSize(new Dimension(winWidth/3,winHeight*7/10));
panel.setLocation(winWidth/2-winWidth/6,winHeight/10);
mainContainer.add(panel,BorderLayout.CENTER);
JComponent jcomponent=new RoundedBackground(winWidth/2-winWidth/6,winHeight/10,winWidth/3,winHeight*7/10);//
mainContainer.add(jcomponent,BorderLayout.CENTER);
After creation of window, we will created RoundedBackground class for background.
public class RoundedBackground extends JComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
int width, height,setX,setY;
public RoundedBackground (int setX,int setY,int width, int height) {
this.width=width;
this.height=height;
this.setX=setX;
this.setY=setY;
setSize(width, height);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.white);
//g2.setStroke(new BasicStroke(2.0f));
double x = setX;
double y = setY;
double w = width;
double h = height;
RoundRectangle2D round=new RoundRectangle2D.Double(x, y, w, h, 80, 80);
g2.fill(round);
}
}
So we are done with all frame and background UI. Now we will create Loging UI.
JLabel label=new JLabel();
label.setText("Login");
label.setBackground(Color.red);
label.setForeground(Color.black);
label.setSize(new Dimension(winWidth/3,winHeight/10));
label.setFont(new Font("Lucida",Font.PLAIN,48));
label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label.setLocation(150, 50);
panel.add(label, BorderLayout.CENTER);
JLabel user_name=new JLabel();
user_name.setText("User name");
user_name.setBackground(Color.red);
user_name.setForeground(Color.black);
user_name.setFont(new Font("Lucida",Font.PLAIN,12));
user_name.setSize(new Dimension(winWidth/3,winHeight/10));
user_name.setLocation(50, 165);
panel.add(user_name, BorderLayout.SOUTH);
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);
usernameField = new JTextField();
usernameField.setSize(new Dimension(winWidth/4,winHeight/15));
usernameField.setLocation(50, 230);
usernameField.setBorder(usernametittle);
panel.add(usernameField,BorderLayout.CENTER);
panel.validate();
panel.repaint();
JLabel password=new JLabel();
password.setText("password");
password.setBackground(Color.red);
password.setForeground(Color.black);
password.setFont(new Font("Lucida",Font.PLAIN,12));
password.setSize(new Dimension(winWidth/3,winHeight/10));
password.setLocation(50, 265);
panel.add(password, BorderLayout.SOUTH);
passwordField = new JPasswordField();
passwordField.setSize(new Dimension(winWidth/4,winHeight/15));
passwordField.setLocation(50, 330);
panel.add(passwordField,BorderLayout.CENTER);
passwordField.setBorder(passwordTittled);
panel.validate();
panel.repaint();
JButton loginButton = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(200, 50, java.awt.Image.SCALE_SMOOTH)));
loginButton.setContentAreaFilled(false);
loginButton.setBorder(null);
loginButton.setText("LOGIN");
loginButton.setHorizontalTextPosition(JButton.CENTER);
loginButton.setVerticalTextPosition(JButton.CENTER);
loginButton.setOpaque(false);
loginButton.setContentAreaFilled(false);
loginButton.setBorderPainted(false);
loginButton.setFont(new java.awt.Font("Serif",1, 15));
loginButton.setForeground(Color.white);
loginButton.setBounds(130,450,200,50);
panel.add(loginButton);
public class SQLiteJDBCDriverConnection {
/**
* Connect to a sample database
*/
public String rate=null;
public String date=null;
public static Connection connect1() {
try {
// SQLite connection string
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ex) {
Logger.getLogger(SQLiteJDBCDriverConnection.class.getName()).log(Level.SEVERE, null, ex);
}
// String url = "jdbc:sqlite:C:\\Users\\LWSSD\\eclipse-workspace\\HostelManagement\\hostelmanagement.db";
String url = "jdbc:sqlite:bankmanagement.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}
Here's logic for login staff who exist in bankmanagment.db database.
String usernameString = usernameField.getText();
String passString = passwordField.getText();
String dbUserName = "";
String dbPassword = "";
String name = "";
String dbStfName = "";
String dbStfPassword = "";
String key1 = "ADMIN_ID";
String key2 = "ADMIN_PASS";
if(!usernameString.isEmpty() && !passString.isEmpty()) {
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'ADMIN_ID'");
ResultSet rs = insert.executeQuery();
while (rs.next()) {
dbUserName = rs.getString(1);
}
insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'ADMIN_PWD'");
ResultSet rs2 = insert.executeQuery();
while (rs2.next()) {
dbPassword = rs2.getString(1);
}
String sql = "SELECT staff_id, name , staff_pass from staff where staff_id like '%"+usernameString+"%';";
Statement stmt = conn.createStatement();
ResultSet rs3 = stmt.executeQuery(sql);
while (rs3.next()) {
dbStfName=rs3.getString("staff_id");
name = rs3.getString("name");
dbStfPassword=rs3.getString("staff_pass");
AppConstant.STAFF_NAME = name;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(usernameString + dbUserName + passString + dbPassword);
// System.out.println(usernameString.equals(dbUserName) && passString.equals(dbPassword));
if( usernameString.equals(dbUserName) && passString.equals(dbPassword)) {
String key_is_logined = "IS_LOGINED";
try {
insert = conn.prepareStatement("UPDATE system_setting "
+ "SET value = ? "
+ "WHERE key = ?");
insert.setString(1, "1");
insert.setString(2, key_is_logined);
insert.executeUpdate();
conn.close();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
navigate_to_staff();
usernameField.setText("");
passwordField.setText("");
//navigate_to_dashboard();
}else if( usernameString.equals(dbStfName) && passString.equals(dbStfPassword)) {
try {
String key_is_logined = "IS_LOGINED";
insert = conn.prepareStatement("UPDATE system_setting "
+ "SET value = ? "
+ "WHERE key = ?");
insert.setString(1, "1");
insert.setString(2, key_is_logined);
insert.executeUpdate();
String sqlUpdate = "UPDATE system_setting SET value = ? WHERE key = ?";
insert = conn.prepareStatement(sqlUpdate);
insert.setString(1, dbStfName);
insert.setString(2, "LOGINED_ID");
insert.executeUpdate();
insert = conn.prepareStatement(sqlUpdate);
insert.setString(1, dbStfPassword);
insert.setString(2, "LOGINED_PWD");
insert.executeUpdate();
usernameField.setText("");
passwordField.setText("");
conn.close();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
navigate_to_dashboard();
}else {
JOptionPane.showMessageDialog(frame, "Unmatched");
}
}else {
String var = "Usdername Should not be empty, Password Should not be empty";
JOptionPane.showMessageDialog(frame, var);
}
Here's logic for check Loged or Not , Also show Loged Staff Name.
String key_id = "IS_LOGINED";
String loginKey = "";
String stafId = "";
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'IS_LOGINED'");
ResultSet rs = insert.executeQuery();
while (rs.next()) {
loginKey = rs.getString(1);
}
if(loginKey.equals("1")) {
insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'LOGINED_ID'");
ResultSet rs2 = insert.executeQuery();
while (rs2.next()) {
stafId = rs2.getString(1);
}
String sql = "SELECT name from staff where staff_id like '%"+stafId+"%';";
Statement stmt = conn.createStatement();
ResultSet rs3 = stmt.executeQuery(sql);
while (rs3.next()) {
AppConstant.STAFF_NAME = rs3.getString("name");
}
conn.close();
// AppConstant.STAFF_NAME = stafId;
navigate_to_dashboard();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
When we want go AddStaff window.
private void navigate_to_staff() {
new AddStaff(frame);
}
When we want go back to DashBoard.
private void navigate_to_dashboard() {
new Dashboard(frame);
}
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.