Here i will posting links for all UI (better and improved version of sir's)
CODE-
import Planet_Food.Dao.UserDao;
import Planet_Food.dbUtil.DBConnection;
import Planet_Food.pojo.User;
import Planet_Food.pojo.UserProfile;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class Login_frame extends javax.swing.JFrame {
private String userId;
private String password;
public Login_frame()
{
initComponents();
this.setLocationRelativeTo(null);
}
public boolean isvalidInput()
{
userId=txtUserId.getText();
char []pwd=txtPassword.getPassword();
if(userId.isEmpty() || pwd.length==0)
{
return false;
}
else
{
password=String.valueOf(pwd);//convert any type into string
return true;
}
}
private String getUserType()
{
if(jrAdmin.isSelected())
return jrAdmin.getText();
else if(jrCashier.isSelected())
return jrCashier.getText();
else
return null;
}
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
//LOGIN here
if(isvalidInput())
{
String usertype=null;
if(usertype.equals(null))
{
JOptionPane.showMessageDialog(null, "Select a radio button","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
//if all values are validated
try
{
User user=new User();
user.setUserId(userId);
user.setPassword(password);
user.setUserType(usertype);
String username=UserDao.validateUser(user);
if(username!=null)
{
//everything correct
JOptionPane.showMessageDialog(null, "Login Accepted","Wlcome"+username,JOptionPane.INFORMATION_MESSAGE);
UserProfile.setUsername(username);
UserProfile.setUsertype(usertype);
if(usertype.equalsIgnoreCase("admin"))
{
admin_option_frame adminFrame=new admin_option_frame();
adminFrame.setVisible(true);
}
else
{
Cashier_option_frame cashierFrame=new Cashier_option_frame();
cashierFrame.setVisible(true);
}
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "UserId or Password incorrect","Login Denied",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "DB error: "+e,"Exception",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
else
{
JOptionPane.showMessageDialog(null, "UserId or Password incorrect","Incorrect",JOptionPane.ERROR_MESSAGE);
}
}
private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DBConnection.closeConnection();
int ans=0;
ans=JOptionPane.showConfirmDialog(null, "Are you sure?", "Quitting", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if(ans==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class DBConnection {
private static Connection conn=null;
static
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe","naman","naman");
JOptionPane.showMessageDialog(null,"Successfully loaded","Sucess!",JOptionPane.INFORMATION_MESSAGE);
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"class not found exception"+ e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(null,"SQL Exception"+ ex.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
public static Connection getConnection()
{
return(conn);
}
public static void closeConnection()
{
if(conn!=null)
{
try{
conn.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Error while disconnecting","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
}
import Planet_Food.dbUtil.DBConnection;
import Planet_Food.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author Naman Khurpia
*/
public class UserDao {
public static String validateUser(User user)throws SQLException
{
Connection conn=DBConnection.getConnection();
String qry="select username from Users where userid=? and password=? and usertype=?";
PreparedStatement ps=conn.prepareStatement(qry);
ps.setString(1,user.getUserid());
ps.setString(2,user.getPassword());
ps.setString(3,user.getUserType());
ResultSet rs=ps.executeQuery();
String username=null;
if(rs.next())//if we user while it will be the same as that of if, no changes in that
{
username=rs.getString(1);
}
return username;
}
}
SPLASH SCREEN-
here screen size is 700*350 px
and the image resource is of size 700*315 px
35px for the progress bar
download the above image from
LINK
CODE-
CODE-
public class Splash_screen_frame extends javax.swing.JFrame {
//declaring inner class object globally to be accessed by others
SplashThread sp;
public Splash_screen_frame()
{
initComponents();
super.setLocationRelativeTo(null);
jProgressBar1.setStringPainted(true);
sp=new SplashThread();
sp.start();
}
//declaring new class
class SplashThread extends Thread
{
public void run()
{
int count=1;
Random r=new Random();
while(jProgressBar1.getValue()<jProgressBar1.getMaximum())
{
try
{
jProgressBar1.setValue((int)count);
Thread.sleep(1200);
count=count+r.nextInt(100);
}
catch(InterruptedException e)
{
JOptionPane.showMessageDialog(null, "Exception in Thread"+e, "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
Splash_screen_frame.this.dispose();
Login_frame login=new Login_frame();
login.setVisible(true);
}
}
}
DIRECTORY STRUCTURE-
LOGIN PAGE -
here screen size is 700*300 px
and the image resource is of size 350*300 px
350px for the login section which is placed over a panel
and the image resource is of size 350*300 px
350px for the login section which is placed over a panel
download the above image from
CODE-
import Planet_Food.Dao.UserDao;
import Planet_Food.dbUtil.DBConnection;
import Planet_Food.pojo.User;
import Planet_Food.pojo.UserProfile;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class Login_frame extends javax.swing.JFrame {
private String userId;
private String password;
public Login_frame()
{
initComponents();
this.setLocationRelativeTo(null);
}
public boolean isvalidInput()
{
userId=txtUserId.getText();
char []pwd=txtPassword.getPassword();
if(userId.isEmpty() || pwd.length==0)
{
return false;
}
else
{
password=String.valueOf(pwd);//convert any type into string
return true;
}
}
private String getUserType()
{
if(jrAdmin.isSelected())
return jrAdmin.getText();
else if(jrCashier.isSelected())
return jrCashier.getText();
else
return null;
}
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
//LOGIN here
if(isvalidInput())
{
String usertype=null;
if(usertype.equals(null))
{
JOptionPane.showMessageDialog(null, "Select a radio button","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
//if all values are validated
try
{
User user=new User();
user.setUserId(userId);
user.setPassword(password);
user.setUserType(usertype);
String username=UserDao.validateUser(user);
if(username!=null)
{
//everything correct
JOptionPane.showMessageDialog(null, "Login Accepted","Wlcome"+username,JOptionPane.INFORMATION_MESSAGE);
UserProfile.setUsername(username);
UserProfile.setUsertype(usertype);
if(usertype.equalsIgnoreCase("admin"))
{
admin_option_frame adminFrame=new admin_option_frame();
adminFrame.setVisible(true);
}
else
{
Cashier_option_frame cashierFrame=new Cashier_option_frame();
cashierFrame.setVisible(true);
}
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "UserId or Password incorrect","Login Denied",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "DB error: "+e,"Exception",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
else
{
JOptionPane.showMessageDialog(null, "UserId or Password incorrect","Incorrect",JOptionPane.ERROR_MESSAGE);
}
}
private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DBConnection.closeConnection();
int ans=0;
ans=JOptionPane.showConfirmDialog(null, "Are you sure?", "Quitting", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if(ans==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
Now Create a package called PlanetFood.pojo
inside this create a java file named as User
CODE inside User- (pojo)
public class User {
private String userId;
private String password;
private String userType;
public String getUserid() {
return userId;
}
public void setUserId(String userid) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
}
Now create another class in pojo package named as UserProfile
public class UserProfile {
private static String username;
private static String usertype;
public static String getUsername() {
return username;
}
public static void setUsername(String username) {
UserProfile.username = username;
}
public static String getUsertype() {
return usertype;
}
public static void setUsertype(String usertype) {
UserProfile.usertype = usertype;
}
}
Now Create a package called PlanetFood.dbUtils
inside this create a java class called DBConnection
Code-
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class DBConnection {
private static Connection conn=null;
static
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe","naman","naman");
JOptionPane.showMessageDialog(null,"Successfully loaded","Sucess!",JOptionPane.INFORMATION_MESSAGE);
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"class not found exception"+ e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(null,"SQL Exception"+ ex.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
public static Connection getConnection()
{
return(conn);
}
public static void closeConnection()
{
if(conn!=null)
{
try{
conn.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Error while disconnecting","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
}
Now Create another package called FoodPlanet.Dao
inside this create a java class called UserDao
CODE-
import Planet_Food.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* @author Naman Khurpia
*/
public class UserDao {
public static String validateUser(User user)throws SQLException
{
Connection conn=DBConnection.getConnection();
String qry="select username from Users where userid=? and password=? and usertype=?";
PreparedStatement ps=conn.prepareStatement(qry);
ps.setString(1,user.getUserid());
ps.setString(2,user.getPassword());
ps.setString(3,user.getUserType());
ResultSet rs=ps.executeQuery();
String username=null;
if(rs.next())//if we user while it will be the same as that of if, no changes in that
{
username=rs.getString(1);
}
return username;
}
}