Mo.
Mo.

Reputation: 42513

How can I connect to a web based Oracle database with Java?

I have an account, password and the URL.

Upvotes: 0

Views: 543

Answers (2)

spork
spork

Reputation: 1265

Just an addendum to OMG Ponies' answer, you will need a couple of items to proceed: 1. A JDBC driver for Oracle on your build path (Oracle offers these drivers for download) 2. The driverName variable in OMG Ponies' code has to be changed to the name of the specific Oracle driver you're using 3. The serverName variable in OMG Ponies' code should NOT be left at 127.0.0.1 but should instead be changed to the server address you mentioned. I only note this because the way you phrased your question implies an unfamiliarity with computer concepts in general and using databases with Java in particular.

Upvotes: 1

OMG Ponies
OMG Ponies

Reputation: 332571

Use:

Connection connection = null;

try {
   // Load the JDBC driver
   String driverName = "oracle.jdbc.driver.OracleDriver";
   Class.forName(driverName);

   // Create a connection to the database
   String serverName = "127.0.0.1";
   String portNumber = "1521";
   String sid = "mydatabase";
   String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
   String username = "username";
   String password = "password";
   connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
   // Could not find the database driver
} catch (SQLException e) {
   // Could not connect to the database
}

Reference: Connecting to an Oracle Database

Upvotes: 7

Related Questions