Showing posts with label jdbc-program. Show all posts
Showing posts with label jdbc-program. Show all posts

Monday, February 13, 2017

Jdbc Program to retrieve all data from 'emp' table of oracle database type-4 driver.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc10
{
public static void main(String[] args)
{
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
ResultSet rs=st.executeQuery("select * from emp");
System.out.println("ID\tName\tSalary\tJob");
System.out.println("-----------------------------------------");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\t"+rs.getString(4));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Jdbc Program to update column data of 'emp' table .

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc9
{
public static void main(String[] args)
{
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
int rc=st.executeUpdate("update emp set sal=sal+500");
System.out.println(rc+" row updated");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Jdbc Program to insert data in 'emp' table in Oracle database using type-4 driver.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc8
{
public static void main(String[] args)
{
try
{
// Loading & Register the Type-4 Driver Class(OracleDriver). But this line is optional
Class.forName("oracle.jdbc.OracleDriver");
}
catch(ClassNotFoundException cn)
{
cn.printStackTrace();
}
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String check=null; // to check user want to enter more data or not
do
{
System.out.print("Enter id : ");
int id=Integer.parseInt(br.readLine());
System.out.print("Enter Name : ");
String name=br.readLine();
System.out.print("Enter Salary : ");
float sal=Float.parseFloat(br.readLine());
System.out.print("Enter Designation");
String job=br.readLine();
int rc=st.executeUpdate("insert into emp values("+id+",'"+name+"',"+sal+",'"+job+"')");
System.out.println(rc+" row inserted");
System.out.println("Do you insert more? (yes/no)");
check=br.readLine();
}while(!check.equalsIgnoreCase("no"));
System.out.println("----------------End of Application -----------------");
}
catch(SQLException|IOException e)//Multi Catch Block supported in jdk1.7 or higher version
{
e.printStackTrace();
}
}
}

Jdbc Program to drop 'emp' table by using type-4 driver of oracle database .

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc7
{
public static void main(String[] args)
{
try
{
Class.forName("oracle.jdbc.OracleDriver");
}
catch(ClassNotFoundException cn)
{
cn.printStackTrace();
}
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
st.executeUpdate("drop table emp");
System.out.println("Table Dropped");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Jdbc Program to create table 'emp' in oracle database by using type-4 driver.

To use type-4 driver of oracle database we have to set classpath(if we are using normal  editor like editplus,notepad++) and we have to import that jar file if we are using
IDE(like Eclipse, NetBeans) of that jar file(ojdbc6.jar) provided by database
vendors(Oracle Corporation) which is available in
 C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar.
How to set classpath or import ojdbc6.jar file in our program search in google.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc6
{
public static void main(String[] args)
{
// Loading & Register the Type-4 Driver Class(OracleDriver). But this line is optional
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
//We can also use this driver Class.
//Class.forName("oracle.jdbc.OracleDriver")
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
// Establishing the connection
// this is try with resources can be applied in jdk1.7 or above version
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{// port_No='1522' & database_name='xe'/'orcl' ,  username='system' & password='manager'
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
st.executeUpdate("create table emp(id number(3)primary key,name varchar2(10),sal number(4),job varchar2(10))");
System.out.println("Table created");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Jdbc Program to retrieve all data from 'emp' table of oracle database type-1 driver.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc5
{
public static void main(String[] args)throws ClassNotFoundException
{
// Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:odbc:alt","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
ResultSet rs=st.executeQuery("select * from emp");
System.out.println("ID\tName\tSalary");
System.out.println("--------------------------------------");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Jdbc Program to update column data on 'emp' table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc4
{
public static void main(String[] args)throws ClassNotFoundException
{
// Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:odbc:alt","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
int rc=st.executeUpdate("update emp set sal=sal+500");
System.out.println(rc+" row updated");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Jdbc Program to insert data in 'emp' table in Oracle database using type-1 driver.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc3
{
public static void main(String[] args)throws ClassNotFoundException
{
// Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:odbc:alt","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String check=null; // to check user want to enter more data or not
do
{
System.out.print("Enter id : ");
int id=Integer.parseInt(br.readLine());
System.out.print("Enter Name : ");
String name=br.readLine();
System.out.println("Enter Salary : ");
float sal=Float.parseFloat(br.readLine());
int rc=st.executeUpdate("insert into emp values("+id+",'"+name+"',"+sal+")");
System.out.println(rc+" row inserted");
System.out.println("Do you insert more? (yes/no)");
check=br.readLine();
}while(!check.equalsIgnoreCase("no"));
System.out.println("----------------End of Application -----------------");
}
catch(SQLException|IOException e) //Multi Catch Block supported in jdk1.7 or higher version
{
e.printStackTrace();
}
}
}

Jdbc Program to drop the 'emp' table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc2 {

public static void main(String[] args) throws ClassNotFoundException
{
// Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:odbc:alt","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
st.executeUpdate("drop table emp");
System.out.println("Table Dropped");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Jdbc Program to create a table 'emp' in Oracle database.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc1
{
public static void main(String[] args)throws ClassNotFoundException
{
// Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Establishing the connection
// this is try with resources can be applied in jdk1.7 or above version
try(Connection con=DriverManager.getConnection("jdbc:odbc:alt","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
st.executeUpdate("create table emp(id number(3)primary key,name varchar2(10),sal number(4))");
System.out.println("Table created");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}

Saturday, February 11, 2017

Java Program to insert an image in Oracle database

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class StoreImage {
public static void main(String[] args) {
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{
File f=new File("D:/Lovely/Image/flower1.jpg");
FileInputStream fis=new FileInputStream(f);
PreparedStatement ps=con.prepareStatement("insert into emp2 values(?,?,?)");
ps.setInt(1, 111);
ps.setString(2, "Sameer");
ps.setBinaryStream(3,fis,f.length());
int rc=ps.executeUpdate();
System.out.println(rc+" row inserted");
}
catch(SQLException|IOException e)
{
e.printStackTrace();
}
}
}