The Artima Developer Community
Sponsored Link

Java Answers Forum
Please, help me with login

6 replies on 1 page. Most recent reply: May 30, 2003 12:57 AM by Fawad Shah

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Jessica

Posts: 3
Nickname: mabel
Registered: May, 2003

Please, help me with login Posted: May 22, 2003 11:21 AM
Reply to this message Reply
Advertisement
Hi everybody!

I am working on a video rental application. The system must provide a password protected environment with 2 different levels of authorisation. 1. Managerial level: who has the right to unlimited access. 2. Employee: who has access to their own specific functionality. I would like to solve this by using a pop-up menu, which pop-up when the user interacts with the application. But I have no idea how to do this. I would really appreciate some help! I attach the gui that I have been working on.

Thanks a million!
Mabel

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.awt.List;
import java.util.Vector;


public class Gui extends JFrame
{

//Declaration of all the panels:
private JPanel myContentPanel;
private JPanel rightPanel;
private JPanel leftPanel;
private MenuBar mb;

//Declaration of all the labels:
private JLabel actor;
private JLabel title;
private JLabel genre;
private JLabel length;
private JLabel videoNo;
private JLabel price;
private JLabel amount;
private JLabel userName1;
private JLabel userName2;
private JLabel password1;
private JLabel password2;
private JLabel date;
private JLabel lendNo;
private JLabel lendSpecNo;
private JLabel searchString;
private JLabel searchString2;
private JLabel searchString3;
private JLabel stString;
private JLabel customerNo;


//Observer objekt declares
private List frameList;

//Input field:
private TextField actorField;
private TextField titleField;
private TextField genreField;
private TextField lengthField;
private TextField videoNoField;
private TextField priceField;
private TextField amountField;
private TextField userNameField;
private TextField passwordField;
private TextField dateField;
private TextField lendNoField;
private TextField lendSpecField;
private TextField stField;
private TextField customerNoField;


//Text field for the search string:
private TextField searchField;
private String result= "";


private Color blue = new Color(44, 153, 211);
private Color grey = new Color(190, 194, 194);
private Color black = new Color(0, 0, 0);
pmqa_DBLayer database;

//-------------------------------------Gui()----------------------------------- -----------------------

public Gui()
{

database = new pmqa_DBLayer();
myContentPanel = new JPanel(new GridLayout(1,2));

//myContentPanel.setBackground(blue);
myContentPanel = new JPanel(new GridLayout(1,2));
myContentPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
setTitle("The Webshop");
//setSize(650,500);
Toolkit theKit = getToolkit();
Dimension wndSize = theKit.getScreenSize();
setBounds(wndSize.width/4, wndSize.height/4,
wndSize.width/2, wndSize.height/2);


leftPanel = new JPanel(new GridLayout(2,1));
leftPanel.setPreferredSize(new Dimension(200, 200));
leftPanel.setBorder(BorderFactory.createTitledBorder("leftPanel"));
leftPanel.setBackground(blue);
leftPanel.setVisible(true);

frameList = new List();
frameList.setBackground(grey);
leftPanel.add(frameList);

myContentPanel.add(leftPanel);

rightPanel = new JPanel(new GridLayout(1,1)); //create centre panel
rightPanel.setBorder(BorderFactory.createTitledBorder("rightPanel"));
rightPanel.setBackground(blue);
myContentPanel.add(rightPanel);

setContentPane(myContentPanel);
makeMenus();

addWindowListener(new WindowListener()); //add the new inner class WindowListener
setVisible(true);
validate();
//end of the constructor

}//Gui()

//-------------------------------------------makeMenus()------------- --------------------------------

public void makeMenus(){
MenuBar mb = new MenuBar ();
Menu staffMenu = new Menu ("Staff");
Menu admMenu = new Menu ("Administration");
mb.add(staffMenu);
mb.add(admMenu);

admMenu.add(new MenuItem("Search Video"));
admMenu.addSeparator();
admMenu.add(new MenuItem("Lend"));
admMenu.addSeparator();
admMenu.add(new MenuItem("Show statistics"));
admMenu.addSeparator();
admMenu.add(new MenuItem("Insert Video"));
admMenu.addSeparator();
admMenu.add(new MenuItem("Remove Video"));
admMenu.addSeparator();
admMenu.add(new MenuItem("Disconnect"));


staffMenu.add(new MenuItem("Search Video"));
staffMenu.addSeparator();
staffMenu.add(new MenuItem("Lend"));
staffMenu.addSeparator();
staffMenu.add(new MenuItem("Show Statistics"));
staffMenu.addSeparator();
staffMenu.add(new MenuItem("Disconnect"));


staffMenu.addActionListener(new MyInnerMenuListenerClass());
admMenu.addActionListener(new MyInnerMenuListenerClass());
setMenuBar(mb);
}//makeMenus() ends

//-------------------------------------------createButton()-------------- ----------------------
//Method to get the same size on the buttons
public JButton createButton(String label)
{
JButton Button = new JButton(label);
Button.setBackground(blue);
Button.setPreferredSize(new Dimension(40, 5));
return Button;
}//createButton()

//--------------------------------------searchPanel()-------------------------- ----------------------

public void searchPanel()
{
clearResult();
myContentPanel.remove(rightPanel);
JPanel searchPanel = new JPanel();
searchPanel.setBackground(blue);
FlowLayout flow = new FlowLayout();
searchPanel.setLayout(flow);


titleString = new JLabel ("Title:" ,Label.LEFT);
titleString.setForeground(black);

titleField = new TextField (14);
titleField.setBackground(grey);
searchPanel.add(titleField);


actorString = new JLabel ("Actor:" ,Label.LEFT);
actorString.setForeground(black);

actorField = new TextField (14);
actorField.setBackground(grey);
searchPanel.add(actorField);


genreString = new JLabel ("Genre:" ,Label.LEFT);
genreString.setForeground(black);

genreField = new TextField (14);
genreField.setBackground(grey);
searchPanel.add(genreField);


lengthString = new JLabel ("Length:" ,Label.LEFT);
lengthString.setForeground(black);

lengthField = new TextField (14);
lengthField.setBackground(grey);
searchPanel.add(lengthField);

JButton search = new JButton("Search");
search.setBackground(grey);
search.addActionListener(new MyInnerButtonListenerClass());
searchPanel.add(search);

rightPanel = searchPanel;
myContentPanel.add(rightPanel);

setVisible(true);
validate();
}//searchPanel()
//------------------------------------stPanel()--------------- --------------------------

public void stPanel()
{
clearResult();
myContentPanel.remove(rightPanel);
JPanel stPanel = new JPanel();
stPanel.setBackground(blue);
FlowLayout flow = new FlowLayout();
stPanel.setLayout(flow);


stString = new JLabel ("Title:" ,Label.LEFT);
stString.setForeground(black);
stPanel.add(stString);

stField = new TextField (14);
stField.setBackground(grey);
stPanel.add(stField);

JButton statistic = new JButton("Statistic");
statistic.setBackground(grey);
statistic.addActionListener(new MyInnerButtonListenerClass());
stPanel.add(statistic);

rightPanel = stPanel;
myContentPanel.add(rightPanel);

setVisible(true);
validate();
}//stPanel()

//---------------------------------loginPanel()------------------ --------------
public void loginPanel()
{
clearResult();
myContentPanel.remove(rightPanel);
JPanel loginPanel = new JPanel();
loginPanel.setBackground(blue);
FlowLayout flow = new FlowLayout();
loginPanel.setLayout(flow);


searchString2 = new JLabel ("Username:" ,Label.LEFT);
searchString2.setForeground(black);
searchPanel2.add(searchString2);

userNameField = new TextField (14);
userNameField.setBackground(grey);
searchPanel2.add(userNameField);

searchString3 = new JLabel ("Password:" ,Label.LEFT);
searchString3.setForeground(black);
searchPanel2.add(searchString3);

passwordField = new TextField (14);
passwordField.setBackground(grey);
searchPanel2.add(passwordField);

JButton logIn = new JButton("Login");
logIn.setBackground(grey);
logIn.addActionListener(new MyInnerButtonListenerClass());
searchPanel2.add(logIn);

rightPanel = searchPanel2;
myContentPanel.add(rightPanel);

setVisible(true);
validate();
}//searchPanel2()
//--------------------------------------lend()--------------- ---------------------------------------

public void lendPanel()
{
clearResult();
myContentPanel.remove(rightPanel);
JPanel orderPanel = new JPanel();
lendPanel.setBackground(blue);
GridLayout grid = new GridLayout(13,2);
lendPanel.setLayout(grid);


lendNo = new JLabel ("LendNo:" , Label.LEFT);
lettingNo.setForeground(black);
date = new JLabel ("Date:" , Label.LEFT);
date.setForeground(black);
customerNo = new JLabel("CustomerNo:", Label.LEFT);
customerNo.setForeground(black);
price = new JLabel("Price:", Label.LEFT);
price.setForeground(black);
amount = new JLabel("Amount:", Label.LEFT);
amount.setForeground(black);
productNo = new JLabel("VideoNo:", Label.LEFT);
productNo.setForeground(black);

//Declare TextField

lendNoField = new TextField (14);
lendNoField.setBackground(grey);
dateField = new TextField (14);
dateField.setBackground(grey);
customerNoField = new TextField (14);
customerNoField.setBackground(grey);
priceField = new TextField (14);
priceField.setBackground(grey);
amountField = new TextField (14);
amountField.setBackground(grey);
videoNoField = new TextField (14);
vidoeNoField.setBackground(grey);

JPanel top1 = new JPanel(); //panels creates to hold the textfield, label and button
top1.setBackground(blue);
JPanel top2 = new JPanel();
top2.setBackground(blue);
JPanel top3 = new JPanel();
top3.setBackground(blue);
JPanel top4 = new JPanel();
top4.setBackground(blue);
JPanel top5 = new JPanel();
top5.setBackground(blue);
JPanel top6 = new JPanel();
top6.setBackground(blue);
JPanel top7 = new JPanel();
top7.setBackground(blue);
JPanel top8 = new JPanel();
top8.setBackground(blue);
JPanel top9 = new JPanel();
top9.setBackground(blue);
JPanel top10 = new JPanel();
top10.setBackground(blue);
JPanel top11 = new JPanel();
top11.setBackground(blue);
JPanel top12 = new JPanel();
top12.setBackground(blue);
JPanel top13 = new JPanel();
top13.setBackground(blue);


top1.add(lendNo); //textfield, label and button is placed on each panel
lendPanel.add(top1);
top2.add(lendNoField);
lendPanel.add(top2);
top3.add(date);
lendPanel.add(top3);
top4.add(dateField);
lendPanel.add(top4);
top5.add(customerNo);
lendPanel.add(top5);
top6.add(customerNoField);
lendPanel.add(top6);
top7.add(price);
lendPanel.add(top7);
top8.add(priceField);
lendPanel.add(top8);
top9.add(amount);
lendPanel.add(top9);
top10.add(amountField);
lendPanel.add(top10);
top11.add(videoNo);
lendPanel.add(top11);
top12.add(videoNoField);
lendPanel.add(top12);


JButton lend = new JButton("Add to list");
letting.setBackground(grey);
letting.addActionListener(new MyInnerButtonListenerClass());
top13.add(lend);
lettingPanel.add(top13);

JButton order = new JButton("Lend");
lend.setBackground(grey);
lend.addActionListener(new MyInnerButtonListenerClass());
top13.add(lend);
lendPanel.add(top13);
lendPanel.setBorder(BorderFactory.createTitledBorder("lendPanel"));

rightPanel = lendPanel;
myContentPanel.add(rightPanel);

setVisible(true);
validate();
}//lendPanel()

//--------------------------------------------insertPanel()---- --------------------------------------

public void insertPanel()
{
clearResult();
myContentPanel.remove(rightPanel);
JPanel insertPanel = new JPanel();
insertPanel.setBackground(blue);
GridLayout grid = new GridLayout(7,2);
insertPanel.setLayout(grid);


title = new JLabel ("Title:" ,Label.LEFT);
title.setForeground(black);
actor = new JLabel ("Actor:" ,Label.LEFT);
actor.setForeground(black);
genre = new JLabel ("Genre:" ,Label.LEFT);
genre.setForeground(black);
length = new JLabel ("Length:" ,Label.LEFT);
length.setForeground(black);
videoNo = new JLabel ("VideoNo:" ,Label.LEFT);
videoNo.setForeground(black);

//Declare TextField
titleField = new TextField (14);
titleField.setBackground(grey);
actorField = new TextField (14);
actorField.setBackground(grey);
genreField = new TextField (14);
genreField.setBackground(grey);
videoNoField = new TextField (14);
videoNoField.setBackground(grey);

JPanel top1 = new JPanel(); //panels are created to hold textfield, label and button
top1.setBackground(blue);
JPanel top2 = new JPanel();
top2.setBackground(blue);
JPanel top3 = new JPanel();
top3.setBackground(blue);
JPanel top4 = new JPanel();
top4.setBackground(blue);
JPanel top5 = new JPanel();
top5.setBackground(blue);
JPanel top6 = new JPanel();
top6.setBackground(blue);
JPanel top7 = new JPanel();
top7.setBackground(blue);
JPanel top8 = new JPanel();
top8.setBackground(blue);
JPanel top9 = new JPanel();
top9.setBackground(blue);
JPanel top10 = new JPanel();
top10.setBackground(blue);
JPanel top11 = new JPanel();
top11.setBackground(blue);


top1.add(title); //panels are created to hold textfield, label and button
insertPanel.add(top1);
top2.add(titleField);
insertPanel.add(top2);
top3.add(actor);
insertPanel.add(top3);
top4.add(actorField);
insertPanel.add(top4);
top5.add(genre);
insertPanel.add(top5);
top6.add(genreField);
insertPanel.add(top6);
top7.add(length);
insertPanel.add(top7);
top8.add(lengthField);
insertPanel.add(top8);
top9.add(videoNo);
insertPanel.add(top9);
top10.add(videoNoField);
insertPanel.add(top10);


JButton insert = new JButton("Insert");
insert.setBackground(grey);
insert.addActionListener(new MyInnerButtonListenerClass());
top11.add(insert);
insertPanel.add(top11);
insertPanel.setBorder(BorderFactory.createTitledBorder("insertPanel"));

rightPanel = insertPanel;
myContentPanel.add(rightPanel);

setVisible(true);
validate();

}//insertPanel()

//--------------------------------------------re movePanel()------------------------------------------

public void removePanel()
{
clearResult();
myContentPanel.remove(rightPanel);
JPanel removePanel = new JPanel();
removePanel.setBackground(blue);
GridLayout grid = new GridLayout(7,2);
removePanel.setLayout(grid);


title = new JLabel ("Title:" ,Label.LEFT);
title.setForeground(black);
actor = new JLabel ("Actor:" ,Label.LEFT);
actor.setForeground(black);
genre = new JLabel ("Genre:" ,Label.LEFT);
genre.setForeground(black);
length = new JLabel ("Length:" ,Label.LEFT);
length.setForeground(black);
videoNo = new JLabel ("VideoNo:" ,Label.LEFT);
videoNo.setForeground(black);

//Declare TextField
titleField = new TextField (14);
titleField.setBackground(grey);
actorField = new TextField (14);
actorField.setBackground(grey);
genreField = new TextField (14);
genreField.setBackground(grey);
videoNoField = new TextField (14);
videoNoField.setBackground(grey);

JPanel top1 = new JPanel(); //panels are created to hold textfield, label and button
top1.setBackground(blue);
JPanel top2 = new JPanel();
top2.setBackground(blue);
JPanel top3 = new JPanel();
top3.setBackground(blue);
JPanel top4 = new JPanel();
top4.setBackground(blue);
JPanel top5 = new JPanel();
top5.setBackground(blue);
JPanel top6 = new JPanel();
top6.setBackground(blue);
JPanel top7 = new JPanel();
top7.setBackground(blue);
JPanel top8 = new JPanel();
top8.setBackground(blue);
JPanel top9 = new JPanel();
top9.setBackground(blue);
JPanel top10 = new JPanel();
top10.setBackground(blue);
JPanel top11 = new JPanel();
top11.setBackground(blue);


top1.add(title); //panels are created to hold textfield, label and button
removePanel.add(top1);
top2.add(titleField);
removePanel.add(top2);
top3.add(actor);
removePanel.add(top3);
top4.add(actorField);
removePanel.add(top4);
top5.add(genre);
removePanel.add(top5);
top6.add(genreField);
removePanel.add(top6);
top7.add(length);
removePanel.add(top7);
top8.add(lengthField);
removePanel.add(top8);
top9.add(videoNo);
removePanel.add(top9);
top10.add(videoNoField);
removePanel.add(top10);


JButton remove = new JButton("Remove");
remove.setBackground(grey);
remove.addActionListener(new MyInnerButtonListenerClass());
top11.add(remove);
removePanel.add(top11);
removePanel.setBorder(BorderFactory.createTitledBorder("removePanel"));

rightPanel = removePanel;
myContentPanel.add(rightPanel);

setVisible(true);
validate();

}//removePanel()


//--------------------------------------insert( )-----------------------------------------------------

public void insert()
{
clearResult();
System.out.println("Du ?r nu i insert()");
String title = titleField.getText();
String aname = actorField.getText();
String genre = genreField.getText();
String length = lengthField.getText();
int videoNo = Integer.parseInt(videoNoField.getText());
String temp = database.connect();
System.out.println(temp);
database.insertVideo(title, aname, genre, length, videoNo);
resultView();
resultat = database.getResult();
System.out.println("Vi ?r nu i GUI: " + resultat);
if(resultat!=null){
view(resultat);
} else {
System.out.println("App app");
}
database.disconnect();

}//insert

//--------------------------------------remove()-------------------- ---------------------------------

public void remove()
{
clearResult();
System.out.println("Du ?r nu i remove()");
String title = titleField.getText();
String aname = actorField.getText();
String genre = genreField.getText();
String length = lengthField.getText();
int videoNo = Integer.parseInt(videoNoField.getText());
String temp = database.connect();
System.out.println(temp);
database.insertVideo(title, aname, genre, length, videoNo);
resultView();
resultat = database.getResult();
System.out.println("Vi ?r nu i GUI: " + resultat);
if(resultat!=null){
view(resultat);
} else {
System.out.println("App app");
}
database.disconnect();

}//remove

//------------------------------------------makeLend()-------------- -------------------------------

public void makeLend()
{
clearResult();
int lendNo = Integer.parseInt(lendNoField.getText());
int date = Integer.parseInt(dateField.getText());
int customerNo = Integer.parseInt(customerNoField.getText());
int price = Integer.parseInt(priceField.getText());
int videoNo = Integer.parseInt(videoNoField.getText());
int amount = Integer.parseInt(amountField.getText());
database.connect();
database.insertLend(lendNo, date, customerNo, price, videoNo, amount);
resultView();
result = database.getResult();
System.out.println("Vi ?r nu i GUIMake lend: " + result);
if(result!=null){
view(result);
} else {
System.out.println("Make lend");
}
database.disconnect();
}//makeOrder()

//-------------------------------------------statistics()---------------------- -------------------

public void statistics()
{
clearResult();
System.out.println("Vi ?r nu i statistics()");
String title = stField.getText();
System.out.println(title);
database.connect();
database.statistics(title);
resultView();
result = database.getResult();
System.out.println("Vi ?r nu i GUIMake letting: " + result);
if(result!=null){
view(result);
} else {
System.out.println("Statistics()");
}
database.disconnect();

}//statistics()
//--------------------------------------------searchTitle()---- ------------------------------------

public void searchTitle()
{
clearResult();
String title = searchField.getText();
database.connect();
database.searchTitle(title);
resultView();
result = database.getResult();
System.out.println("Vi ?r nu i GUI searchTitle(): " + resultat);
if(result!=null){
view(result);
} else {
System.out.println("searchTitle()");
}
database.disconnect();

}//searchAuthor()

//------------------------------------------searchStaff()--- -------------------

public void searchStaff()
{
clearResult();
String username = userNameField.getText();
String password = passwordField.getText();
database.connect();
database.searchCustomer(username, password);
resultView();
result = database.getResult();
System.out.println("Vi ?r nu i GUI searchStaff(): " + result);
if(result!=null){
view(result);
} else {
System.out.println("searchStaff()");
}
database.disconnect();


}//searchStaff()
//--------------------------------------------resultView()---- --------------------------------------------

public void resultView()
{
clearResult();
Vector temp = database.getVector();
String sresult= "";
for(int i=0; i<temp.size(); i++)
{
frameList.add(temp.elementAt(i).toString());
}//for

int count = frameList.getItemCount();
for(int i=0; i<count; i++)
{
System.out.println("Objekten i listan ?r: " + frameList.getItem(i));
}//for

}//resultView()

//---------------------------------------------view()--------- -------------------------------------

public void view(String result)
{
clearResult();
String temp= result;
frameList.add("Objektet i databasen ?r: " + temp);

}
//---------------------------------------------clearResult()--------- -------------------------------
public void clearResult()
{
frameList.removeAll();

}//clearResult()
//------------------------------------------------------------ ---------------------------------------
//Inner class Buttonlistner
class MyInnerButtonListenerClass implements ActionListener {

public MyInnerButtonListenerClass() {

}//ButtonListener()

public void actionPerformed (ActionEvent event) { //method which listen to the buttons
String buttonText = event.getActionCommand();
if(buttonText.equals("Search")) {
searchTitle();
}
if(buttonText.equals("Statistic")) {
statistics();
}
if(buttonText.equals("Search Staff")) {
searchStaff();
}
if(buttonText.equals("Lend")) {
makeLetting();
}
if(buttonText.equals("Insert")) {
insert();
}

}//actionPerformed() slut
}//class ButtonListener slut

//------------------------------------------------MenuListener----------- ------------------------

//inner class MenuListener
class MyInnerMenuListenerClass implements ActionListener{

public MyInnerMenuListenerClass(){

}

public void actionPerformed(ActionEvent event){ //method which listen to menu
String menuChoise = event.getActionCommand();

if(menuChoise.equals("Close")){
System.exit(0);
}

if(menuChoise.equals("Search")){
searchPanel();
}

if(menuChoise.equals("Search Staff")){
searchPanel2();
}
if(menuChoise.equals("Lend")){
lettingPanel();
}

if(menuChoise.equals("Insert Video")){
insertPanel();
}
if(menuChoise.equals("Remove Video")){
removePanel();
}
if(menuChoise.equals("Show statistics")){
stPanel();
}

if(menuChoise.equals("Disconnect")){
System.exit(0);
}

}//actionPerformed() ends
}//class MenuListener

//-----------------------------------------WindowListener-------- --------------------------------------

//inner class WindowListener
class WindowListener extends WindowAdapter
{

public void windowClosing(WindowEvent event){ //closes window
System.exit(0);
}//windowClosing
}//class WindowListener

//------------------------------------------class Gui-------------------------------------------------------
}//class Gui


Fawad Shah

Posts: 9
Nickname: smartarse
Registered: May, 2003

Re: Please, help me with login Posted: May 24, 2003 10:23 AM
Reply to this message Reply
Ok! What I can understand from your code is that you want to check the details entered by the user and then open the relative GUI with the relative details.
Best way to go about is after your main GUI, create two classes: Manager and Employee. Create a GUI interface for the authentication window you want to appear, which asks for something like username and password. When details are entered and "OK" is pressed, the GUI will check on what was entered against your fixed info or a database and open the relative class.
Give some more details and I will send you some code.

Cheers and best of luck!

Jessica

Posts: 3
Nickname: mabel
Registered: May, 2003

Re: Please, help me with login Posted: May 26, 2003 10:39 AM
Reply to this message Reply
Thanks!
But I'm not sure what you mean.
When I run the application and the user e.g. tries to add a video, I would like a pop-up menu to appear that asks for username and password.
I have created two tables in the database, one manager and one employee. When the user types in username and password, I would like to have a method that check the username and password against the database. I would really appreciate some code.

Fawad Shah

Posts: 9
Nickname: smartarse
Registered: May, 2003

Re: Please, help me with login Posted: May 26, 2003 8:58 PM
Reply to this message Reply
ok....to check the validity of the password you need to create a connection to the database. The design of your GUI could be changed to as follows: a main Frame which has two login options, Employee and Manager. When either one is clicked, a popup window opens to ask for the username and password, which are checked against their respective tables in the database. If true, it will open a respective GUI for the user with the functionality added as you want.

Alternatively as in your case, u can have one table called user which has a component called "Type". This can be either Employee or Manager, for all usernames and password. The system will check the value of this column and return for you the correct component.

the following code should help, although you will need to change the variables around.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;

public class DBConn
{
Employee e; //GUI for the employee
Manager m; //GUI for the manager

private String DSN="nameofdatabase", url = "jdbc:odbc:Library;" + DSN, user="", password="";
private Connection con;
private Statement stmt;

public void DBConn()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}

public void verify(String un, String pw)
{
e = null;
m = null;
String user;
String pass;

user = un;
pass = pw;

String query = "SELECT Type FROM User WHERE Id = '" + user + "' AND Password = '" + pass + "'";

try
{
ResultSet rs = stmt.executeQuery(query);
boolean gIt = rs.next();

if(gIt)
{
String type = rs.getString(1);

if(type=="Manager")
{
m = new Manager();
}
else if(type=="Employee")
{
e = new Employee();
}
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect User details");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.toString());
}
}

public void terminate()
{
stmt.Close();
con.Close();
}
}
In your popup window, when ok is pressed, add the following in the code for the ActionEvent.

String username = usernametextfield.getText();
String password = passwordtextfield.getText();
DBConn db = new DBConn();
db.initiaize();
db.verify(username, password);
db.terminate();

Hope it helps. Yell back if any problems.

Fawad Shah

Posts: 9
Nickname: smartarse
Registered: May, 2003

Re: Please, help me with login Posted: May 26, 2003 9:03 PM
Reply to this message Reply
Also remember, your database needs to be added to the ODBC Data Sources in Windows. This can be accessed from Settings-->Control Panel-->ODBC Data Sources or search for it.
In the window, go to the section called System DSN-->Press Add-->Select Microsoft Access Driver-->Data Source Name must be the same given to DSN in the DBConn class-->then select your database-->press ok---press ok.

Cheers.

Jessica

Posts: 3
Nickname: mabel
Registered: May, 2003

Re: Please, help me with login Posted: May 29, 2003 12:39 PM
Reply to this message Reply
Hello again!

Thank you very much for all your help! :-)
If I create two gui, do I have to create two pop-up menus as well? Or how do I code the pop-up?
I'm also wondering how I create DDL in Access, I only find the design view and the vizard.?
When I tried to connect the database to odbc it didnt work I got the message "Invalid directory or file path", do you no whats wrong?

I'm very sorry if I'm annoying you :-(
Thanks again!

Fawad Shah

Posts: 9
Nickname: smartarse
Registered: May, 2003

Re: Please, help me with login Posted: May 30, 2003 12:57 AM
Reply to this message Reply
no worries.....
The understanding I got was that the Manager's GUI will have more options and functionality than the Employees one. So you can create two seperate GUIs for each with the relevant functionality.
The first GUI which should be called in your main method should the main one, which will have two buttons only, one for Employee and the other for Manager.
When the user selects anyone, the popup window for username and password is called up. You just need to code this once and call it from both buttons. e.g.
Popupwindow pw = new Popupwindow();
In the popup window, you will have a button which the user will press to submit the details of username and password. In the Action Performed method for this button, create an object of the class DBConn.i.e.
DBConn db = new DBConn();
db.verify(username, password);
db.terminate();
This will pass the details to the DBConn class and verify the details. Then it will open the correct GUI window, eith Employee or Manager.
As far as Access is concerned, once you create you file, save it in the same folder as you .java files. Then open up ODBC and go the section for System DSN. Here click ADD, then select the option Microsoft Access Driver, and click Ok. This will bring you to a setup window. On the top, add a relevant name for the database like Video. Then press SELECT in the section for Database and select your file. Then click OK and Ok again to close the application. In the DBConn file that you create, write the name of the DSN that you gave in the setup like Video. This is put in for DSN in the code that I sent you, right up on top. This will allow you access the database without providing the entire path.

If you still have problems, send me a zipped copy of all your code on geegee_pk@yahoo.com, and I will try to sort it out for you.

Cheers.

Flat View: This topic has 6 replies on 1 page
Topic: Ways to make java instant Previous Topic   Next Topic Topic: Swing application - pizzeria

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use