*How to execute Programme.
1) First Make a DataBase Name is Student.
2)Make a Table Name is st.
Create table st(firstName varchar(30),
lastName varchar(30),
erNumber int,age int,
gen varchar(30));
3)FileName:
*Main Class:Pra_12_java.java
*insert & Update GUI: Form.java
*DataBase Connection:DriverClass.java
*Insert & Update: DriverDB
*BackButton: Back_bitton,java
CODE:
*Pra_12_java.java
package p12;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class Pra_12_java extends JFrame implements ActionListener {
DriverDB db=new DriverDB();
CardLayout card;
JButton insert, delete, update,sumbitDel,submitUp;
JPanel p_insert, p_delete, p_update, p1,p_update_in;
JLabel for_delete,for_update,er_update_label,Result;
JTextField delete_no,er_update_no;
public static boolean flag=false;
public static int er=0;
public Pra_12_java() {
card = new CardLayout();
p1 = new JPanel();
p_insert = new JPanel();
p_delete = new JPanel();
p_update = new JPanel();
p_update_in =new JPanel();
insert = new JButton("INSERT");
delete = new JButton("DELETE");
update = new JButton("UPDATE");
Result=new JLabel("");
for_delete = new JLabel("Enter enroolemnt number for DELETE data");
Font font = new Font("Times New Romen", Font.BOLD, 20);
sumbitDel =new JButton("Submit");
submitUp =new JButton("Submit Up");
delete_no = new JTextField(20);
er_update_no=new JTextField(20);
for_update = new JLabel("Enter new values...");
for_update.setFont(font);
er_update_label=new JLabel("Enter er Number");
er_update_label.setFont(font);
setLayout(card);
//FIRST PAGE
p1.add(insert);
p1.add(delete);
p1.add(update);
//INSERT CODE
p_insert.add(new Form(card, getContentPane()));
//DELETE COD
p_delete.add(for_delete);
p_delete.add(delete_no);
p_delete.add(sumbitDel);
p_delete.add(Result);
p_delete.add(new Back_bitton(card, getContentPane()));
p_update_in.add(er_update_label);
p_update_in.add(er_update_no);
p_update_in.add(submitUp);
p_update_in.add(new Back_bitton(card, getContentPane()));
// UPDATE CODE
p_update.setLayout(new BorderLayout());
p_update.add(for_update,BorderLayout.NORTH);
p_update.add(new Form(card, getContentPane()));
add(p1, "1");
add(p_insert, "2");
add(p_delete, "3");
add(p_update_in,"4");
add(p_update, "5");
insert.addActionListener(this);
delete.addActionListener(this);
update.addActionListener(this);
sumbitDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String re="";
try {
int d=Integer.parseInt(delete_no.getText().toString());
re=db.Delete(d);
Result.setText(re);
} catch (ClassNotFoundException ex) {
System.out.println(ex);
} catch (SQLException ex) {
System.out.println(ex);
}
}
});
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag=true;
}
});
submitUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
card.show(getContentPane(), "5");
er=Integer.parseInt(er_update_no.getText().toString());
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
Pra_12_java obj = new Pra_12_java();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == insert) {
card.show(getContentPane(), "2");
} else if (e.getSource() == delete) {
card.show(getContentPane(), "3");
} else if (e.getSource() == update) {
card.show(getContentPane(), "4");
}
}
}
*Form.java
package p12;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import p12.Pra_12_java.*;
public class Form extends JPanel{
public static boolean flag1=false;
CardLayout cn;
Container cr;
JLabel f_name, l_name, en_no, age, dept, gender, for_delete,nothing,result;
JTextField fname, lname, en_no_t, age_t, dept_t, gender_t, delete_no;
JRadioButton male, female;
ButtonGroup g;
JButton submit;
public Form(CardLayout cn, Container cr) {
submit = new JButton("SUBMIT");
f_name = new JLabel("First name:");
l_name = new JLabel("Last name:");
en_no = new JLabel("Enroll number:");
dept = new JLabel("Departement");
gender = new JLabel("Gender:");
age = new JLabel("Age:");
nothing = new JLabel(" ");
result=new JLabel("");
fname = new JTextField(10);
lname = new JTextField(10);
en_no_t = new JTextField(10);
dept_t = new JTextField(10);
age_t = new JTextField(10);
male = new JRadioButton("male", true);
female = new JRadioButton("Female");
g = new ButtonGroup();
g.add(male);
g.add(female);
setLayout(new GridLayout(8, 3));
add(f_name);
add(fname);
add(l_name);
add(lname);
add(en_no);
add(en_no_t);
add(age);
add(age_t);
add(gender);
add(male);
add(nothing);
add(female);
add(submit);
add(new Back_bitton(cn, cr));
add(result);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Pra_12_java.flag==false)
{
try {
query();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}
}
else
{
flag1=true;
try {
query();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
public void query() throws ClassNotFoundException, SQLException
{
String re="";
DriverDB db=new DriverDB();
int er=Integer.parseInt(en_no_t.getText().toString());
int age=Integer.parseInt(age_t.getText().toString());
String gen="";
if(female.isSelected()){
gen="female";
}else{
gen="male";
}
re=db.insertAndUpdate(fname.getText().toString(), lname.getText().toString(),er , age,gen);
result.setText(re);
}
}
*DriverClass.java
package p12;
public class DriverClass {
public String Driver="com.mysql.jdbc.Driver";
public String UserName="root";
public String password="ujash7878";
public String Path="jdbc:mysql://localhost:3306/student";
}
*DriverDB.java
package p12;
Thanks ;)
Happy Coding
1) First Make a DataBase Name is Student.
2)Make a Table Name is st.
Create table st(firstName varchar(30),
lastName varchar(30),
erNumber int,age int,
gen varchar(30));
3)FileName:
*Main Class:Pra_12_java.java
*insert & Update GUI: Form.java
*DataBase Connection:DriverClass.java
*Insert & Update: DriverDB
*BackButton: Back_bitton,java
CODE:
*Pra_12_java.java
package p12;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class Pra_12_java extends JFrame implements ActionListener {
DriverDB db=new DriverDB();
CardLayout card;
JButton insert, delete, update,sumbitDel,submitUp;
JPanel p_insert, p_delete, p_update, p1,p_update_in;
JLabel for_delete,for_update,er_update_label,Result;
JTextField delete_no,er_update_no;
public static boolean flag=false;
public static int er=0;
public Pra_12_java() {
card = new CardLayout();
p1 = new JPanel();
p_insert = new JPanel();
p_delete = new JPanel();
p_update = new JPanel();
p_update_in =new JPanel();
insert = new JButton("INSERT");
delete = new JButton("DELETE");
update = new JButton("UPDATE");
Result=new JLabel("");
for_delete = new JLabel("Enter enroolemnt number for DELETE data");
Font font = new Font("Times New Romen", Font.BOLD, 20);
sumbitDel =new JButton("Submit");
submitUp =new JButton("Submit Up");
delete_no = new JTextField(20);
er_update_no=new JTextField(20);
for_update = new JLabel("Enter new values...");
for_update.setFont(font);
er_update_label=new JLabel("Enter er Number");
er_update_label.setFont(font);
setLayout(card);
//FIRST PAGE
p1.add(insert);
p1.add(delete);
p1.add(update);
//INSERT CODE
p_insert.add(new Form(card, getContentPane()));
//DELETE COD
p_delete.add(for_delete);
p_delete.add(delete_no);
p_delete.add(sumbitDel);
p_delete.add(Result);
p_delete.add(new Back_bitton(card, getContentPane()));
p_update_in.add(er_update_label);
p_update_in.add(er_update_no);
p_update_in.add(submitUp);
p_update_in.add(new Back_bitton(card, getContentPane()));
// UPDATE CODE
p_update.setLayout(new BorderLayout());
p_update.add(for_update,BorderLayout.NORTH);
p_update.add(new Form(card, getContentPane()));
add(p1, "1");
add(p_insert, "2");
add(p_delete, "3");
add(p_update_in,"4");
add(p_update, "5");
insert.addActionListener(this);
delete.addActionListener(this);
update.addActionListener(this);
sumbitDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String re="";
try {
int d=Integer.parseInt(delete_no.getText().toString());
re=db.Delete(d);
Result.setText(re);
} catch (ClassNotFoundException ex) {
System.out.println(ex);
} catch (SQLException ex) {
System.out.println(ex);
}
}
});
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag=true;
}
});
submitUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
card.show(getContentPane(), "5");
er=Integer.parseInt(er_update_no.getText().toString());
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
Pra_12_java obj = new Pra_12_java();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == insert) {
card.show(getContentPane(), "2");
} else if (e.getSource() == delete) {
card.show(getContentPane(), "3");
} else if (e.getSource() == update) {
card.show(getContentPane(), "4");
}
}
}
package p12;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import p12.Pra_12_java.*;
public class Form extends JPanel{
public static boolean flag1=false;
CardLayout cn;
Container cr;
JLabel f_name, l_name, en_no, age, dept, gender, for_delete,nothing,result;
JTextField fname, lname, en_no_t, age_t, dept_t, gender_t, delete_no;
JRadioButton male, female;
ButtonGroup g;
JButton submit;
public Form(CardLayout cn, Container cr) {
submit = new JButton("SUBMIT");
f_name = new JLabel("First name:");
l_name = new JLabel("Last name:");
en_no = new JLabel("Enroll number:");
dept = new JLabel("Departement");
gender = new JLabel("Gender:");
age = new JLabel("Age:");
nothing = new JLabel(" ");
result=new JLabel("");
fname = new JTextField(10);
lname = new JTextField(10);
en_no_t = new JTextField(10);
dept_t = new JTextField(10);
age_t = new JTextField(10);
male = new JRadioButton("male", true);
female = new JRadioButton("Female");
g = new ButtonGroup();
g.add(male);
g.add(female);
setLayout(new GridLayout(8, 3));
add(f_name);
add(fname);
add(l_name);
add(lname);
add(en_no);
add(en_no_t);
add(age);
add(age_t);
add(gender);
add(male);
add(nothing);
add(female);
add(submit);
add(new Back_bitton(cn, cr));
add(result);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(Pra_12_java.flag==false)
{
try {
query();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}
}
else
{
flag1=true;
try {
query();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
public void query() throws ClassNotFoundException, SQLException
{
String re="";
DriverDB db=new DriverDB();
int er=Integer.parseInt(en_no_t.getText().toString());
int age=Integer.parseInt(age_t.getText().toString());
String gen="";
if(female.isSelected()){
gen="female";
}else{
gen="male";
}
re=db.insertAndUpdate(fname.getText().toString(), lname.getText().toString(),er , age,gen);
result.setText(re);
}
}
*DriverClass.java
package p12;
public class DriverClass {
public String Driver="com.mysql.jdbc.Driver";
public String UserName="root";
public String password="ujash7878";
public String Path="jdbc:mysql://localhost:3306/student";
}
*DriverDB.java
package p12;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import p12.DriverDB;
public class DriverDB {
DriverClass d=new DriverClass();
public String insertAndUpdate(String firstName,String lastName,int erNumber,int age,String gender) throws ClassNotFoundException, SQLException
{
String re="";
Class.forName(d.Driver);
Connection con=DriverManager.getConnection(d.Path,d.UserName,d.password);
if(Form.flag1==false)
{
System.out.println("you are in insert panel");
System.out.println(Form.flag1);
PreparedStatement ps=con.prepareStatement("insert into st values(?,?,?,?,?)");
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setInt(3, erNumber);
ps.setInt(4, age);
ps.setString(5, gender);
int i=ps.executeUpdate();
if(i>=0)
{
re="Record Inserted";
Form.flag1=true;
}
}
else
{
int oldEr=Pra_12_java.er;
Statement s=con.createStatement();
s.executeUpdate("set sql_safe_updates=0;");
PreparedStatement ps=con.prepareStatement("update st set fisrtName=?,lastName=?,erNumber=?,age=?,gen=? where erNumber=?");
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setInt(3, erNumber);
ps.setInt(4, age);
ps.setString(5, gender);
ps.setInt(6, oldEr);
int i=ps.executeUpdate();
if(i>=0)
{
re="Record Updated";
Form.flag1=false;
}
}
return re;
}
public String Delete(int er) throws ClassNotFoundException, SQLException
{
String re="";
Class.forName(d.Driver);
Connection con=DriverManager.getConnection(d.Path,d.UserName,d.password);
PreparedStatement ps=con.prepareStatement("delete from st where erNumber=?");
ps.setInt(1, er);
int i=ps.executeUpdate();
if(i>=0)
{
re="Record Deleted";
}
return re;
}
}
*Back_Bitton.java
package p12;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Back_bitton extends JPanel{
CardLayout c;
Container con;
JButton btn;
public Back_bitton(CardLayout c , Container con) {
btn = new JButton("BACK");
this.c=c;
this.con = con;
btn.addActionListener(backListener);
add(btn);
}
ActionListener backListener=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.show(con, "1");
}
};
}
Note:I am using Mysql as Database Connection.If you do not how to Create Connection in java then follow this link:http://rctiajava.blogspot.com/2017/02/connection-of-database-with-mysql.html
Download Source Code:https://drive.google.com/drive/folders/0By7E21gumtOVWkpPSmkyVDhJRlE?usp=sharing
OutPut:
*FirstLayout:
Note:I am using Mysql as Database Connection.If you do not how to Create Connection in java then follow this link:http://rctiajava.blogspot.com/2017/02/connection-of-database-with-mysql.html
Download Source Code:https://drive.google.com/drive/folders/0By7E21gumtOVWkpPSmkyVDhJRlE?usp=sharing
OutPut:
*FirstLayout:
Thanks ;)
Happy Coding
update nahi thatu
ReplyDeletesu karvu