Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

NAME: NISHCHAY PATEL

COURSE CODE:CSI0403

ROLL NO:20BCM053

OOAD 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.

Gender should be entered as text i.e. Male or Female

Hobbies and Languages known should be inserted as text separated by commas.

e.g. Gujarati,Hindi (if two checkboxes have been checked on)

Date of Birth should be inserted as date field.

City name should be inserted as text.

No text area needed now for showing the details.

Use Swing controls for the same.

SOURCE CODE::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

class string_operation{
String t,s1,s2;
String split(String t){
this.t=t;
int len=0;
String s="";
do{
++len;
}
while(t.charAt(len)!='@');
char[] str = new char[(t.length()-len)];
int j=0;
for(int i=len+1;i<t.length();i++){
str[j]=t.charAt(i);
++j;
}
for(int i=0;i<t.length()-len-1;i++)
s+=Character.toString(str[i]);
// s.append(Character.toString(str[i]));
return s;
}
boolean stringEqual(String s1,String s2){
this.s1=s1;
this.s2=s2;
int i,len=0;
if(s1.length()==s2.length()){
for (i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == s2.charAt(i)) {
++len;
}
}
}
if(s1.length()==len){
return true;
}
return false;
}
boolean isThere(String t){
this.t=t;
int i=0,len=0;
do{
if(t.charAt(i)=='@'){
++len;
}
++i;
}
while(i<t.length());
if(len==0){
return true;
}
return false;
}
}

class Form extends JFrame implements KeyListener,MouseListener,ActionListener{


JTextField nm1,nm2,nm3,email,pin;
JPasswordField pass;
JPanel p1,p2,p3,p4,p5;
JLabel gen1,gen2,gender,l1,l2,l3,l4,l5,l6,l7,l8,l9;
JComboBox<String> jc1,jc2,jc3,jc4;
JRadioButton male,female;
JTextArea address;
JButton submit;
JLabel submt;

JLabel lab1,lab2;
JLabel[] cb=new JLabel[3];
JCheckBox[] hobby=new JCheckBox[3];
JLabel[] lan=new JLabel[3];
JCheckBox[] lang=new JCheckBox[3];

String[] date
={"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"};
String[] month =
{"Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec"};
String[] year = {"1998","1999","2000","2001","2002","2003"};
String[] city = {" ","Mumbai","Ahmedabad","New
Delhi","Bangalore","Surat","Chennai","Kolkata","Rajkot"};

Form(String msg){
super(msg);
// name
p1=new JPanel();

l1=new JLabel();
l2=new JLabel();
l3=new JLabel();

l1.setText("First Name");
p1.add(l1);
nm1=new JTextField(15);
p1.add(nm1);
nm1.addKeyListener(this);

l2.setText("Mid Name");
p1.add(l2);
nm2=new JTextField(15);
p1.add(nm2);
nm2.addKeyListener(this);

l3.setText("Last Name");
p1.add(l3);
nm3=new JTextField(15);
p1.add(nm3);
nm3.addKeyListener(this);
add(p1);

//Birth-date && gender


p2=new JPanel();
l4=new JLabel();
l4.setText("BirthDate ");
p2.add(l4);
jc1=new JComboBox<>(date);
p2.add(jc1);
jc2=new JComboBox<>(month);
p2.add(jc2);
jc3=new JComboBox<>(year);
p2.add(jc3);
gender=new JLabel();
gender.setText(" gender ");
p2.add(gender);
male=new JRadioButton();
p2.add(male);
male.addActionListener(this);
gen1=new JLabel();
gen1.setText("male ");
p2.add(gen1);
female=new JRadioButton();
p2.add(female);
female.addActionListener(this);
gen2=new JLabel();
gen2.setText("female");
p2.add(gen2);
add(p2);

// email-password-pin_code
p3=new JPanel();
l5=new JLabel();
l5.setText("Email");
p3.add(l5);
email=new JTextField(15);
p3.add(email);
email.addKeyListener(this);
l6=new JLabel();
l6.setText("password");
p3.add(l6);
pass=new JPasswordField(15);
p3.add(pass);
pass.addKeyListener(this);
l7=new JLabel();
l7.setText("pin");
p3.add(l7);
pin=new JTextField(15);
p3.add(pin);
pin.addKeyListener(this);
add(p3);

//hobbies-languages-city
p4=new JPanel();
lab1=new JLabel();
lab1.setText("Hobby ");
p4.add(lab1);
hobby[0]=new JCheckBox();
p4.add(hobby[0]);
cb[0]=new JLabel();
cb[0].setText("play");
p4.add(cb[0]);
hobby[1]=new JCheckBox();
p4.add(hobby[1]);
cb[1]=new JLabel();
cb[1].setText("read");
p4.add(cb[1]);
hobby[2]=new JCheckBox();
p4.add(hobby[2]);
cb[2]=new JLabel();
cb[2].setText("other ");
p4.add(cb[2]);
lab2=new JLabel();
lab2.setText(" Languages");
p4.add(lab2);
lang[0]=new JCheckBox();
p4.add(lang[0]);
lan[0]=new JLabel();
lan[0].setText("English");
p4.add(lan[0]);
lang[1]=new JCheckBox();
p4.add(lang[1]);
lan[1]=new JLabel();
lan[1].setText("Gujarati");
p4.add(lan[1]);
lang[2]=new JCheckBox();
p4.add(lang[2]);
lan[2]=new JLabel();
lan[2].setText("Hindi ");
p4.add(lan[2]);
l8=new JLabel();
l8.setText(" city");
p4.add(l8);
jc4=new JComboBox<>(city);
p4.add(jc4);
add(p4);

//address
p5=new JPanel();
l9=new JLabel();
l9.setText("address ");
p5.add(l9);
address=new JTextArea(10,20);
p5.add(address);
submt=new JLabel();
submt.setText(" submit data");
p5.add(submt);
submit=new JButton("submit");
p5.add(submit);
submit.addActionListener(this);
add(p5);

setSize(750,400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
setVisible(true);
}

//key_listener abstract methods overriding


public void keyPressed(KeyEvent ke){}
public void keyReleased(KeyEvent ke){
// name validations
if(ke.getSource()==nm1){
nm1.setText(nm1.getText().toUpperCase());
}
else if(ke.getSource()==nm2){
nm2.setText(nm2.getText().toUpperCase());
}
else if(ke.getSource()==nm3){
nm3.setText(nm3.getText().toUpperCase());
}

//email validations
else if(ke.getSource()==email){
String E_server="gmail.com";
string_operation str_op=new string_operation();
if(str_op.isThere(email.getText())){
l5.setText("NOT VALID");
}
else{
String t=str_op.split(email.getText());
if(!str_op.stringEqual(E_server,t)){
l5.setText("NOT VALID");
}
else if(str_op.stringEqual(E_server,t)){
l5.setText("Email");
JFrame f=new JFrame();
JLabel l=new JLabel("valid id");
f.add(l);
f.setSize(50,100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
f.setVisible(true);
}
}
}

//password validations
else if(ke.getSource()==pass){
if(pass.getText().length()>10){
JFrame f=new JFrame();
JLabel l=new JLabel("password not valid");
f.add(l);
f.setSize(50,100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
f.setVisible(true);
}
String password=pass.getText();
int i=0;
int check=0;
do{

if(password.charAt(i)=='@'||password.charAt(i)=='#'||password.charAt(i)=='$'||passw
ord.charAt(i)=='&'){
++check;
}
++i;
}
while(i<password.length());
if(check==0){
l6.setText("Enter special symbol");
}
if(check>0){
l6.setText("password");
}
}
else if(ke.getSource()==pin){
try{
int PIN_CODE=Integer.parseInt(pin.getText());
if(pin.getText().length()>6){
pin.setText("Enter valid pin");
}
}
catch(NumberFormatException nfe){
pin.setText("Enter integers");
}
}
}
public void keyTyped(KeyEvent ke){}

// Mouse_listener abstract methods overriding


public void mouseEntered(MouseEvent me){}
public void mouseClicked(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me){}

// Action_listener abstract methods overriding


public void actionPerformed(ActionEvent ae){
// radio button validity
if(male.isSelected()){
female.setSelected(false);
}
else if(female.isSelected()){
male.setSelected(false);
}

if(ae.getSource()==submit){
String s="";
String s1="";
String s2="";
if(male.isSelected()){
s+="male";
}
else{
s+="female";
}
for(int i=0;i<hobby.length;i++){
if(hobby[i].isSelected()){
s1+=cb[i].getText()+",";
}
if(lang[i].isSelected()){
s2+=lan[i].getText()+",";
}
}
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root","N@
78?926:365");
Statement stmt=con.createStatement();
String sql="CREATE TABLE Formdata( id INT, name VARCHAR(45) ,
birthdate VARCHAR(45) , email VARCHAR(45) ," +
" password VARCHAR(45) , pin VARCHAR(45) , gender VARCHAR(45) ,
hobby VARCHAR(45) ,languages VARCHAR(45) , address VARCHAR(45) , city VARCHAR(45) ,
PRIMARY KEY(id) )";
stmt.executeUpdate(sql);
String insertData="INSERT INTO
Formdata(id,name,birthdate,email,password,pin,gender,hobby,languages,address,city)
VALUES (1,'"+nm1.getText()+" "+nm2.getText()+" "+nm3.getText()+"' , '"

+jc1.getItemAt(jc1.getSelectedIndex())+"/"+jc2.getItemAt(jc2.getSelectedIndex())+"/
"+jc3.getItemAt(jc3.getSelectedIndex())+"','"+email.getText()+"','"
+pass.getText()+"' , "+pin.getText()+" , '"+s+"' , '"+s1+"' ,
'"+s2+"' , '"+address.getText()+"' , '"+jc4.getItemAt(jc4.getSelectedIndex())+"'
)";

stmt.execute(insertData);
con.close();
}
catch(ClassNotFoundException | SQLException e){
System.out.println(e);
}
}
}
}
class Main {
public static void main(String[] args) {
new Form("Form");
}
}

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 for Successful/Unsuccessful login using
DialogBox (JDialog class).

SOURCE CODE::
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class loginForm extends JFrame implements ActionListener{
JLabel l1,l2,l3,l4;
JTextField tf1;
JPasswordField pf1;
JButton login;
loginForm(){
l1=new JLabel();
l1.setText("USER ID ");
add(l1);
tf1=new JTextField(20);
add(tf1);
l2=new JLabel();
l2.setText("PASSWORD");
add(l2);
pf1=new JPasswordField(20);
add(pf1);
l3=new JLabel();
l3.setText("********************");
add(l3);
login=new JButton("login");
add(login);
login.addActionListener(this);
l4=new JLabel();
l4.setText("********************");
add(l4);

setSize(300,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(new FlowLayout(FlowLayout.LEFT));
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
String pass="";
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root","N@
78?926:365");
Statement stmt=con.createStatement();
String sql="SELECT password FROM Formdata WHERE
email='"+tf1.getText()+"' ";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
pass+=rs.getString(1);
System.out.println(rs.getString(1));
}
if(pass.equals(pf1.getText())){
JDialog d=new JDialog();
JLabel l=new JLabel();
l.setText("login successful!");
d.add(l);
d.setVisible(true);
d.setSize(100,100);
}
else{
JDialog d=new JDialog();
JLabel l=new JLabel();
l.setText("login unsuccessful!");
d.add(l);
d.setVisible(true);
d.setSize(100,100);
}
con.close();
}
catch (ClassNotFoundException | SQLException e){
System.out.println(e);
}
}
}
class Main {
public static void main(String[] args) {
new loginForm();
}
}

output::

You might also like