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