Database Connectivity

ESTABLISHING CONNECTION WITH DATABASE

Using sql queries for insert, update and delete

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;
public class mydatabase extends Applet implements ActionListener{
Label l1,l2,l3,l4;
TextField t1,t2,t3,t4;
Button b1,b2,b3,b4;
Connection con;
Statement sta;

public void init()
{    setLayout(null);
l1=new Label(“enter id”);
l1.setBounds(15,15,100,30);
add(l1);
l2=new Label(“enter name”);
l2.setBounds(15,75,100,30);
add(l2);
l3=new Label(“enter age”);
l3.setBounds(15,135,100,30);
add(l3);
l4=new Label(“enter class”);
l4.setBounds(15,195,100,30);
add(l4);
t1=new TextField();
t1.setBounds(175,15,100,30);
add(t1);
t2=new TextField();
t2.setBounds(175,75,100,30);
add(t2);
t3=new TextField();
t3.setBounds(175,135,100,30);
add(t3);
t4=new TextField();
t4.setBounds(175,195,100,30);
add(t4);
b1=new Button(“insert”);
b1.setBounds(15,250,50,30);
add(b1);
b1.addActionListener(this);
b2=new Button(“update”);
b2.setBounds(75,250,50,30);
add(b2);
b2.addActionListener(this);
b3=new Button(“delete”);
b3.setBounds(135,250,50,30);
add(b3);
b3.addActionListener(this);
b4=new Button(“select”);
b4.setBounds(195,250,50,30);
add(b4);
b4.addActionListener(this);

try {

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//JOptionPane.showConfirmDialog(null, “connection established”);
con = DriverManager.getConnection(“jdbc:odbc:rec”);
JOptionPane.showConfirmDialog(null, “connection established”);

}
catch (Exception e) {
JOptionPane.showMessageDialog(null,e.getMessage());

}
}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==b1)
{

try {
sta=con.createStatement();
sta.executeUpdate(“insert into tb1 values(“+t1.getText()+”,’”+t2.getText()+”‘,”+t3.getText()+”,’”+t4.getText()+”‘)”);
JOptionPane.showConfirmDialog(null, “record inserted”);

}
catch(Exception e){
JOptionPane.showConfirmDialog(null,”Error!”);
}

}
if(arg0.getSource()==b2)
{
try {
Connection con;
Statement sta;

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
con = DriverManager.getConnection(“jdbc:odbc:recc”);

sta=con.createStatement();
sta.executeUpdate(“update tb1 set name=’”+t2.getText()+”‘,age=”+t3.getText()+”,class=’”+t4.getText()+”‘ where ID=”+t1.getText()+”‘”);

}
catch(Exception e){
JOptionPane.showConfirmDialog(null,”Error!”);
}

}
if(arg0.getSource()==b3)
{
try {
Connection con;
Statement sta;

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
con = DriverManager.getConnection(“jdbc:odbc:rec”);

sta=con.createStatement();
sta.executeUpdate(“delete from tb1 where ID=”+t1.getText()+””);

}
catch(Exception e){
JOptionPane.showConfirmDialog(null,”Error!”);
}
}

}

}

 

USE OF RESULTSET FOR RETRIEVAL OF INFORMATION FROM DATABASE

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;
public class mydatabase extends Applet implements ActionListener{
Label l1,l2,l3,l4;
TextField t1,t2,t3,t4;
Button b1,b2,b3,b4;
Connection con;
Statement sta;

public void init()
{    setLayout(null);
l1=new Label(“enter id”);
l1.setBounds(15,15,100,30);
add(l1);
l2=new Label(“enter name”);
l2.setBounds(15,75,100,30);
add(l2);
l3=new Label(“enter age”);
l3.setBounds(15,135,100,30);
add(l3);
l4=new Label(“enter class”);
l4.setBounds(15,195,100,30);
add(l4);
t1=new TextField();
t1.setBounds(175,15,100,30);
add(t1);
t2=new TextField();
t2.setBounds(175,75,100,30);
add(t2);
t3=new TextField();
t3.setBounds(175,135,100,30);
add(t3);
t4=new TextField();
t4.setBounds(175,195,100,30);
add(t4);
b1=new Button(“insert”);
b1.setBounds(15,250,50,30);
add(b1);
b1.addActionListener(this);
b2=new Button(“update”);
b2.setBounds(75,250,50,30);
add(b2);
b2.addActionListener(this);
b3=new Button(“delete”);
b3.setBounds(135,250,50,30);
add(b3);
b3.addActionListener(this);
b4=new Button(“select”);
b4.setBounds(195,250,50,30);
add(b4);
b4.addActionListener(this);

try {

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//JOptionPane.showConfirmDialog(null, “connection established”);
con = DriverManager.getConnection(“jdbc:odbc:rec”);
JOptionPane.showConfirmDialog(null, “connection established”);

}
catch (Exception e) {
JOptionPane.showMessageDialog(null,e.getMessage());

}
}
public void actionPerformed(ActionEvent arg0) {

if(arg0.getSource()==b4)
{
try {
Connection con;
Statement sta;
ResultSet rs1;

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
con = DriverManager.getConnection(“jdbc:odbc:rec”);

sta=con.createStatement();
rs1=sta.executeQuery(“select * from tb1 where ID=”+t1.getText()+””);
rs1.next();
rs1.getString(0);
Label l4=new Label(rs1.getString(“ID”));
l4.setBounds(15,300,50,30);
Label l5=new Label(rs1.getString(“name”));
l4.setBounds(75,300,50,30);
Label l6=new Label(rs1.getString(“age”));
l4.setBounds(135,300,50,30);
Label l7=new Label(rs1.getString(“class”));
l4.setBounds(195,300,50,30);
add(l4);
add(l5);
add(l6);
add(l7);

}
catch(Exception e){
JOptionPane.showConfirmDialog(null,”Error!”);
}

}

}
}

Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s