Sunteți pe pagina 1din 6

Steps to Creating JDBC Application:

There are following six steps involved in building a JDBC application:

Import the packages . Requires that you include the packages containing the JDBC
classes needed for database programming. Most often, using import java.sql.* will suffice.
Register the JDBC driver . Requires that you initialize a driver so you can open a
communications channel with the database.
Open a connection . Requires using the DriverManager.getConnection() method to
create a Connection object, which represents a physical connection with the database.
Execute a query . Requires using an object of type Statement for building and
submitting an SQL statement to the database.
Extract
data
from
result
set
. Requires
that
you
use
the
appropriate ResultSet.getXXX() method to retrieve the data from the result set.
Close the Connection . Requires explicitly closing all database resources versus relying
on the JVM's garbage collection.

/*Write ajdbc program to connect to the database*/


import java.sql.*;
public class DBConnect{
public static void main(String ar[]) {
Class.forName("oracl.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","uname","pwd");
Statement stmt=con.createStatement();
System.out.println(con);
}
}

/*Write a jdbc program to create a table in Database*/


import java.sql.*;
public class HEROS{
public static void main(String ar[]){
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
Con=DriverManager.getConnection(jdbc:oracle:thin:localhost:1521:xe,uname,pwd);
Statement stmt=con.createstatement();
stmt.executeUpdate("create table HEROS(Hname varchar2(15),Hrenumeration
number(10),Hstatus varchar2(10))");
}
}

/*Write a jdbc program to insert a record into Database*/


import java.sql.*;
public class InsertRecords{
public static void main(String ar[]) throws Exception{
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","uname","pwd");
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into HEROS values('Naresh',50000,'Married')");
stmt.executeUpdate("insert into HEROS values('Vinod',100000,UnMarried')");
stmt.executeUpdate("insert into HEROS values('Suman',25000,'Married')");
stmt.executeUpdate("insert into HEROS values('Rajasekhar',30000,'Married')");
stmt.executeUpdate("insert into HEROS values('Krishna',40000,'Married')");
}
}

/*Write a jdbc program to delete a record from Database*/


import java.sql.*;
public class DeleteRecords{
public static void main(String ar[]){
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","uname","pwd");

Statement stmt=con.createStatement();
stmt.executeUpdate("delete from HEROS where Hname=Naresh);
}
}

/*Write ajdbc program to retrive the records from Database*/


import java.sql.*;
public class InsertRecords{
public static void main(String ar[]) throws Exception{
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","uname","pwd");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table HEROS(Hname varchar2(15),Hrenumeration
number(10),Hstatus varchar2(10))");
stmt.executeUpdate("insert into HEROS values('Naresh',50000,'Married')");
stmt.executeUpdate("insert into HEROS values('Vinod',100000,UnMarried')");
stmt.executeUpdate("insert into HEROS values('Suman',25000,'Married')");
stmt.executeUpdate("insert into HEROS values('Rajasekhar',30000,'Married')");
stmt.executeUpdate("insert into HEROS values('Krishna',40000,'Married')");

}
}

/*Write a jdbc program to get the Resultset Object*/


import java.sql.*;

public class GetRset{


public static void main(String ar[])throws Exception{
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","uname","pwd");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from Customer");
rs.next(){
int i=rs.getInt("CNO");
System.out.println(i);
String s=rs.getString("CNAME");
System.out.println(s);
int sal=rs.getInt("CPURAMT");
System.out.println(sal);
System.out.println(rs.getRow());

/*Write a jdbc program using Prepared Statement Object*/


import java.sql.*;
public class WOparameter{

public static void main(String ar[]) throws Exception{


DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","uname","pwd");
PreparedStatement pstmt=con.prepareStatement();
pstmt.setInt(1,5);
pstmt.setString(2,"sur");
pstmt.setInt(3,30000);
pstmt.setString(4,"lotus");
pstmt.executeQuery();
con.close();

}
}