0% found this document useful (0 votes)
26 views24 pages

Java Registration Form with Swing

This document provides instructions for creating a registration form application in Java using the NetBeans IDE. It includes: 1) Importing necessary packages and extending JFrame to create registration form components like labels, text fields, buttons. 2) Adding an action listener to handle form submission and validation, including checking if passwords match before inserting data into an Oracle database table using JDBC. 3) Steps to set up the project in NetBeans, add the ojdbc.jar file, and run the application to test registration form functionality like validation and data insertion.

Uploaded by

vru23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views24 pages

Java Registration Form with Swing

This document provides instructions for creating a registration form application in Java using the NetBeans IDE. It includes: 1) Importing necessary packages and extending JFrame to create registration form components like labels, text fields, buttons. 2) Adding an action listener to handle form submission and validation, including checking if passwords match before inserting data into an Oracle database table using JDBC. 3) Steps to set up the project in NetBeans, add the ojdbc.jar file, and run the application to test registration form functionality like validation and data insertion.

Uploaded by

vru23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction

This article explains how to create a Registration Form in a Windows Forms application using
Swing in Java. The NetBeans IDE is used to create this application.

Registration Form in Windows Forms application

For creating this application, we need the following files:

1. Java file
2. SQL file
3. [Link] file
4. NetBeans IDE

1. Java File

This Java file is necessary for writing the code. In this file we use Swing components to create a
registration form with password validation.

What we can do

A. First we can import several packages.

[Link].*;
[Link].*;
[Link].*;
[Link].*;

The Swing package is used for the Swing components. All Swing components are defined within
this package. The AWT package provides an event handling mechanism, in other words it deals
with events like "Button Click". The SQL package creates the JDBC connection.

B. Extends the JFrame components and implements the ActionListener.

Syntax

class EmpSearchApp extends JFrame implements ActionListener

C. Now Declare following components

JLabel l1, l2, l3, l4, l5, l6, l7, l8;


JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2;
JPasswordField p1, p2;

D. Now declare Frame components in a default constructor.

Syntax
RegistrationFormApp()
{
......
......
try{
//JDBC CODE
}Catch(Exception ex)
{
[Link](ex)
}
......
}

Note: In the dotted part we declare and add various components of Swing; in this part the JDBC
code is also used to save user records in a database named "reg". The full code I'll show you
below, but here I'll only summarize for you what I can do.

D. Add an ActionListener for the button clicked event

public void actionPerformed(ActionEvent e) {


........
........
}

Note: Since we have multiple buttons we can use "if ([Link]() == buttonName)".

E. Create main method and run the constructor

Finally, create a main method and run the constructor as in the following:

public static void main(String arr[]) {


new RegistrationFormApp();
}

2. [Link] file

This JAR file provides a way to set up a Java connection with an Oracle Database. Since the JDBC
connection is provided by the Oracle Server vendor, we need to import this JAR file to our library
folder.

3. [Link] table

For fetching records we need a database table; for that we create an "emp" table in our "sandeep"
database.

Syntax

[Link]
create table emp
(
name varchar2(30), email varchar2(40),
pass varchar2(30), count varchar2(30),
state varchar2(30), phone number(15)
);

Now let's start creating this app. Use the following procedure to do that in the NetBeans IDE.

Step 1

Open the NetBeans IDE.

Step 2

Choose "Java" -> "Java application" as shown below


Step 3

Type your project name as "RegistrationFormApp" as in the following and click on "finish".
Step 4

Create a new Java Class "Registration" and write the following there.

[Link]

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

public class Registration extends JFrame implements ActionListener


{
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2;
JPasswordField p1, p2;

Registration()
{
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Registration Form in Java");

l1 = new JLabel("Registration Form in Windows Form:");


[Link]([Link]);
[Link](new Font("Serif", [Link], 20));

l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();

btn1 = new JButton("Submit");


btn2 = new JButton("Clear");

[Link](this);
[Link](this);

[Link](100, 30, 400, 30);


[Link](80, 70, 200, 30);
[Link](80, 110, 200, 30);
[Link](80, 150, 200, 30);
[Link](80, 190, 200, 30);
[Link](80, 230, 200, 30);
[Link](80, 270, 200, 30);
[Link](80, 310, 200, 30);
[Link](300, 70, 200, 30);
[Link](300, 110, 200, 30);
[Link](300, 150, 200, 30);
[Link](300, 190, 200, 30);
[Link](300, 230, 200, 30);
[Link](300, 270, 200, 30);
[Link](300, 310, 200, 30);
[Link](50, 350, 100, 30);
[Link](170, 350, 100, 30);

add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
}

public void actionPerformed(ActionEvent e)


{
if ([Link]() == btn1)
{
int x = 0;
String s1 = [Link]();
String s2 = [Link]();

char[] s3 = [Link]();
char[] s4 = [Link]();
String s8 = new String(s3);
String s9 = new String(s4);

String s5 = [Link]();
String s6 = [Link]();
String s7 = [Link]();
if ([Link](s9))
{
try
{
[Link]("[Link]");
Connection con =
[Link]("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");
PreparedStatement ps = [Link]("insert into reg values(?,?,?,?,?,?)");
[Link](1, s1);
[Link](2, s2);
[Link](3, s8);
[Link](4, s5);
[Link](5, s6);
[Link](6, s7);
ResultSet rs = [Link]();
x++;
if (x > 0)
{
[Link](btn1, "Data Saved Successfully");
}
}
catch (Exception ex)
{
[Link](ex);
}
}
else
{
[Link](btn1, "Password Does Not Match");
}
}
else
{
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
}
}
public static void main(String args[])
{
new Registration();
}
}

Note

You need to add a JAR file named "[Link]" to set up the database connection.

Step 5

Now our application is ready to run.

Right-click on the project menu then select "Run". The following output will be generated.
Step 6

Now we provide a password validation in this application so first check for that. For checking
password validation we provide an incorrect password first, as in the following figure.
Step 7

We have used a "clear" button so now check whether its working or not!
After clicking on the "clear" button the contents will be cleared as in the following.
Step 8

Now provide a correct entity and clicked on submit.


Step 9

If you want to be sure that the data was saved in the database

Then open the "Oracle Home Page" and login with your "username" and "password" then open
"Object Explorer" then open the "reg" table; the following data exists over there.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class RegistrationFormDesign {
JLabel
label1,label2,label3,label4,label5,label6,label7,label8,label9,label10,label11,label12,s1,s2,s3
;
JPanel panel;
JFrame jf;
JButton register;
JTextField
textfield1,textfield2,textfield3,textfield4a,textfield4b,textfield4c,textfield5,textfield6,textfield
7,textfield8,textfield9;
JPasswordField passwordfield1;
JRadioButton radiobutton2,radiobutton3;
String
name,gender,dob1,dob2,dob,dobb,password,mobileNumber,email,area,state,nationality,se
lectedState;
JComboBox combobox1;
Connection con1;
Statement st1;
int index,count;
String states[] = new String[50];
public RegistrationFormDesign()
{
initComponents();
event();
}
public void initComponents() {
jf=new [Link]("Registration Form");
panel=new [Link]();
[Link](panel);
[Link](new Color(191,239,255));
[Link](null);
[Link](970,700);
[Link]();
label1=new [Link]("Registration Form");
[Link](new Font("Dialog", [Link], 24));
[Link](300,20,400,40);
[Link](label1);
label2=new [Link]("Name");
[Link](new Font("Dialog", [Link], 20));
[Link](100,80,150,40);
[Link](label2);
label3=new [Link]("Gender");
[Link](new Font("Dialog", [Link], 20));
[Link](100,140,150,40);
[Link](label3);
label4=new [Link]("Password");
[Link](new Font("Dialog", [Link], 20));
[Link](100,200,150,40);
[Link](label4);
label5=new [Link]("Date Of Birth");
[Link](new Font("Dialog", [Link], 20));
[Link](100,260,150,40);
[Link](label5);
label6=new [Link]("Mobile Number");
[Link](new Font("Dialog", [Link], 20));
[Link](100,320,150,40);
[Link](label6);
label7=new [Link]("E-mail");
[Link](new Font("Dialog", [Link], 20));
[Link](100,380,150,40);
[Link](label7);
label8=new [Link]("Area");
[Link](new Font("Dialog", [Link], 20));
[Link](100,440,150,40);
[Link](label8);
label9=new [Link]("State");
[Link](new Font("Dialog", [Link], 20));
[Link](100,500,150,40);
[Link](label9);
textfield1=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,80,180,30);
[Link](textfield1);
label10=new [Link]("Male");
[Link](new Font("Dialog", [Link], 20));
[Link](350,140,60,40);
[Link](label10);
label11=new [Link]("Female");
[Link](new Font("Dialog", [Link], 20));
[Link](460,140,100,40);
[Link](label11);
label12=new [Link]("Nationality");
[Link](new Font("Dialog", [Link], 20));
[Link](100,560,150,40);
[Link](label12);
radiobutton2=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](420,140,20,30);
[Link](radiobutton2);
radiobutton3=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](570,140,20,30);
[Link](radiobutton3);
ButtonGroup jb = new ButtonGroup();
[Link](radiobutton2);
[Link](radiobutton3);
textfield3=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,200,180,30);
[Link](textfield3);
textfield4a=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,260,40,30);
[Link](textfield4a);
s1=new [Link]("/");
[Link](new Font("Dialog", [Link], 16));
[Link](400,260,20,30);
[Link](s1);
textfield4b=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](430,260,40,30);
[Link](textfield4b);
s2=new [Link]("/");
[Link](new Font("Dialog", [Link], 16));
[Link](480,260,20,30);
[Link](s2);
textfield4c=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](510,260,100,30);
[Link](textfield4c);
s3=new [Link]("DD/MM/YYYY");
[Link](new Font("Dialog", [Link], 16));
[Link](620,260,150,30);
[Link](s3);
textfield5=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,320,180,30);
[Link](textfield5);
textfield6=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,380,180,30);
[Link](textfield6);
textfield7=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,440,180,30);
[Link](textfield7);
textfield8=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,500,180,30);
//[Link](textfield8);
textfield9=new [Link]();
[Link](new Font("Dialog", [Link], 16));
[Link](350,560,180,30);
[Link](textfield9);
register = new [Link]("Register");
[Link](new Font("Dialog", [Link], 16));
[Link](250,620,100,30);
[Link](register);
combobox1 = new JComboBox();
[Link](350,500,180,30);
[Link](new Font("Dialog",[Link],18));
[Link](combobox1);
states[0] = "Andra Pradesh";
states[1] = "Arunachal Pradesh";
states[2] = "Assam";
states[3] = "Bihar";
states[4] = "Chhattisgarh";
states[5] = "Goa";
states[6] = "Gujarat";
states[7] = "Haryana";
states[8] = "Himachal Pradesh";
states[9] = "Jammu and Kashmir";
states[10] = "Jharkhand";
states[11] = "Karnataka";
states[12] = "Kerala";
states[13] = "Madya Pradesh";
states[14] = "Maharashtra";
states[15] = "Manipur";
states[16] = "Meghalaya";
states[17] = "Mizoram";
states[18] = "Nagaland";
states[19] = "Orissa";
states[20] = "Punjab";
states[21] = "Rajasthan";
states[22] = "Sikkim";
states[23] = "Tamil Nadu";
states[24] = "Tripura";
states[25] = "Uttaranchal";
states[26] = "Uttar Pradesh";
states[27] = "West Bengal";
states[28] = "Andaman and Nicobar Islands";
states[29] = "Chandigarh";
states[30] = "Dadar and Nagar Haveli";
states[31] = "Daman and Diu";
states[32] = "Delhi";
states[33] = "Lakshadeep";
states[34] = "Pondicherry";
for (int j= 0 ; j< 35; j++) { [Link](states[j]); } gender = ""; } public void event()
{ [Link](new KeyAdapter() { public void keyTyped(KeyEvent e) { char c
= [Link](); String sag=[Link](); if (!(((c >= 'a')||(c >= 'A')) && ((c <= 'z')||(c
<= 'Z')) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {
[Link](); } } }); [Link](new KeyAdapter() { public void
keyTyped(KeyEvent e) { char c = [Link](); if (!((c >= '0') && (c <= '9') || (c ==
KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) { // getToolkit().beep();
[Link](); } } }); [Link](new KeyAdapter() { public void
keyTyped(KeyEvent e) { char c = [Link](); if (!((c >= '0') && (c <= '9') || (c ==
KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) { // getToolkit().beep();
[Link](); } } }); [Link](new KeyAdapter() { public void
keyTyped(KeyEvent e) { char c = [Link](); if (!((c >= '0') && (c <= '9') || (c ==
KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) { // getToolkit().beep();
[Link](); } } }); [Link](new KeyAdapter() { public void
keyTyped(KeyEvent e) { char c = [Link](); if (!((c >= '0') && (c <= '9') || (c ==
KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) { // getToolkit().beep();
[Link](); } } }); [Link](new KeyAdapter() { public void
keyTyped(KeyEvent e) { char c = [Link](); String sag=[Link](); if (!(((c >=
'a')||(c >= 'A')) && ((c <= 'z')||(c <= 'Z')) || (c == KeyEvent.VK_BACK_SPACE) || (c ==
KeyEvent.VK_DELETE))) { [Link](); } } }); [Link](new KeyAdapter()
{ public void keyTyped(KeyEvent e) { char c = [Link](); String
sag=[Link](); if (!(((c >= 'a')||(c >= 'A')) && ((c <= 'z')||(c <= 'Z')) || (c ==
KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {
[Link]();
}
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
getUserInformation();
validation();
registerUserData();
}
});
[Link](new ActionListener() {
public void actionPerformed(ActionEvent event)
{
Object selectedStateobj = [Link]();
selectedState = [Link](selectedStateobj);
index = [Link]();
}
});
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
gender = "Male";
}
});
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
gender = "Female";
}
});
}
public void getUserInformation() {
name = [Link]();
password = [Link]();
dob = [Link]();
dob1 = [Link]();
dob2 = [Link]();
dobb = dob + "/" + dob1 + "/" + dob2;
mobileNumber = [Link]();
email = [Link]();
area = [Link]();
state = selectedState;
nationality = [Link]();
}
public void validation() {
count = 0;
if([Link](""))
{
[Link](null,"Enter Name");
}
else if([Link](""))
{
[Link](null,"Enter Father Name");
}
else if([Link](""))
{
[Link](null,"Enter Date");
}
else if([Link](""))
{
[Link](null,"Enter Month");
}
else if([Link](""))
{
[Link](null,"Enter Year");
}
else if([Link](""))
{
[Link](null,"Enter Mobile Number");
}
else if([Link](""))
{
[Link](null,"Enter E-Mail address");
}
else if([Link](""))
{
[Link](null,"Enter Area");
}
else if([Link](""))
{
[Link](null,"Select State");
}
else if([Link](""))
{
[Link](null,"Enter Nationality");
}
else if([Link](""))
{
[Link](null,"Select gender");
}
else {
if(([Link](dob2)<=2010) && ([Link](dob2)>=1900))
{
if(([Link](dob1)<=12) && ([Link](dob1)>0))
{
if(([Link](dob1)==1) || ([Link](dob1)==3) || ([Link](dob1)==5) ||
([Link](dob1)==7) || ([Link](dob1)==8) || ([Link](dob1)==10) ||
([Link](dob1)==12))
{
if(([Link](dob)<=31) && ([Link](dob)>=1))
{
dobb=dob+"/"+dob1+"/"+dob2;
if([Link]("@") && [Link](".com")) {
if([Link]()==10) {
count++;
}
else {
[Link](null,"Enter 10-digit moblie number");
}
}
else {
[Link](null,"Enter valid mail ID");
}
}
else
{
[Link](null,"Invalid Date");
}
}
else if(([Link](dob1)==4) || ([Link](dob1)==6) ||
([Link](dob1)==9) || ([Link](dob1)==11))
{
if(([Link](dob)<=30) && ([Link](dob)>=1))
{
dobb=dob+"/"+dob1+"/"+dob2;
if([Link]("@") && [Link](".com")) {
if([Link]()==10) {
count++;
}
else {
[Link](null,"Enter 10-digit moblie number");
}
}
else {
[Link](null,"Enter valid mail ID");
}
}
else
{
[Link](null,"Invalid Date");
}
}
else
{
if((([Link](dob2)%100)==0) || (([Link](dob2)%4)==0))
{
if(([Link](dob)<=29) && ([Link](dob)>=1))
{
dobb=dob+"/"+dob1+"/"+dob2;
}
else
{
[Link](null,"Invalid Date");
}
}
else
{
if(([Link](dob)<=28) && ([Link](dob)>=1))
{
dobb=dob+"/"+dob1+"/"+dob2;
}
else
{
[Link](null,"Invalid Date");
}
}
}
}
else
{
[Link](null,"Enter month between 1 and 12");
}
}
else
{
[Link](null,"Enter year between 1900 and 2010");
}
}
}
public void registerUserData() {
try {
if(count==1) {
[Link]("[Link]");
con1 = [Link]("jdbc:odbc:user");
st1 = [Link]();
[Link]("INSERT INTO User_Details
(Name,Gender,Password,DOB,Mobile_Number,Email,Area,State,Nationality,StateIndex)
VALUES('"+name+"','"+gender+"','"+password+"','"+dobb+"','"+mobileNumber+"','"+email
+"','"+area+"','"+state+"','"+nationality+"',"+index+")");
[Link]();
[Link]();
[Link](null,"Data are Registered Successfully");
}
}
catch (Exception e) {
[Link]("Exception1 is " + e);
}
}
}
class RegistrationForm {
public static void main(String args[]) {
RegistrationFormDesign form = new RegistrationFormDesign();
[Link]("Registration Form");
}
}

You might also like