vendredi 27 février 2015

how can i create a logon form with different users with Java and SQLite

i am creating a login form for my project and i works great,the issue is that i want to open up different forms dependant on the username and password that i enter.for example if i enter a password 'manager'then it should open the manager page,or 'employee'should open the employee page.


i'm thinking that it is just a case of creating another sql query but i have tried and i cannot see the solution


any help would be appreciated


Thanks guys


here is my login code



public class Logging_in extends javax.swing.JFrame {


Connection conn = null;//connection to the database
ResultSet rs = null; //will hold the result of the sql query
PreparedStatement pst = null;//willJJ hold the sql statement


public Logging_in() {
initComponents();
conn = Connect.ConnecrDb();//function that will be used to connect to the database
}

public void close(){
//the close function will be used to close a form after another form has been opened
WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);

}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
txt_password = new javax.swing.JPasswordField();
txt_username = new javax.swing.JTextField();
btn_Login = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BACK OFFICE SYSTEM", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 18), new java.awt.Color(0, 0, 255))); // NOI18N

jLabel2.setText("Password");

jLabel1.setText("Username");

txt_password.setBackground(new java.awt.Color(153, 255, 255));

txt_username.setBackground(new java.awt.Color(153, 255, 255));

btn_Login.setText("Login to system");
btn_Login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_LoginActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(71, 71, 71)
.addComponent(txt_username, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 10, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txt_password, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(btn_Login)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(62, 62, 62)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(txt_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txt_password, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(66, 66, 66)
.addComponent(btn_Login)
.addContainerGap())
);

jLabel3.setIcon(new javax.swing.ImageIcon("C:\\Users\\jason\\Documents\\computer graded project.jpg")); // NOI18N

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(124, Short.MAX_VALUE)
.addComponent(jLabel3)
.addGap(76, 76, 76)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(90, Short.MAX_VALUE))
);

setSize(new java.awt.Dimension(717, 412));
setLocationRelativeTo(null);
}// </editor-fold>

private void btn_LoginActionPerformed(java.awt.event.ActionEvent evt) {
// code to login to the back office system

String sql =" select username,password from Employee where username =? AND password =?";//sql query

try{

pst =conn.prepareStatement(sql);
pst.setString(1,txt_username.getText());//get username from user
pst.setString(2,txt_password.getText());//get password from user

rs = pst.executeQuery();

if(rs.next())//if resultset(results of the sql query is correct)show message
{

JOptionPane.showMessageDialog(null,"username and password are correct");
rs.close();
pst.close();
close();
Office office = new Office();//create object of new form
office.setVisible(true);//set the new form visible if the login is successful

}
else if(!rs.next())//if login is not successful(user enters the wrong username and password)

{
JOptionPane.showMessageDialog(null,"username and password are not correct");//show error message

Employee_page emp_page = new Employee_page();
emp_page.setVisible(true);

}


}catch(Exception e)

{
JOptionPane.showMessageDialog(null, e);

}finally{
try{

rs.close();
pst.close();
//conn.close();



}catch(Exception e){}



}

Aucun commentaire:

Enregistrer un commentaire