You are on page 1of 6

1.

import java.sql.*;
class jdbc1
{
public static void main(String args[]) throws Exception
{
//driver registration
Class.forName("oracle.jdbc.driver.OracleDriver");
//establish connection
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");
}
}

2.
import java.sql.*;

class jdbc2
{
public static void main(String args[]) throws Exception
{

//establish connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");
String str="create table student(sid number(10),sname
varchar2(20),saddr varchar2(20))";
Statement stmt=con.createStatement();
stmt.executeUpdate(str);
System.out.println("table created");
}
}

3.
import java.sql.*;
class jdbc3
{
public static void main(String args[]) throws Exception
{

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");
Statement stmt=con.createStatement();
//values insert
String str1="insert into demo1 values(1,'arun','hyd')";
stmt.executeUpdate(str1);
System.out.println("values inserted successfully");

}
}

4.
import java.sql.*;
import java.util.*;

public class jdbc4 {


public static void main(String args[]) throws Exception
{
//establish connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");
Statement stmt = conn.createStatement();
String str = "update student set sname = 'xxx' where id =
100 ";
int count= stmt.executeUpdate(strUpdate);
System.out.println(count+ " records updated");
}
}

5.
import java.sql.*;
import java.util.*;

public class jdbc5


{
public static void main(String args[]) throws Exception
{
//establish connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");
Statement stmt = conn.createStatement();
String sqlDelete = "delete from books where id == 101";
int count= stmt.executeUpdate(sqlDelete);
System.out.println(countDeleted + " records
deleted.\n");
}
}

6.
import java.sql.*;
import java.util.*;

public class jdbc6 {


public static void main(String args[]) throws Exception
{
//establish connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");
Statement stmt = conn.createStatement();
String str = "select * from student ";
System.out.println("The SQL statement is: " + strSelect + "\n");

ResultSet rset = stmt.executeQuery(strSelect);


while(rs.next())
{ // Move the cursor to the next row
System.out.println(rs.getInt("id") + " " +
rs.getString("sname") + " " + rs.geString("addr") );
}
}
}

7.
import java.sql.*;
import java.util.*;

public class jdbc7{


public static void main(String args[]) throws Exception
{
//establish connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");
//values inserting
String str1="insert into s17 values(?,?,?)";
PreparedStatement pst=con.prepareStatement(str1);
Scanner sc=new Scanner(System.in);
int sid=sc.nextInt();
String sname=sc.next();
String saddr=sc.next();
pst.setInt(1,sid);
pst.setString(2,sname);
pst.setString(3,saddr);
pst.executeUpdate();
System.out.println("row inserted successfully");

}
}

8.
package databasepro;
import java.util.*;
import java.sql.*;

public class jdbc8


{

public static void main(String[] args) throws Exception


{
//establish connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ora
cle");
if(con!=null)
System.out.println("connection successful");
else
System.out.println("connection not successful");

//multiple values inserting


String str1="insert into demo1 values(?,?,?)";
PreparedStatement stmt=con.prepareStatement(str1);
Scanner sc=new Scanner(System.in);
System.out.println("enter no of records?");
int nr = sc.nextInt();

for(int i=1;i<=nr;i++)
{
int sid = sc.nextInt();
String sname = sc.next();
String saddr = sc.next();

stmt.setInt(1,sid);
stmt.setString(2,sname);
stmt.setString(3,saddr);
stmt.executeUpdate();
}

System.out.println(nr+"Rows inserted");
}

9.
import java.sql.*;
class jdbc9
{
public static void main(String args[])throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","or
acle");
PreparedStatement ps=con.prepareStatement("select * from emp");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Total columns: "+rsmd.getColumnCount());
System.out.println("Column Name of 1st column:
"+rsmd.getColumnName(1));
System.out.println("Column Type Name of 1st column:
"+rsmd.getColumnTypeName(1));

}
}

10.Java Example to store image in the database


import java.sql.*;
import java.io.*;
public class jdbc10
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","or
acle");
PreparedStatement ps=con.prepareStatement("insert into
imgtable values(?,?)");
ps.setString(1,"sonoo");
FileInputStream fi=new FileInputStream("d:\\Prasad.jpg");
ps.setBinaryStream(2,fi,fi.available());
int i=ps.executeUpdate();
System.out.println(i+" records updated");
}
}

11.Example to retrieve image from Oracle database

import java.sql.*;
import java.io.*;
public class jdbc11
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","or
acle");
PreparedStatement ps=con.prepareStatement("select * from
imgtable");
ResultSet rs=ps.executeQuery();
if(rs.next())
{//now on 1st row
Blob b=rs.getBlob(2);//2 means 2nd column data
byte arr[]=b.getBytes(1,(int)b.length());//1 means first image
FileOutputStream fo=new
FileOutputStream("d:\\Prasad.jpg");
fo.write(arr);
}
}

You might also like