JDBC TYPE-2 DRIVER

Connecting to Oracle Database using Thin Driver

To connect a Java application with Oracle database using Thin Driver. You need to follow the following steps
  1. Load Driver Class: The Driver Class for oracle database is oracle.jdbc.driver.OracleDriver andClass.forName("oracle.jdbc.driver.OracleDriver") method is used to load the driver class for Oracle database.
  2. Create Connection: For creating a connection you will need a Connection URL. The Connection URL for Oracle is
    connecting to oracle
    You will also require Username and Password of your Oracle Database Server for creating connection.
  3. Loading jar file: To connect your java application with Oracle, you will also need to load ojdbc14.jarfile. This file can be loaded into 2 ways.
    1. Copy the jar file into C:\Program Files\Java\jre7\lib\ext folder.
      or,
    2. Set it into classpath. For more detail see how to set classpath
NOTE: Here we are discussing about Oracle 10g as database. For other version of Oracle you will be require to do some small changes in the Connection URL.

Example

Create a table in Oracle Database

create table Student(sid number(10),sname varchar2(20));

Insert some record into the table

insert into Student values(101,'adam');
insert into Student values(102,'abhi');

Accessing record from Student table in Java application

import java.sql.*;
class Test
{
public static void main(String []args)
{
try{
//Loading driver
Class.forName("oracle.jdbc.driver.OracleDriver");

//creating connection
Connection con = DriverManager.getConnection
                     ("jdbc:oracle:thin:@localhost:1521:XE","username","password");

Statement s=con.createStatement(); //creating statement

ResultSet rs=s.executeQuery("select * from Student"); //executing statement

while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

con.close(); //closing connection
}catch(Exception e){
e.printStacktrace();
}
}
}
Output
101 adam
102 abhi

Inserting record into a table using java application

import java.sql.*;
class Test
{
public static void main(String []args)
{
try{
//Loading driver...
Class.forName("oracle.jdbc.driver.OracleDriver");

//creating connection...
Connection con = DriverManager.getConnection
                     ("jdbc:oracle:thin:@localhost:1521:XE","username","password");

PreparedStatement pst=con.prepareStatement("insert into Student values(?,?)");
         
         pst.setInt(1,104);
         pst.setString(2,"Alex");
  pst.executeUpdate();

con.close(); //closing connection
}catch(Exception e){
e.printStacktrace();
}
}
}


Connecting to MySQL Database using Thin Driver

To connect a Java application with MySQL database using Thin Driver. You need to follow the following steps
  1. Load Driver Class: The Driver Class for MySQL database is com.mysql.jdbc.Driver andClass.forName("com.mysql.jdbc.Driver") method is used to load the driver class for MySQL database.
  2. Create Connection: For creating a connection you will need a Connection URL. The Connection URL for MySQL is
    connecting to mysql database
    You will also require Username and Password of your MySQL Database Server for creating connection.
  3. Loading jar file: To connect your java application with MySQL, you will also need to loadmysql-connector.jar file. This file can be loaded into 2 ways.
    1. Copy the jar file into C:\Program Files\Java\jre7\lib\ext folder.
      or,
    2. Set it into classpath. For more detail see how to set classpath

Example

Create a table in MySQL Database

create table Student(sid int(10),name varchar(20));

Insert some record into the table

insert into Student values(102,'adam');
insert into Student values(103,'abhi');

Accessing record from Student table in Java application

import java.sql.*;
class Test
{
public static void main(String []args)
{
try{
//Loading driver
Class.forName("com.mysql.jdbc.Driver");

//creating connection
Connection con = DriverManager.getConnection
                ("jdbc:mysql:/ /localhost:3306/test","username","password");

Statement s = con.createStatement(); //creating statement

ResultSet rs = s.executeQuery("select * from Student"); //executing statement

while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

con.close(); //closing connection
}catch(Exception e){
e.printStacktrace();
}
}
}
Output
102 adam
103 abhi

Inserting record into a table using java application

import java.sql.*;
class Test
{
public static void main(String []args)
{
try{
//Loading driver
Class.forName("com.mysql.jdbc.Driver");

//creating connection
Connection con = DriverManager.getConnection
                ("jdbc:mysql:/ /localhost:3306/test","username","password");

PreparedStatement pst=con.prepareStatement("insert into Student values(?,?)");
         
         pst.setInt(1,104);
         pst.setString(2,"Alex");
  pst.executeUpdate();

con.close(); //closing connection
}catch(Exception e){
e.printStacktrace();
}
}
}



No comments:

Post a Comment