You are on page 1of 7

Practical 12

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.*;

public class pract12 extends Frame implements WindowListener,ActionListener

Frame f;

Button ad,de,cl;

TextField t1,t2,t3;

TextArea dis;

Label st;

int id[];

int age[];

int count=0;

String name[];

static String upt=null;

static String del=null;

static final String jdbc_Driver="com.mysql.cj.jdbc.Driver";

static final String db_url="jdbc:mysql://localhost/emp";

static final String user="root";

static final String pass="kartik";

static Connection conn=null;


static Statement stmt=null;

public pract12(){

addWindowListener(this);

setLayout(new FlowLayout(FlowLayout.CENTER));

setTitle("MySQL Update");

setSize(500,500);

setVisible(true);

Label l1=new Label("ID=");

t1=new TextField(10);

Label l2=new Label("Age=");

t2=new TextField(10);

Label l3=new Label("Name=");

t3=new TextField(10);

ad=new Button("Add Query");

de=new Button("Del Query");

cl=new Button("Display");

ad.addActionListener(this);

de.addActionListener(this);

cl.addActionListener(this);

dis=new TextArea("Database:",10,50);

st=new Label("Status Bar ");

add(l1); add(t1);

add(l2); add(t2);

add(l3); add(t3);

add(ad); add(de);

add(cl); add(dis);

add(st);

public void con(){

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();

public void insrt(String a){

try{stmt.executeUpdate(a);

st.setText("Query OK :)");

catch(SQLException se)

se.printStackTrace();

catch(Exception e)

e.printStackTrace();

public void delet(String a){

try{

stmt.executeUpdate(a);
st.setText("Query OK :)");

catch(SQLException se)

se.printStackTrace();}

catch(Exception e)

e.printStackTrace();

public void disp(){

try

dis.setText("Database:");

ResultSet rs;

rs=stmt.executeQuery("Select Count(*) from Employees");

while(rs.next()){

count=rs.getInt(1);

id=new int[count];

age=new int[count];

name=new String[count];

rs=stmt.executeQuery("Select * From Employees");

int i=0; while(rs.next()){

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();

public void windowOpened(WindowEvent we){

con();}

public void windowClosing(WindowEvent we){

setVisible(false);

System.exit(0);

public void windowClosed(WindowEvent we){}

public void windowIconified(WindowEvent we){}

public void windowDeiconified(WindowEvent we){}

public void windowActivated(WindowEvent we){}

public void windowDeactivated(WindowEvent we){}

public void actionPerformed(ActionEvent ae)

if(ae.getActionCommand()=="Add Query"){

upt="Insert into Employees

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++){

dis.append("\nID:"+id[i]+" Name:"+name[i]+" Age"+age[i]);

public static void main(String []args){

new pract12();

Output:-
After query:-

You might also like