aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Implementing scanner into a simple login page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Implementing scanner into a simple login page" Watch "Implementing scanner into a simple login page" New topic
Author

Implementing scanner into a simple login page

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Basically I'm creating this simple login page that consist of couple of labels, two text boxes for inputs and two buttons but a couple of questions puzzle me. By using the if/else statement, it only limits the validation of 2 specific users for the program, thus I would like to create a scanner class to read the inputs from the database (supposingly a notepad) and compare it with the input from the two text boxes. However to no avail, I could neither find the way to implement the scanner in the existing codes, or save the input results (from the notepad) of the scanner to compare with the input from the text boxes. Included below are the original codes, and I'm hoping that you guys out there can come out with some suggestions or even better some improvement to the codes. Thanks in advance!


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

public class Login extends JFrame implements ActionListener
{
JButton login, exit;
JPanel panel1, panel2, panel3;
JLabel label1, label2, enterlabel;
final JTextField text1, text2;

Login() // constructor
{
enterlabel = new JLabel("Please key in usernmae and password.");

label1 = new JLabel();
label1.setText(" Username :");
text1 = new JTextField(10);

label2 = new JLabel();
label2.setText(" Password :");
text2 = new JPasswordField(10);

login = new JButton("Login");
exit = new JButton("Exit");


panel1 = new JPanel();
panel2 = new JPanel(new GridLayout(3,2));
panel3 = new JPanel();

panel1.add(enterlabel);

panel2.add(label1);
panel2.add(text1);
panel2.add(label2);
panel2.add(text2);

panel3.add(login);
panel3.add(exit);

add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);

login.addActionListener(this);

ExitButtonHandler ehandler = new ExitButtonHandler(); // instantiate a handler
exit.addActionListener(ehandler); // register the handler

setTitle("Welcome To CIMB - Login Page");

}

public void actionPerformed(ActionEvent o)
{
String value1 = text1.getText();
String value2 = text2.getText();

if(value1.equals("chee yin") && value2.equals("123")||value1.equals("luen hong") && value2.equals("hong987"))
{
NextPage page = new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome :"+ value1);
page.getContentPane().add(label);
}
else {
JOptionPane.showMessageDialog(this, "Incorrect username or password. Please re-enter username and password", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}

// inner classes for the button event handlers

class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame = new Login();
frame.setSize(350, 150);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}

// end of inner class
Ravin Psinh
Greenhorn

Joined: Feb 04, 2008
Posts: 8
if you wish to do login check with database entry than its easier . in database reading each row will be checked for user name and password. \

you can also do it with notepad where you will have to read the file and make some distinction among different users.
for this you can put a different string between the username and password and each user record starts with new line.

so your entry in notpad will be like

user1$_$_$password1
user2$_$_$password2
.
.
.
.
usern$_$_$passwordn

for checking login name and password what you have to do is to open a file reader and read line by line record and deleminate the user name and password with StringTokenizer till the match is found or the file end.
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
well is there an example i could use? i've not bumped into anything using database or using readline function for comparison
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Luen, please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
owh..sorry, did not know about it.

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23


This part of the code has to change, as the above forementioned scenario on previous post. I'm sorry as I do not know how does the posting format works here.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

This is obviously mostly Swing related, so moving.
Ravin Psinh
Greenhorn

Joined: Feb 04, 2008
Posts: 8
here is code for database usage .......
i am writing for simply MS-ACCESS.......




i hope this will be helpfull
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Ravindra,

Please read your private messages.

Thank you,

Rob
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23


According to the second paragraph after i had given the data sources name as 'login' then select the database, is this found in the System Database group?

Besides that, if you don't mind can you tell me where should i store the data source that i had created??

The ms-access file that i had created name as login...do i need to transfer to the file where i store my Login code?

besides that, here's 1 error after I follow all the instruction


Login.java:63: cannot find symbol
symbol : variable DriveManager
location : class Login
Connection con = DriveManager.getConnection("jdbcdbc:JdbcOdbcDriver");

^

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Check your imports.
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Thank you so much, I was able to compile without any error. However the execution triggered an exception problem:

Following exception occured during login checkjava.lang.ClassNotFoundException:sun.jdbc.odbc.login

Can someone explain what does the error means?

Ravin Psinh
Greenhorn

Joined: Feb 04, 2008
Posts: 8
sorry luen i made some mistake in code..........
please correct it as follows......

replace the code which resembles like this.....


just change these two line in your code........
and one thing more..........

name of your ms-access table must be logTable as we mentioned in sql_query string............
name of data source name .......not the database name(ms-access file).......... should be login.......
and its just a entry ....... you can store or save your database anywhere in your system.........
the data source name will help the program to find it....................... because using this we are locating the database file.........
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Dear Ravin, after implementing the corrections for the code, yes indeed I do not receive an error regarding the class anymore, however I received this one:


D:\Documents and Settings\Owner\Desktop>javac login.java

D:\Documents and Settings\Owner\Desktop>java LoginDemo
Following exception occured during login checkjava.sql.SQLException: [Microsoft]
[ODBC Driver Manager] Data source name not found and no default driver specified


I'm sorry for the trouble as I'm not very familiar with databases. What exactly is a "data source name"?

I've named my database file, login.mdb and my table logTable. The layout is as below:

user password
cheeyin 123
hong 456
waijin 789



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Do you have an ODBC link called "login"?

If you want to use an Access file, you need to change your connection string:
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Rob >> Hmm, I know this might be a stupid question, but what is ODBC link?

I tried inserting the code you provided, however alot of errors popped up; ';' , ')' expected, as well as illegal start of expression for the part (*.mdb) ; contributed 8-9 errors in total. So I'm wondering if I put it in right. Heres the code:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

It's supposed to be a String

You may want to read http://support.microsoft.com/kb/110093 and http://en.wikipedia.org/wiki/Odbc
You can access it through Control Panel -> Administrative Tools -> Data Sources (ODBC). You may want to prefer System DSNs over User DSNs because the latter only work if you're logged in.
Ravin Psinh
Greenhorn

Joined: Feb 04, 2008
Posts: 8
ok leun........ now it 100%sure the code is working........only one thing we have to make...... its not in the code........its on the
system...........

what you have to do is.....

1>open "Conrol Panel"........
2>open "Administrative Tools"
3>open "Data Sources (ODBC)"
4>click on "Add..." button
5>select a driver......"Microsoft Access Driver(*.mdb)"......from the list of driver.......
6>here in the first input box enter the ........"Data Source Name"....as "login"..... this is what program was unable to find....
7>here on the same dialog box....... you have to Select DataBase............ just locate the your database file..... in which you created the table..........
8>press OK button....................

thats all......about the creating dsn........ its helps the program to find the actual database........ its a bridge between Database and the Java Program........ via which our data will travel......... so the user and password strings...... in order to complete the journey one must specify connect the bridge properly...... that is what we have done here.........

now run the program........ if you still find problem then please post here........... we have to solve it today....

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Dear Rob and Ravin,

I'm uber grateful for your enthusiasm and anticipation in solving this. I've a feeling that we're really this close =.="

I've no error during compilation after fixing the connection string and setting up the dsn. But this did popped up during execution :


checkjava.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (comma) in query expression 'user= 'cheeyin' , password = '123'


When i replaced the comma with an 'and', what i get is this :

[b]checkjava.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

I don't understand how would data type mismatch error appear, when both the values are declared as strings earlier on.
I've been thinking, if it isn't the code or the system problem anymore, would it then be the database file itself? Is there anything I should take note in the database file?

However, an empty field, allows the system to execute properly, in this case, display error message as below:

Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
i checked my database done by my group member. The field 'password', data type has been set as a number =.=" i've changed it to text, and the program worked lol..

question is, how can i convert the 'value2' which is the password, into int data type? can i use the parseInt codes?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

If you're sure it's an integer, sure.
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Ok, so i know we can read the information from the database file. Is it also possible to use 'alter' and 'update' just like in sql?

For example, lets say it involves calculation, a bank account for example, if transactions are done, surely the current balance would be updated right?

Even if the current balance can be updated, can it also be aligned with the correct users?
Luen Hong Wuan
Greenhorn

Joined: May 30, 2009
Posts: 23
Is it also possible to implement a max number of tries for dis kind of login form? I've tried using do-while loop, but instead of letting a user try 3 times and displaying an error message for each time an invalid username/password is presented, it displays the error message 3 times and then closes the system. Can you guys check my loop to see if i did anything wrong?

Ravin Psinh
Greenhorn

Joined: Feb 04, 2008
Posts: 8
leun, it may be possible because you are declaring ' i ' in the actionPerformed()........ i think you should declared it as static at the class level......... because each time you press button the event is generated and new ' i ' values are stored.....

yes you can perform operation that sql does....... here also.......... but with minor changes..........
here in our program accessing information from database returns the ResultSet object on executeQuery() method call on Statement object...............

for update, insert, delete,create ........ these all queries make changes to database so what we have to use is method executeUpdate() on Statement object......... it returns int......... (for example if you run update query than it returns the number of rows updated)..............

as you asked about updating balance of particular user........ yes we can....... we have to use WHERE clause in sql query.......
for example........

UPDATE loginTable SET balance=2000 WHERE user='ravin' AND password='1234';

for learning more about sql queries you can read w3shools SQL tutorial...... it will help faster learning.......

thanks.........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing scanner into a simple login page