0% found this document useful (0 votes)
17 views2 pages

2

The document outlines a Java code snippet for a login system that connects to a database and validates user credentials. It checks for empty username and password fields, executes a SQL query to verify the credentials, and either opens a dashboard or displays an error message. Error handling is included for SQL exceptions during the process.

Uploaded by

dilankasilva86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

2

The document outlines a Java code snippet for a login system that connects to a database and validates user credentials. It checks for empty username and password fields, executes a SQL query to verify the credentials, and either opens a dashboard or displays an error message. Error handling is included for SQL exceptions during the process.

Uploaded by

dilankasilva86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

try {

Connection conn = DBConnection.connect();


PreparedStatement pst = null;
ResultSet rs = null;

boolean error = false;


String uname = username.getText().trim();
String pword = password.getText().trim();

if(uname.isEmpty()){
error = true;
//JOptionPane.showMessageDialog(null,"UserName Cannot be empty.");
//errorla.setText("UserName Cannot be empty !.");

}
if(pword.isEmpty()){
error = true;
//JOptionPane.showMessageDialog(null,"Password Cannot be empty.");
//errorla.setText("Password Cannot be empty !.");
}
if(error){
errorla.setText("UserName & Password Cannot be empty !.");
}

String loginsql = "SELECT ID,First_Name,Last_Name FROM WHERE


User_Name=? AND Password=?";
pst = (PreparedStatement)conn.prepareStatement(loginsql);
pst.setString(1,uname);
pst.setString(2, pword);

rs = pst.executeQuery();

if(rs.next()){
SystemDashbord obj = new SystemDashbord();
obj.setVisible(true);
this.dispose();

JLabel Session = new JLabel(uname);


obj.add(Session);
obj.setLayout(null);
obj.setVisible(true);

}else{
errorla.setText("UserName or Password Invalid !.");
}

//SystemDashbord mydb = new SystemDashbord();


// mydb.setVisible(true);

//this.dispose();
} catch (SQLException ex) {
Logger.getLogger(SystemLogin.class.getName()).log(Level.SEVERE, null,
ex);
}

You might also like