JDBC Oracle
JDBC Thin Driver
See: here to determine which file you need (summarised below)...
Download if the file is not already under $ORACLE_HOME/jdbc/lib
Note: you can copy the jar file from your $ORACLE_HOME and use it on any target.. the jar files are not platform specific and do not need a paid license.You will need the appropriate jar file in your CLASSPATH. For example...
export CLASSPATH=$ORACLE_HOME/jdbc/lib/ojdbc6.jar:.
JDBC Install
OJDBC8
If you have installed JDK8 and you plan to access Oracle 12.2...
Download ojdbc8-full.tar.gz
tar xvf ojdbc8-full.tar.gz
This will create a directory called OJDBC8-Full, which you should add to your CLASSPATH
OJDBC7
If you have installed JDK7 or JDK8 and you plan to access Oracle 12.1...
ojdbc7 and ojdbc6
OJDBC6
If you have installed JDK6 and you plan to access Oracle 12.1... Or if you plan to access Oracle 11.2
ojdbc7 and ojdbc6
Example
At it's simplest, you can then create a .java file containing your Java program (e.g. JDBCtest.java)...
import java.sql.*;
class JDBCtest {
public static void main (String args []) throws SQLException
{
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@machineName:1521/SERVICE", "username", "password"); // @machineName:port:SID, userid, password
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
while (rset.next())
System.out.println (rset.getString(1)); // Print col 1
stmt.close();
}
}
Compile it...
javac JDBCtest.java
Run it..
java JDBCtest
Long form connection string
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myservice)))
Mainframe
On a mainframe you may need to force compatible encoding otherwise you will see strange characters in the listener.log...
To force it to use UTF8 ASCII use...
java -Dfile.encoding=UTF8 JDBCtest
JDBC and Multi-Tenant Architecture
When connecting to a PDB via JDBC you must connect via the SERVICE not the SID, hence the first example below is invalid, the second is valid...
jdbc:oracle:thin:@myserver:1521:mypdb
jdbc:oracle:thin:@myserver:1521/mypdb
JDBC and Oracle Wallet
In addition to adding ojdbcx.jar to the CLASSPATH (as described above) you should also add these additional items to the CLASSPATH...
$ORACLE_HOME/jlib/oraclepki.jar
$ORACLE_HOME/jlib/osdt_cert.jar
$ORACLE_HOME/jlib/osdt_core.jar
Note that you can either add these to the CLASSPATH environment variable or specify then on the java command line using the -cp argument. Using the -cp argument is recommended if you need to cron schedule your programCreate a tnsnames.ora file containing stanzas for the target databases.
Note that if you plan to connect to the same database with different userids from your Java program then you will need to create separate stanzas in the tnsnames.ora file with different names (e.g. DB1U1 and DB1U2).Create a Wallet (if you don't have one already).
Note: It is recommended to create the wallet using orapki with the -auto_login_local option.Add passwords to the wallet.
Remember to only assign one password for each alias in your tnsnames.ora fileYou can then connect using a command similar to this in your Java code...
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:/@TNSALIAS");
To run the code you should define two system properties on the command line to specify the tnsnames.ora and wallet location...
-Doracle.net.tns_admin=/u01/app/oracle/product/12.1.0.2/network/admin
-Doracle.net.wallet_location=/home/oracle/wallet
For example...
/u01/app/oracle/product/12.1.0.2/jdk/bin/java -cp /u01/app/oracle/product/12.1.0.2/jdbc/lib/ojdbc6.jar:/u01/dba/java:/u01/app/oracle/product/12.1.0.2/jlib/oraclepki.jar:/u01/app/oracle/product/12.1.0.2/jlib/osdt_cert.jar:/u01/app/oracle/product/12.1.0.2/jlib/osdt_core.jar -Doracle.net.tns_admin=/u01/app/oracle/product/12.1.0.2/network/admin -Doracle.net.wallet_location=/home/oracle/wallet myclass
Bibliography
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/jjdbc/JDBC-getting-started.html
1498108.1 Certification : Oracle JDBC driver 11.2.0.3 Supports JDK 1.7401934.1 Starting With Oracle JDBC Drivers
http://searchoracle.techtarget.com/tip/Accessing-non-Oracle-databases-using-stored-procedures-and-JDBC
OTN - Configure JDBC Driver in SQL Developer
https://stackoverflow.com/questions/6031877/jvm-property-dfile-encoding-utf8-or-utf-8https://stackoverflow.com/questions/13163715/package-oracle-jdbc-driver-does-not-existhttps://stackoverflow.com/questions/24398953/error-cannot-find-symbol-for-class-connection-and-class-statementhttps://stackoverflow.com/questions/43884504/jdbc-thin-connection-string-in-oracle-uses-both-colon-and-forward-slashhttps://stackoverflow.com/questions/7634196/what-is-correct-jdbc-url-syntax-if-oracle-wallets-are-used/9772728
http://razorsql.com/articles/oracle_jdbc_connect.html
https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html