/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Controller;
public static void login(String usName, String pass){ try{ String username = null; //initial value of the username String password = null;//initial value of the password
ResultSet rs= new DbSearch().searchLogin(usName); while (rs.next()){ username=rs.getString("username"); password=rs.getString("password"); } if (username !=null && password != null){ if(password.equals(pass)){ System.out.println("Login Successfull"); Login.getFrames()[1].dispose(); new Home().setVisible(true); } else{ JOptionPane.showMessageDialog(null, "Please check the credentials", "Error", JOptionPane.ERROR_MESSAGE); } DbConnection.closeCon(); } catch (SQLException ex){ Logger.getLogger(LoginController.class.getName()).Log(Level.SEVERE, null, ex); } } } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package View;
import java.sql.ResultSet;
ReplyDeleteimport java.sql.Statement;
public class DBSerch {
Statement stmt;
ResultSet rs;
public ResultSet searchLogin(String usName){
try{
stmt =DBConnection.getStatementConnection();
String name = usName;
rs=stmt.executeQuery("SELECT * FROM login where username=*"+name+ "'");
}catch (Exception e){
e.printStackTrace();
}
return rs;
}}
/*
ReplyDelete* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Controller;
/**
*
* @author student.lab1
*/
import view.Home;
import model.DbConnection;
import model.DbSearch;
import java.awt.Frame;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import view.Login;
public class LoginController {
public static void login(String usName, String pass){
try{
String username = null; //initial value of the username
String password = null;//initial value of the password
ResultSet rs= new DbSearch().searchLogin(usName);
while (rs.next()){
username=rs.getString("username");
password=rs.getString("password");
}
if (username !=null && password != null){
if(password.equals(pass)){
System.out.println("Login Successfull");
Login.getFrames()[1].dispose();
new Home().setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "Please check the credentials", "Error", JOptionPane.ERROR_MESSAGE);
}
DbConnection.closeCon();
}
catch (SQLException ex){
Logger.getLogger(LoginController.class.getName()).Log(Level.SEVERE, null, ex);
}
}
}
}
to hashi
ReplyDelete/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package View;
/**
*
* @author student.lab1
*/
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection {
static Connection conn;
static Statement stat = null;
public static Statement getStatementConnection(){
try{
//Establis the connection
String url ="jdbc:mysql://localhost:3306/librarydb";
conn = DriverManager.getConnection(url, "root", "");
//Create the Connection
stat = conn.createStatement();
}
catch (Exception e) {
e.printStackTrace();
}
return stat;
}
//Close the connection
public static void closeCon() throws SQLException {
conn.close();
}
}