STEPS TO CONNECT TO DATABASE USING JDBC (Type 4 driver)
Software Required: 
1)Oracle 11g Express Edition: For downloading and installing refer-->LINK
2) Eclipse IDE: I have used Juno,you can use any other version.For download link and information refer--->LINK
3) Oracle Driver (ojdbc6.jar file): download from here--->LINK

NOTE:
For JDBC we need a table in the database to work with.When you install 11g, HR schema is automatically created.We will use the LOCATIONS table for this tutorial.
HR schema is locked by default. To unlock it you can login as system

(When you started installing oracle ,it asked for global database name and database password that password is used for the sys, system, sysman, dbsnmp account.)
1.     Try to login as "system" - pass
          SQL> conn system/password;
2.     Unlock "hr"
          SQL> alter user hr account unlock;
3.     Can change password to "hr":
           SQL> alter user hr identified by hr;
4.   Now you can connect to hr
               SQL> conn hr/hr;
            -Success

You can use Locations table for JDBC example.It has following columns
LOCATION_ID            NUMBER
STREET_ADDRESS    VARCHAR
POSTAL_CODE          VARCHAR
CITY                           VARCHAR
STATE_PROVINCE     VARCHAR
COUNTRY_ID             CHAR

STEPS:

1)Start Eclipse and create a new Java project.
File-->New-->Java Project
2)Add the ojdbc6.jar file to the Build path of the project
Right Click on Project-->Build Path--->Configure Build Path
Go to Library tab and Select Add External Jars-->Locate the jar file and click open.
For Snapshots refer Eclipse document

3)Add a new java class and Name it as JDBCProgram
Type the following program:


import java.sql.*;
public class  JDBCProgram {
public static void main(String[] args) {
Statement stmt=null;
Connection con=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
                                                                            "hr","hr");
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from locations");
while(rs.next())
{
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getString(4)+"\t");
System.out.println(rs.getString(5)+"\t");

}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}// nothing we can do
try{
if(con!=null)
con.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}
}
}

Now Run the project to get entire data of Locations table

Note: In con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr") ;
  xe is the Oracle SID, hr is the username and  hr is the password.

To know your oracle Sid you can use following command
 SQL>select instance from v$thread;



Comments

Popular posts from this blog

Servlet Advantages and Disadvantages

The Deployment Descriptor: web.xml

Session Management