0% found this document useful (0 votes)
2 views16 pages

UNIT-I Example Programs

The document contains multiple Java JDBC examples demonstrating how to create a database, create tables, insert, delete, update, and select data using both Statement and PreparedStatement interfaces. Each example includes code snippets and expected output for operations on a 'stud' table and a 'product' table. Additionally, it showcases a GUI application for performing these operations interactively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

UNIT-I Example Programs

The document contains multiple Java JDBC examples demonstrating how to create a database, create tables, insert, delete, update, and select data using both Statement and PreparedStatement interfaces. Each example includes code snippets and expected output for operations on a 'stud' table and a 'product' table. Additionally, it showcases a GUI application for performing these operations interactively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

-1Write a program to create database in using JDBC.

import [Link].*;
public class CreateDB
{
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection cn=[Link]("jdbc:mysql://localhost:3306","root","");

Statement stmt=[Link]();
String sql = "create Database mydb";
[Link](sql);
[Link]("database created") ;

[Link]() ;
[Link]() ;

}
catch(Exception e)
{
[Link](e);
}
}

Output: database created

id name (20)

Page 1
Example-2
Write a JDBC program to create table having following structure.

import [Link].*;

public class CreateTable


{
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root","");

Statement stmt=[Link]();
[Link]("drop table stud"); //if table stud is already exit

String sql = "create table stud(id int,name varchar(20))";


[Link](sql);
[Link]("Table is created") ;

[Link]() ;
[Link]() ;
}
catch(Exception e)
{
[Link](e);
}
}
}
Output: Table is created

Page 2
Example -3 Write a JDBC program to insert data into table.

import [Link].*;
public class insert
{
public static void main(String[] args)
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root","");

Statement stmt=[Link]();
String sql = "INSERT INTO stud VALUES (1, 'Rita')";
[Link](sql);

String sql = "INSERT INTO stud VALUES (2, 'Ram')";


[Link](sql);

[Link](“Row is inserted") ;

ResultSet rs=[Link]("select DISTINCT * from stud ");


while([Link]())
{
[Link]([Link]("id")+"\t");
[Link]([Link]("name")+"\n");
}
[Link]() ;
[Link]() ;
[Link]() ;
}
catch(Exception e)
{
[Link](e);
}
}
}
Output:
Row is inserted

1 Rita
2 Ram

Page 3
Example -4 Write a JDBC program to delete data from table.

import [Link].*;
public class del
{
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root","");

Statement stmt=[Link]();
String sql = "delete from stud " + "where id=2";
[Link](sql);
[Link]("Row is deleted") ;

ResultSet rs=[Link]("select DISTINCT * from stud ");


while([Link]())
{
[Link]([Link]("id")+"\t"); // you can use 1 instead of id,where
1 is column index
[Link]([Link]("name")+"\n");
}
[Link]() ;
[Link]() ;
[Link]() ;
}
catch(Exception e)
{
[Link](e);
}

}
}
Output :
Row is deleted
1 Rita

Page 4
Example -5 Write a JDBC program to update data from table.

import [Link].*;
public class update
{
public static void main(String[] args)
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
Statement stmt=[Link]();
int up=[Link]("update stud set name='Sita' where id=1");
if(up>0)
{
[Link]("Record Sucessfully Updated...!!!");
}
ResultSet rs=[Link]("select DISTINCT * from stud ");
while([Link]())
{
[Link]([Link]("id")+"\t");
[Link]([Link]("name")+"\n");
}
[Link]() ;
[Link]() ;
[Link]() ;
}
catch(Exception e)
{
[Link](e);
}
}
}

Output :
Record Sucessfully Updated...!!!
1 Sita

Page 5
Example -6 Write a JDBC program to display record from table.

import [Link].*;
public class selectdata
{
public static void main(String args[])
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root", "");
Statement stmt=[Link]();
[Link](“ Table data :”);
ResultSet rs=[Link]("select distinct * from stud");
while([Link]())
{
[Link]([Link]("id")+ "\t");
[Link]([Link]("name")+"\n");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
Output ;

Table data :

1 Sita
 Program using prepareStatement( )

Page 6
Example -7
Write JDBC program to insert data using PreparedStatement interface.

import [Link].*;
public class insert
{
public static void main(String[] args)
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
String sql = "insert into stud(id, name) values(?, ?)";
PreparedStatement pstmt=[Link](sql);
[Link](1, 10); // set input parameter 1 for id,value 100
[Link](2, "dhara"); // set input parameter 2 for name ,value dhara
[Link]();
ResultSet rs=[Link]("select DISTINCT * from stud ");
while([Link]())
{
[Link]([Link]("id")+"\t");
[Link]([Link]("name")+"\t");
}
[Link]() ;
[Link]() ;
}
catch(Exception e)
{
[Link](e);
}
}
}
Output :
10 dhara

Page 7
Example -8
Write JDBC program to delete data from table using PreparedStatement
interface.

import [Link].*;
public class delete

{
public static void main(String[] args)
{

try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
String sql="delete from stud where id=?";
PreparedStatement pstmt=[Link](sql);
[Link](1,10); //10 is id you want to delete
[Link]();
ResultSet rs=[Link]("select DISTINCT * from stud ");
while([Link]())
{
[Link]([Link]("id")+"\t");
[Link]([Link]("name")+"\t");
}
[Link]() ;
[Link]() ;
[Link]() ;

}
catch(Exception e)
{
[Link](e);
}
}
}

Page 8
Example -9
Write JDBC program to update data from table using PreparedStatement
interface.

import [Link].*;
public class updatepre
{
public static void main(String[] args)
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb1","root","");
String sql= "UPDATE stud SET name = ? WHERE id = ?";
PreparedStatement pstmt = [Link](sql);
[Link](1, "abc"); // name =abc
[Link](2, 10); //id=10
// execute insert SQL stetement
int up=pstmt .executeUpdate();
if(up>0)
{
[Link]("Record Sucessfully Updated...!!!");
}
ResultSet rs=[Link]("select DISTINCT * from stud ");
while([Link]())
{
[Link]([Link]("id")+"\t");
[Link]([Link]("name")+"\n");
}
[Link]() ;
[Link]() ;
[Link]() ;

}
catch(Exception e)
{
[Link](e);
}
}
}
Output:
Record Sucessfully Updated...!!!
10 abc
Page 9
Example 10 :
Write a program which creates GUI and performs following operations:
1) Insert 2) delete 3) Update
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

public class textdata extends JApplet implements ActionListener


{
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(10);
JLabel jlb1=new JLabel("id :",[Link]);
JLabel jlb2=new JLabel("name:",[Link]);
JLabel jlb3=new JLabel(" ");
JLabel jlb4=new JLabel(" ");
JButton jbinst=new JButton("Insert");
JButton jbdlt=new JButton("Delete");
JButton jbrst=new JButton("Reset");
JButton jbupdt=new JButton("Update");
Statement stmt;
ResultSet rs;
public void init()
{
[Link](this);
[Link](this);
[Link](this);
[Link](this);

JPanel jp=new JPanel();


[Link](new GridLayout(0,2));
[Link](jlb1);
[Link](jtf1);
[Link](jlb2);
[Link](jtf2);
[Link](jbinst);
[Link](jbdlt);
[Link](jbrst);
[Link](jbupdt);
[Link](jlb3);
[Link](jlb4);

add(jp);
initializeDB();
Page 10
}
private void initializeDB()
{
try
{
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root"
,"");
[Link]("database connection");
stmt=[Link]();
}
catch(Exception e)
{
[Link](e);
}
}

public void actionPerformed(ActionEvent e)


{
String s1,s2;

if([Link]().equals("Insert"))
{

s1= [Link]();
s2= [Link]();
try
{
int in=[Link]("INSERT INTO stud VALUES
('"+s1+"','"+s2+"')");
if(in>0)
[Link]("inserted") ;
String qs="select distinct* from stud";
rs=[Link](qs);
if([Link]())
{
String st1=s1;//[Link](1);
String st2=s2;//[Link](2);
[Link]("id is: "+st1);
[Link]("name is: "+st2);
}
}
catch(SQLException ex)
{
Page 11
[Link]();
}
}
if([Link]().equals("Delete"))
{
try
{
String sql = "delete from stud " + "where id="+[Link]();
[Link](sql);
[Link](null, "Record is deleted!!!");
}
catch(SQLException ex)
{
[Link]();
}
}

if([Link]().equals("Update"))
{
try
{

String sql="update stud set name='"+[Link]()+"' where id =


"+[Link]()+"";
[Link](sql);
[Link](null, "Record is Updated!!!");
}
catch(SQLException ex)
{
[Link]();
}
}
if([Link]().equals("Reset"))
{

[Link]("");
[Link]("");
[Link]("");
[Link]("");
}

}
}

Page 12
Output:
Insert

Reset

Delete

Page 13
Example -11
Write a program which display product detail when user selects any product.

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class product extends JApplet implements ActionListener
{
/**
create table product(pid int, productname varchar(25), price int);
insert into product values(1, 'Laptop', 35000);
insert into product values(2, 'SmartPhone', 15000);
insert into product values(3, 'LED TV 48 Inch', 50000);
insert into product values(4, 'Iphone', 60000);
insert into product values(5, 'Tablet', 10000);
*/

ArrayList<String> list = new ArrayList<>();


JComboBox jcb;
Connection cn;
Statement stmt;
ResultSet rs;
JPanel pc = new JPanel();
JLabel jlb = new JLabel("Select Product");
JLabel jlb1 = new JLabel("Product = ");
JLabel jlb2 = new JLabel("Price = ");
JLabel jlproduct = new JLabel();
JLabel jlprice = new JLabel();
public void init()
{
initializeDB();
try
{
[Link]("Select Product");
ResultSet rs = [Link]("select * from product");
while([Link]())
{
[Link]([Link]("productname"));
}
}
catch(Exception e)
{
[Link]();
Page 14
}
[Link](null);
[Link](15, 25, 100, 25);
jcb = new JComboBox([Link]());
[Link](110, 25, 150, 25);
[Link](this);
[Link](15,60,70,25);
[Link](80,60,160,25);
[Link](180,60,50,25);
[Link](230,60,70,25);
[Link](jlb);
[Link](jcb);
[Link](jlb1);
[Link](jlb2);
[Link](jlproduct);
[Link](jlprice);
add(pc);
setSize(325, 245);
}

private void initializeDB()


{
try {
[Link]("[Link]");
Connection
cn=[Link]("jdbc:mysql://localhost:3306/mydb","root","");
[Link]("database connection");
stmt=[Link]();
} catch(Exception e){
[Link](e);
}
}

public void actionPerformed(ActionEvent e)


{
JComboBox cb = (JComboBox)[Link]();
String proName = (String)[Link]();
updateLabel(proName);
}
public void updateLabel(String proName)
{
try
{
ResultSet rs = [Link]("select * from product where
productname='"+proName+"'");
Page 15
while([Link]())
{
[Link](proName);
[Link]([Link]("price"));
}
}
catch(Exception e)
{
[Link]();
}
}
}

Output:

Page 16

You might also like