·  JAVA – DB – Oracle – Connect – JDBC Thin Driver

 

- Info:

  - This tutorial shows how to use Java to connect to Oracle Database using JDBC Thin Driver.

  - JDBC Thin Driver directly connects to DB through TCP/IP protocol using connection string:

    DriverManager.getConnection(jdbc:oracle:thin:myUser/myPassword@ivordesktop:1521:XE);        or

    DriverManager.getConnection(jdbc:oracle:thin:@ivordesktop:1521:XE","myUser","myPassword");

  - Connection string defines:

    - ivordesktop

 

as server on which ORACLE DB is installed

    - 1521

 

as port on which ORACLE DB listens

    - XE

 

as ORACLE DB SERVICE_NAME since more DBs can listen on the same port

    - myUser  

 

as schema which exists on that DB and contains data we are interested in

    - myPassword

 

as password for schema myUsername

  - File tnsnames.ora is not used by JDBC Thin Driver but if you have this file as part of your Oracle Client or

    Oracle DB installaton you can use it to find highlighted connection string parameters.

 

- JARs:

  - JDBCDriver_Oracle10g.jar

 

- Example:

  - Create "JAVA Application" Test.java.

 

    Test.java

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.ResultSet;

    import java.sql.Statement;

    

    public class Test {

    

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

    

        //CONNECT.

        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

        Connection con =DriverManager.getConnection("jdbc:oracle:thin:myUser/myPassword@vori:1521:XE");

    

        //GET DATA.

        Statement stmt = con.createStatement();

        ResultSet rs   = stmt.executeQuery("SELECT 1 FROM DUAL");

        while (rs.next()) {

          System.out.println(rs.getString(1));

        }

        

        //CLOSE.

        rs.close();

        con.close();

      }

      

    }

    

    tnsnames.ora      (H:\Installed\Programming\Oracle10gXE\app\oracle\product\10.2.0\server\NETWORK\ADMIN)

    XE =

      (DESCRIPTION =

        (ADDRESS = (PROTOCOL = TCP)(HOST = vori)(PORT = 1521))

        (CONNECT_DATA =

          (SERVER = DEDICATED)

          (SERVICE_NAME = XE)

        )

      )