0% found this document useful (0 votes)
29 views15 pages

21BCM075 Csi0403 Assignment 3

1. The document describes a Java program that creates a student registration form with various fields like name, date of birth, gender, email, password, hobbies, languages known, address etc. 2. When the submit button is clicked, it validates the password field to check for a special character and inserts the student details into a database table if valid. 3. It then creates a login form to login using the email and password created during registration and displays an appropriate message.

Uploaded by

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

21BCM075 Csi0403 Assignment 3

1. The document describes a Java program that creates a student registration form with various fields like name, date of birth, gender, email, password, hobbies, languages known, address etc. 2. When the submit button is clicked, it validates the password field to check for a special character and inserts the student details into a database table if valid. 3. It then creates a login form to login using the email and password created during registration and displays an appropriate message.

Uploaded by

Chirag Saraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ROLL NUMBER : 21BCM075

COURSE CODE : CSI0403


ASSIGNMENT : 3
1) With respect to a form designed in Assignment 2,
upon pressing the submit button , student details
should be inserted in database table.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class a3 extends JFrame implements ActionListener, ItemListener {


String data1 = "";
String data8 = "";
String data9 = "";

a3() {

JLabel l1 = new JLabel("First Name");


add(l1);
l1.setBounds(100, 100, 100, 30);

JTextField t1 = new JTextField();


add(t1);
t1.setBounds(200, 100, 150, 30);

JLabel l2 = new JLabel("Middle Name");


add(l2);
l2.setBounds(100, 150, 100, 30);

JTextField t2 = new JTextField();


add(t2);
t2.setBounds(200, 150, 150, 30);

JLabel l3 = new JLabel("Last Name");


add(l3);
l3.setBounds(100, 200, 100, 30);

JTextField t3 = new JTextField();


add(t3);
t3.setBounds(200, 200, 150, 30);
JLabel l4 = new JLabel("Date of Birth");
add(l4);
l4.setBounds(100, 250, 100, 30);

String dates[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31" };

JComboBox c1 = new JComboBox(dates);


c1.setBounds(200, 255, 50, 30);

add(c1);

String months[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12" };

JComboBox c2 = new JComboBox(months);


c2.setBounds(250, 255, 50, 30);

add(c2);
String years[] = { "1998", "1999", "2000", "2001", "2002", "2003"
};
JComboBox c3 = new JComboBox(years);
c3.setBounds(300, 255, 60, 30);

add(c3);

JLabel l5 = new JLabel("Gender");


add(l5);
l5.setBounds(100, 305, 100, 30);
ButtonGroup gr = new ButtonGroup();
JRadioButton cb1 = new JRadioButton("Male", false);
cb1.setBounds(200, 300, 70, 40);
JRadioButton cb2 = new JRadioButton("Female", false);
cb2.setBounds(270, 300, 80, 40);
gr.add(cb1);
gr.add(cb2);

cb1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data1 = "Male";
}
});
cb2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data1 = "Female";
}
});

add(cb1);
add(cb2);

JLabel l6 = new JLabel("Email ID");


add(l6);
l6.setBounds(100, 350, 90, 30);

JTextField t4 = new JTextField();


add(t4);
t4.setBounds(190, 350, 150, 30);

JLabel l7 = new JLabel("Password");


add(l7);
l7.setBounds(100, 400, 90, 30);

JTextField t5 = new JTextField();


add(t5);
t5.setBounds(190, 400, 150, 30);
t5.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (t5.getText().length() >= 10) {
e.consume();
}
}
});
JLabel l14 = new JLabel("one special symbol required");
add(l14);
l14.setBounds(190, 430, 250, 20);
l14.setVisible(false);

JLabel l8 = new JLabel("Hobbies");


add(l8);
l8.setBounds(100, 450, 90, 30);

JCheckBox cb3 = new JCheckBox("Photography", false);


cb3.setBounds(190, 450, 130, 30);
JCheckBox cb4 = new JCheckBox("Sports", false);
cb4.setBounds(190, 480, 130, 30);
JCheckBox cb8 = new JCheckBox("Blogging", false);
cb8.setBounds(190, 510, 130, 30);
add(cb3);
add(cb4);
add(cb8);

cb3.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data9 = data9 + "Photography";
}
});
cb4.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data9 = data9 + ",Sports";
}
});
cb8.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data9 = data9 + ",Blogging";
}
});

JLabel l9 = new JLabel("Languages Known");


add(l9);
l9.setBounds(100, 540, 140, 30);

JCheckBox cb5 = new JCheckBox("English", false);


cb5.setBounds(240, 540, 80, 30);
JCheckBox cb6 = new JCheckBox("Hindi", false);
cb6.setBounds(240, 570, 80, 30);
JCheckBox cb7 = new JCheckBox("Gujarati", false);
cb7.setBounds(240, 600, 80, 30);
add(cb5);
add(cb6);
add(cb7);

cb5.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data8 = data8 + "English";
}
});
cb6.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data8 = data8 + ",Hindi";
}
});
cb7.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
data8 = data8 + ",Gujarati";
}
});

JLabel l10 = new JLabel("Address");


add(l10);
l10.setBounds(100, 640, 80, 30);
TextArea ta1 = new TextArea();
add(ta1);
ta1.setBounds(190, 640, 200, 70);

JLabel l11 = new JLabel("Pincode");


add(l11);
l11.setBounds(100, 720, 80, 30);

JTextField t6 = new JTextField();


add(t6);
t6.setBounds(190, 720, 150, 30);
t6.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (t6.getText().length() >= 6) {
e.consume();
}
}
});

JLabel l12 = new JLabel("City Name");


add(l12);
l12.setBounds(100, 770, 80, 30);

String[] city = { "Ahemdabad", "Bangalore", "Chennai", "Delhi",


"Hyderabad", "Kolkata", "Mumbai", "Pune" };
JComboBox c4 = new JComboBox(city);
c4.setBounds(190, 770, 100, 30);
add(c4);

JButton b1 = new JButton("Submit");


add(b1);
b1.setBounds(350, 820, 80, 30);

JButton b2 = new JButton("Reset");


add(b2);
b2.setBounds(250, 820, 80, 30);

JLabel l13 = new JLabel();


add(l13);
l13.setBounds(550, 700, 180, 30);

setLayout(null);
setSize(900, 900);
setTitle("Registration Form");
getContentPane().setBackground(new Color(219, 255, 204));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count = 0;
char[] ch = t5.getText().toCharArray();
for (char c : ch)
if (!(Character.isDigit(c) ||
(Character.isAlphabetic((int) c)))) {
count = count + 1;

break;
}
if (count < 1) {
l14.setVisible(true);
t5.setBackground(Color.red);
l14.setForeground(Color.red);
l14.setFont(new Font("Cascadia code", Font.BOLD,
16));

String data2 = (String) c3.getSelectedItem() + "-" +


(String) c2.getSelectedItem() + "-"
+ (String) c1.getSelectedItem();
l13.setText("Registration Successfully..");

try {
String DB_URL =
"jdbc:mysql://localhost:3306/assignment3_ooad";
String USER = "root";
String PASS = "1234";
Connection conn = DriverManager.getConnection(DB_URL,
USER, PASS);
System.out.println("database connected...");

String q1 = "insert into


stu_data(FirstName,MiddleName,LastName,DateofBirth,Gender,emailID,passwor
d,Hobbies,Languages,Address,Pincode,CityName)
values(?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(q1);
pstmt.setString(1, t1.getText());
pstmt.setString(2, t2.getText());
pstmt.setString(3, t3.getText());
pstmt.setString(4, data2);
pstmt.setString(5, data1);
pstmt.setString(6, t4.getText());
pstmt.setString(7, t5.getText());
pstmt.setString(8, data9);
pstmt.setString(9, data8);
pstmt.setString(10, ta1.getText());
pstmt.setInt(11, Integer.parseInt(t6.getText()));
pstmt.setString(12, (String) c4.getSelectedItem());
pstmt.executeUpdate();
System.out.println("data inserted succesfully..");
conn.close();
} catch (SQLException a) {
a.printStackTrace();
}

});

public static void main(String[] args) {


new a3();
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub

}
}
OUTPUT :
2) Use login form to login into the system. Use email id
and password created in part 1 as login credentials.
Display appropriate message.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class a3_2 extends JFrame implements ActionListener {


a3_2() {

JButton b1 = new JButton("Login");


add(b1);
b1.setBounds(220, 250, 80, 30);

JButton b2 = new JButton("Reset");


add(b2);
b2.setBounds(100, 250, 80, 30);

JLabel l1 = new JLabel("emailID");


add(l1);
l1.setBounds(80, 100, 80, 30);

JLabel l2 = new JLabel("password");


add(l2);
l2.setBounds(80, 150, 150, 30);

JTextField t1 = new JTextField();


add(t1);
t1.setBounds(160, 100, 150, 30);

JPasswordField t2 = new JPasswordField();


add(t2);
t2.setBounds(160, 150, 150, 30);

setLayout(null);
setSize(400, 400);
setTitle("Login Form");
getContentPane().setBackground(new Color(255, 204, 219));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t1.setText("");
t2.setText("");
}
});

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

try {
String DB_URL =
"jdbc:mysql://localhost:3306/assignment3_ooad";
String USER = "root";
String PASS = "1234";
Connection conn = DriverManager.getConnection(DB_URL,
USER, PASS);
System.out.println("database connected...");

Statement stmt = conn.createStatement();


String emailID = t1.getText();
String password = t2.getText();

String q = "select * from stu_data where emailID='" +


emailID + "'and password='" + password + "'";
ResultSet rs = stmt.executeQuery(q);
if (rs.next()) {
dispose();
JFrame jf = new JFrame();
JLabel l3 = new JLabel("Login Sucessfull..");
jf.add(l3);
l3.setBounds(70, 100, 300, 100);
l3.setFont(new Font("Cascadia code", Font.BOLD,
26));
jf.setLayout(null);
jf.setSize(400, 400);

jf.getContentPane().setBackground(new Color(255,
204, 219));
jf.setVisible(true);

} else {
JFrame f = new JFrame();
JOptionPane.showMessageDialog(f, "emailID or
password incorrect!!");
t1.setText("");
t2.setText("");
}
conn.close();

} catch (Exception a) {
a.printStackTrace();
}
}

});

public static void main(String[] args) {


new a3_2();
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
}

OUTPUT :
WHEN EMAIL AND PASSWORD ENTERED IS STORED IN
THE DATABASE .
WHEN EMAIL AND PASSWORD ENTERED IS
NOT STORED IN THE DATABASE.

You might also like