Practical 12 Java
Practical 12 Java
Aim:- Develop a Graphical User Interface that performs the following SQL operations:
(a) Insert
(b) Delete
(c) Display.
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
Frame f;
Button ad,de,cl;
TextField t1,t2,t3;
TextArea dis;
Label st;
int id[];
int age[];
int count=0;
String name[];
public pract12(){
addWindowListener(this);
setLayout(new FlowLayout(FlowLayout.CENTER));
setTitle("MySQL Update");
setSize(500,500);
setVisible(true);
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
cl=new Button("Display");
ad.addActionListener(this);
de.addActionListener(this);
cl.addActionListener(this);
dis=new TextArea("Database:",10,50);
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(ad); add(de);
add(cl); add(dis);
add(st);
try{Class.forName(jdbc_Driver);
st.setText("Connecting Databases...");
conn=DriverManager.getConnection(db_url,user,pass);
st.setText("Connection ESTABLISHED...");
st.setText("Creating Statement...");
stmt=conn.createStatement();
st.setText("Statement Created...");
catch(SQLException se)
se.printStackTrace();
catch(Exception e)
e.printStackTrace();
try{stmt.executeUpdate(a);
st.setText("Query OK :)");
catch(SQLException se)
se.printStackTrace();
catch(Exception e)
e.printStackTrace();
try{
stmt.executeUpdate(a);
st.setText("Query OK :)");
catch(SQLException se)
se.printStackTrace();}
catch(Exception e)
e.printStackTrace();
try
dis.setText("Database:");
ResultSet rs;
while(rs.next()){
count=rs.getInt(1);
id=new int[count];
age=new int[count];
name=new String[count];
id[i]=rs.getInt("id");
age[i]=rs.getInt("age");
name[i]=rs.getString("name");
i++;
st.setText("Readed MySQL");
}
catch(SQLException se)
se.printStackTrace();
catch(Exception e)
e.printStackTrace();
con();}
setVisible(false);
System.exit(0);
if(ae.getActionCommand()=="Add Query"){
value("+Integer.valueOf(t1.getText())+","
+Integer.valueOf(t2.getText())+",'"+t3.getText()+"');";
insrt(upt);
if(ae.getActionCommand()=="Del Query")
{
del="delete from Employees where id="+Integer.valueOf(t1.getText());
delet(del);
if(ae.getActionCommand()=="Display"){
disp();
for(int i=0;i<count;i++){
new pract12();
Output:-
After query:-