Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

JDBC Connection to Exadata Express Cloud Database from a Raspberry Pi

0
0

Spoiler Alert: It’s pretty much the same as any other linux system.

The officialdocumentation can be found on the Oracle website.

Install Linux on your Raspberry Pi

I prefer to use BerryBoot, it makes it real easy to install several flavors of Linux on my pi. You can get it from their repo . For this example, I’m using Raspbian.

If you plan to use SSH this is a good guide for setting up passwordless SSH access .

Java Version

Verify that you have the correct Java Version:

JDK 8 JDK8u71 or higher

JDK 7 JDK7u80 or higher

When this post was written, Raspbianwas using an older version of Java 8 so an upgrade is needed.

There are already quite a few guides out there if you need help. I used this one .

JCE Files

Next, we need to download and install the JCE Unlimited Strength Jurisdiction Policy Files .

After you download the .zip file, extract the files and place the two .jar files (local_policy.jar& US_export_policy.jar) in

<java-home>/lib/security.

<java-home> refers the home for the JRE not the JDK. My full path is:

/opt/java/jdk1.8.0_121/jre/lib/security/

Oracle Exadata Express Cloud Database

Now that Linux and Java are setup, it’s time to setup yourOracle Exadata Express Cloud Database.

Log into yourService Console and find the Client Access section.

If client access is not already enabled,you will see a link to Enable Clent Access. Click that link and follow the instructions. Once it’s done it will look like this.


JDBC Connection to Exadata Express Cloud Database from a Raspberry Pi

Click on Download Client Credentials and you’ll get this popup:


JDBC Connection to Exadata Express Cloud Database from a Raspberry Pi

Enter and confirm a password. This is the password that your JDBC client will use to access your credentials.

Click download then extract the file. I extracted mine into my home directory in a directory named EE_Credentials.

/home/pi/EE_Credentials

Create a TestClass

Now we have everything in place we can try it out.

In order to ensure that nothing else is interfering with our test, let’s compile a simple test class.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Simple Oracle JDBC Connection Test
*/
public class testJDBC {
static Connection conn = null;
static Statement stmt = null;
static ResultSet resultSet = null;
static final String URL = "jdbc:oracle:thin:/@dbaccess";
static final String USER = "yourUser";
static final String PW = "yourPassword";
public static void main(String[] args) throws SQLException {
try {
// Returns the Class object associated with the class
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException exception) {
System.out.println("Oracle Driver Class Not found: " + exception.toString());
return;
}
try {
// Get a connection
conn = DriverManager.getConnection(URL, USER, PW);
} catch (SQLException se) {
System.out.println("SQL Exception");
se.printStackTrace();
return;
}
// Creates a Statement object
stmt = conn.createStatement();
// Executes the SQL statement, which returns a single ResultSet object
resultSet = stmt.executeQuery("select user from dual");
if (resultSet.next()) {
System.out.println("Your User: " + resultSet.getString(1));
} else {
throw new SQLException("No results returned");
}
System.out.println("Test completed.");
}
} Replace the strings for your user and password. Save this into a file called testJDBC.java. Copy the oracle driver into the same directory. We’ll manually set the classpath to keep our test self-contained. Compile the class.
javac testJDBC.java Required System Properties

The last thing we need to do is set some system properties using the -D option for Java.

The parameters we’ll be using are:

-Doracle.net.tns_admin=/home/pi/EE_Credentials
-Doracle.net.ssl_server_dn_match=true
-Doracle.net.ssl_version=1.2
-Djavax.net.ssl.trustStore=/home/pi/EE_Credentials/truststore.jks
-Djavax.net.ssl.trustStorePassword=YourPassword
-Djavax.net.ssl.keyStore=/home/pi/EE_Credentials/keystore.jks
-Djavax.net.ssl.keyStorePassword=YourPassword

If you’re using JDK7 you’ll also need to include

-Doracle.net.ssl_cipher_suites=(TLS_RSA_WITH_AES_256_CBC_SHA256)

Remember to replace YourPassword with the password you set above and /home/pi/EE_Credentials with the path you extracted the credentials file into.

To run the test: (all one line)

java -Doracle.net.tns_admin=/home/pi/EE_Credentials -Doracle.net.ssl_server_dn_match=true -Doracle.net.ssl_version=1.2 -Djavax.net.ssl.trustStore=/home/pi/EE_Credentials/truststore.jks -Djavax.net.ssl.trustStorePassword=YourPassword -Djavax.net.ssl.keyStore=/home/pi/EE_Credentials/keystore.jks -Djavax.net.ssl.keyStorePassword=YourPassword -cp .:./ojdbc7.jar testJDBC

If you see the error:

Caused by: oracle.net.ns.NetException: could not resolve the connect identifier “dbaccess”

Make sure the path in the properties is where you extracted the credentials file to. You should see a tnsnames.ora file in there.

Use Environment Variables

If everything worked then we can make it a bit easier by moving those properties to an environment variable.

export JAVA_OPTS="-Doracle.net.tns_admin=/home/pi/EE_Credentials -Doracle.net.ssl_server_dn_match=true -Doracle.net.ssl_version=1.2 -Djavax.net.ssl.trustStore=/home/pi/EE_Credentials/truststore.jks -Djavax.net.ssl.trustStorePassword=YourPassword -Djavax.net.ssl.keyStore=/home/pi/EE_Credentials/keystore.jks -Djavax.net.ssl.keyStorePassword=YourPassword"

Then run it with:

java $JAVA_OPTS -cp .:./ojdbc7.jar testJDBC

Now we can be confident that we can connect to and query from the OracleExadata Express Cloud Database.

To use the connection in your other applications, follow these instructions and read the documentation on setting the Java properties.

For example:

If you’re using Liquibase , set the environment variable like in the example above. If you’re using OpenHab the environment variable would be namedEXTRA_JAVA_OPTS.

Connecting toOracleExadata Express Cloud Database is easy and it provides a secu

Viewing all articles
Browse latest Browse all 12749