Thursday, September 29, 2011

Display Records as Normal


Display Record

import java.sql.*;
class DisplayRecord
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select eno,name from emp3");
System.out.println(rs);
while(rs.next())
{
int deno=rs.getInt("eno");
System.out.println("Eno: "+deno);
String s=rs.getString("name");
System.out.println("Nane: "+s);
}
}
}

Using Index Retrive Data From The Table
Using Prepared Statement
retrieve-records

Display Record Using Index


DisplayRecordIndex

//Retrive the data from DB and display it Using with INDEXS

import java.sql.*;
import java.sql.*;
class DisplayRecordIndex
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select eno,name from emp3");
System.out.println(rs);
while(rs.next())
{
int deno=rs.getInt(1);
System.out.println("Eno: "+deno);
String s=rs.getString(2);
System.out.println("Nane: "+s);
}
con.close();
System.out.println("connection is closed");
}
}


Using Prepared Statement 
retrieve-records

PrepareStmt_insert operation


PrepareStmt_insert operation

import java.sql.*;
class PrepareStmt
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
PreparedStatement pstmt=con.prepareStatement("insert into emp3 values(?,?)");
pstmt.setInt(1,10);
pstmt.setString(2,"anjali");
int a=pstmt.executeUpdate();
System.out.println("insert record is with Prepare Statement......"+a);
}
}



Using Index Retrive Data From The Table
retrieve-records

Prepare_statement


Prepare_statement

// PrepareStatement retriving the values from table student in a database

import java.sql.*;
public class Prepare_statement

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
PreparedStatement pstmt=con.prepareStatement("select *from student where sno=?");
System.out.println("preparedStatement object is created");
pstmt.setInt(1,15);
ResultSet rs=pstmt.executeQuery();
System.out.println("sno"+" "+"name"+" "+"place");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println();
System.out.print(rs.getInt("sno")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("place"));
}
pstmt.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:
Table in Database:
create table student(sno number(10),name varchar2(20),place varchar2(20));
prepared statement is improves the performence

*/


Retrieve Records


Retrieve_Records

//Retrieving the values from table student in a database

import java.sql.*;
public class Retrieve_Records

{
public static void main(String args[])
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("thin driver is registerd");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
System.out.println("Connection is established to DB");
Statement stmt=con.createStatement();
System.out.println("Statement object is created");
ResultSet rs=stmt.executeQuery("select *from student");
System.out.println("sno"+" "+"name"+" "+"place");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println();
System.out.print(rs.getInt("sno")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("place"));
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Registeration faild");
}
}
}

/*
Explanation:

For Select Querys we have to use the "executeQuery" method
it return the Results into a ResultSet object
by using next() method we can move the next row from one row to anethor
here we used differnt type getter method(we can use getString() for all data types)
but its not recomended
TRy with different type select querys

*/



Using Index Retrive Data From The Table
Using Prepared Statement 

Batch Updates


BatchUpdates

import java.sql.*;
class BatchUpdates
{
public static void main(String[] args) throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement stmt=con.createStatement();
String s1="insert into emp3 values(1,'rahul')";
String s2="insert into emp3 values(2,'arun')";
String s3="insert into emp3 values(3,'pavan')";
String s4="insert into emp3 values(4,'gopi')";
String s5="insert into emp3 values(2,'arun')";
String s6="insert into emp3 values(2,'arun')";
stmt.addBatch(s1);
stmt.addBatch(s2);
stmt.addBatch(s3);
stmt.addBatch(s4);
stmt.addBatch(s5);
stmt.addBatch(s6);
int a[]=stmt.executeBatch();
for(int i=0;i<=a.length;i++) { System.out.println("no of rows inserted:------->"+a[i]);
}
}
}