Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL string format for connecting to Oracle database with JDBC

I'm a newbie to Java-related web development, and I can't seem to get a simple program with JDBC working. I'm using off-the-shelf Oracle 10g XE and the Eclipse EE IDE. From the books and web pages I've checked so far, I've narrowed the problem down to either an incorrectly written database URL or a missing JAR file. I'm getting the following error:

java.sql.SQLException: No suitable driver found for jdbc:oracle://127.0.0.1:8080

with the following code:

import java.sql.*;  public class DatabaseTestOne {     public static void main(String[] args) {         String url = "jdbc:oracle://127.0.0.1:8080";         String username = "HR";         String password = "samplepass";          String sql = "SELECT EMPLOYEE_ID FROM EMPLOYEES WHERE LAST_NAME='King'";         Connection connection;         try {             connection = DriverManager.getConnection(url, username, password);             Statement statement = connection.createStatement();             System.out.println(statement.execute(sql));             connection.close();         } catch (SQLException e) {             System.err.println(e);         }     } } 

What is the proper format for a database URL, anyways? They're mentioned a lot but I haven't been able to find a description.

EDIT (the resolution):

Based on duffymo's answer, I got ojdbc14.jar from Oracle's download site and dropped it in the Eclipse project's Referenced Libraries. Then I changed the start of the code to

... // jdbc:oracle:thin:@<hostname>:<port>:<sid> String url = "jdbc:oracle:thin:@GalacticAC:1521:xe"; ... 

and it worked.

like image 385
Pops Avatar asked Jun 28 '09 02:06

Pops


People also ask

What is the correct format of JDBC URL?

jdbc:mysql://myhost1:3306,myhost2:3307/db_name. jdbc:mysql://[myhost1:3306,myhost2:3307]/db_name.

What is the connection URL in Oracle for JDBC?

Connection URL: The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver, localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port number and XE is the Oracle service name.

How do I find the URL of a JDBC connection?

In the above example, we first obtain the Connection instance. Then, we call the getMetaData method on our Connection to get the DatabaseMetaData. Finally, we call the getURL method on the DatabaseMetaData instance. As we'd expect, it returns the URL of our database.

How can we connect Oracle database using JDBC example?

Establish connection to Oracle database With JDBC, we can establish a database connection by calling the method getConnection() of the DriverManager class. There are three versions of this method: static Connection getConnection(String url) static Connection getConnection(String url, Properties info)


1 Answers

There are two ways to set this up. If you have an SID, use this (older) format:

jdbc:oracle:thin:@[HOST][:PORT]:SID 

If you have an Oracle service name, use this (newer) format:

jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE 

Source: this OraFAQ page

The call to getConnection() is correct.

Also, as duffymo said, make sure the actual driver code is present by including ojdbc6.jar in the classpath, where the number corresponds to the Java version you're using.

like image 181
Pops Avatar answered Sep 17 '22 02:09

Pops