Anda di halaman 1dari 4

Create A Login Form Using NetBeans IDE

How to create a login form(Java GUI Programming) through NetBeans IDE or how
to use JPassword?
We will be using a text field to get the username and a password field to get the
password.

1st Method
STEP-1 First of all design your form by placing the required components i.e label,
text-field, password field and a button to perform the action.

Here, I have used a panel with titled border to make design look better.
STEP-2 Double-click on the 'Log In' button or right
clickEventsActionactionPerformed
STEP-3 On the Log In button's action performed event, write the following piece of
code
String user=jTextField1.getText();
String pwd= new String (jPasswordField1.getPassword());
if (user.equals("yourusername") && pwd.equals("yourpassword"))
new Home().setVisible(true);
Explanation of code- We will accept the password from the password field and store
it in the variable pwd. Similarly, we will store the username in the variable user. Now,
we can compare the username and password we have received to the real
username and password using the if command.
Now if the username and password is correct your Home page(Java form) could be
visible or you can perform any other action.
If there is only few IDs i.e only few combinations of username and password you can
use If-else-if but if the IDs are more in number you have to maintain a database of
usernames and their corresponding password. In such a case you can use a DBMS

eg- MySQL. After that you have to create a connection between your database and
your Java application.

2nd Method (Using Java Database Connectivity)


STEP-1 Same as above.
STEP-2 Add 'MySql JDBC Driver' Library in your project(Right click on project icon
Properties Libraries Add Library MySql JDBC Driver)
STEP-3 Under your package and before class starts write the following codes in the
source tab.
import java.sql.*;
import javax.swing.JOptionPane;
STEP-3 Now on the Login button's actionPerformed event
String sql="Select * from Table_name";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdata
basename","yourmysqlid","yourmysqlpassword");
/*As we are creating a connection on a local computer we will
write the url as jdbc:mysql://localhost:3306 */
Statement stmt=con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
}
catch (Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
STEP-4 Now we have to match our inputs of username and password with that in
the database. We will use while loop for this purpose. Write the following codes
under the line ResultSet rs = stmt.executeQuery(sql); and before the
'}'
String user=jTextField1.getText();
String pwd= new String (jPasswordField1.getPassword());
while(rs.next()) {
String uname=rs.getString("Username");
//Username is the coloumn name in the database table
String password=rs.getString("Password");
if ((user.equals(uname)) && (pwd.equals(password)))
new Home().setVisible(true);
}
The loop will execute the number of times equal to the total of rows in the table. The
Home form will open when the right combination of username and password is

found.

Tips regarding Log In form


1) There are always chances that a user will enter a wrong password and
username combination, in that case you should show a message to the user
regarding the same. You can use a JOptionPane for this purpose.

a- For 1st Method


You can put a else statement for displaying this error message.
else {
JOptionPane.showMessageDialog(this, "Incorrect Username or
Password!");
}

b- For 2nd Method


There maybe other methods but I thought this one to be the simplest one.
Before the while loop starts declare a integer variable and initialize it with the
value 0 and later in the loop we can increase its value if a right match is found.
Here's the complete code that I used.
int tmp=0;
while(rs.next()) {
String uname=rs.getString("Username");
String password=rs.getString("Password");
if ((user.equals(uname)) && (pwd.equals(password))) {
new Home().setVisible(true);
tmp++;
}
}
if (tmp==0) {
JOptionPane.showMessageDialog(null, "Username and Password not in
database!");
}

2) If you are opening a new form after a login is successful, there will be two forms
on the screen as the login form will not close. So to close the login form just
write dispose(); below the line new Home().setVisible(true); This will
close the current form(i.e login) and there will be only one form on the screen.

3) Important properties for Password field.


Method
setBackground

Description
Sets the background color of
the password field.

Example
jPasswordField1.setBackground(new
java.awt.Color(255, 255, 204));

setFont

Sets the font style and size for


the text of the password field.

jPasswordField1.setFont(new
java.awt.Font("Times New Roman", 1, 12));

setEnabled

The enabled state of


component.True if enabled else jPasswordField1.setEnabled(false);
false (Boolean)

setEchoChar

This method determines which


character will replace the text jPasswordField1.setEchoChar('*');
in display.

Anda mungkin juga menyukai