|
| 1 | +/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/ |
| 2 | +/* |
| 3 | + DESCRIPTION |
| 4 | + The server-side Type 2 (T2S) driver (aka KPRB driver) is for Java in the |
| 5 | + database. Java running in the database session uses the KPRB driver or |
| 6 | + T2S driver to access data, locally. |
| 7 | + We furnish the server-side thin JDBC (aka Type 4 server driver) for |
| 8 | + accessing data in other session in the same database or a remote Oracle |
| 9 | + database. |
| 10 | + |
| 11 | + Step 1: Connect to SQLPLUS using the database USER/PASSWORD. |
| 12 | + Make sure to have ServersideConnect.sql accessible on the |
| 13 | + client side to execute. |
| 14 | + Step 2: Run the SQL file after connecting to DB "@ServersideConnect.sql" |
| 15 | +
|
| 16 | + NOTES |
| 17 | + Use JDK 1.6 and above |
| 18 | +
|
| 19 | + MODIFIED (MM/DD/YY) |
| 20 | + nbsundar 03/23/15 - Creation (kmensah - Contributor) |
| 21 | + */ |
| 22 | +import java.sql.Connection; |
| 23 | +import java.sql.ResultSet; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.sql.Statement; |
| 26 | + |
| 27 | +import oracle.jdbc.driver.OracleDriver; |
| 28 | +import oracle.jdbc.pool.OracleDataSource; |
| 29 | + |
| 30 | + |
| 31 | +public class ServersideConnect { |
| 32 | + |
| 33 | + static public void jrun() throws SQLException { |
| 34 | + // For testing ServersideConnect |
| 35 | + // test("jdbc:oracle:kprb:@"); |
| 36 | + method1("jdbc:default:connection"); |
| 37 | + method2(); |
| 38 | + } |
| 39 | + /* |
| 40 | + * Shows using the server side Type 2 driver a.k.a KPRB driver |
| 41 | + */ |
| 42 | + static public void method1(String url) throws SQLException { |
| 43 | + Connection connection = null; |
| 44 | + try { |
| 45 | + System.out.println("Connecting to URL " + url); |
| 46 | + // Method 1: Using OracleDataSource |
| 47 | + OracleDataSource ods = new OracleDataSource(); |
| 48 | + ods.setURL(url); |
| 49 | + connection = ods.getConnection(); |
| 50 | + System.out.println("Method 1: Getting Default Connection " |
| 51 | + + "using OracleDataSource"); |
| 52 | + // Perform database operation |
| 53 | + printEmployees(connection); |
| 54 | + |
| 55 | + } catch (SQLException e) { |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /* |
| 62 | + * Method to show that a connection is obtained without specifying the URL. |
| 63 | + * This method uses the default connection to establish the connection. |
| 64 | + */ |
| 65 | + |
| 66 | + static public void method2() throws SQLException { |
| 67 | + Connection connection = null; |
| 68 | + // Method 2: Using defaultConnection() method |
| 69 | + OracleDriver ora = new OracleDriver(); |
| 70 | + connection = ora.defaultConnection(); |
| 71 | + System.out.println("Method 2: Getting Default Connection " |
| 72 | + + "using OracleDriver"); |
| 73 | + // Perform database operation |
| 74 | + printEmployees(connection); |
| 75 | +} |
| 76 | + /* |
| 77 | +
|
| 78 | + /* |
| 79 | + * Displays employee_id and first_name from the employees table. |
| 80 | + */ |
| 81 | + static public void printEmployees(Connection connection) throws SQLException { |
| 82 | + ResultSet resultSet = null; |
| 83 | + Statement statement = null; |
| 84 | + try { |
| 85 | + statement = connection.createStatement(); |
| 86 | + resultSet = statement.executeQuery("SELECT employee_id, first_name FROM " |
| 87 | + + "employees order by employee_id"); |
| 88 | + while (resultSet.next()) { |
| 89 | + System.out.println("Emp no: " + resultSet.getInt(1) + " Emp name: " |
| 90 | + + resultSet.getString(2)); |
| 91 | + } |
| 92 | + } |
| 93 | + catch (SQLException ea) { |
| 94 | + System.out.println("Error during execution: " + ea); |
| 95 | + ea.printStackTrace(); |
| 96 | + } |
| 97 | + finally { |
| 98 | + if (resultSet != null) resultSet.close(); |
| 99 | + if (statement != null) statement.close(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
0 commit comments