56

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.

8 Answers 8

67

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.

1
  • 4
    I highly recommend to use the new format! The old format connects to one specific database instance and one specific database instance only. If you chose to create a database cluster later on, i.e. for scalability, you won't be able to connect to any other node without introducing another connection string. Services provide flexibility, especially for Oracle RAC environments. Planned downtime, coordinated connections and service based performance analysis, this all comes with using /SERVICE and not :SID. The database creates a service equal to the database name by default on installation.
    – gvenzl
    Commented Jan 21, 2015 at 8:48
52

Look here.

Your URL is quite incorrect. Should look like this:

url="jdbc:oracle:thin:@localhost:1521:orcl"

You don't register a driver class, either. You want to download the thin driver JAR, put it in your CLASSPATH, and make your code look more like this.

UPDATE: The "14" in "ojdbc14.jar" stands for JDK 1.4. You should match your driver version with the JDK you're running. I'm betting that means JDK 5 or 6.

5
23

The correct format for url can be one of the following formats:

jdbc:oracle:thin:@<hostName>:<portNumber>:<sid>;  (if you have sid)
jdbc:oracle:thin:@//<hostName>:<portNumber>/serviceName; (if you have oracle service name)

And don't put any space there. Try to use 1521 as port number. sid (database name) must be the same as the one which is in environment variables (if you are using windows).

1
  • 1
    This one covers two databases. This answer should have more upvotes. Take one from me Commented Nov 26, 2018 at 9:17
5

if you are using oracle 10g expree Edition then:
1. for loading class use DriverManager.registerDriver (new oracle.jdbc.OracleDriver()); 2. for connecting to database use Connection conn = DriverManager.getConnection("jdbc:oracle:thin:username/password@localhost:1521:xe");

4

These are the valid, supported JDBC URLs that Oracle accepts:

Connect using the Oracle JDBC driver (oracle.jdbc.pool.OracleDataSource):

String url = "jdbc:oracle:thin:@tcp://my-host:1522/my-service";
OracleDataSource ods = new OracleDataSource();
ods.setUser(userName);
ods.setPassword(password);
ods.setURL(url);
Connection con = ods.getConnection();

The Oracle JDBC Driver supports the following URL formats:

  • EZConnect Format

    Since 19c, the connection properties can be added at the end of the URL. The syntax uses '?' to indicate the start of connection properties and '&' as a delimiter between each property.

    Syntax:

    jdbc:oracle:thin:@[[protocol:]//]host1[,host2,host3][:port1][,host4:port2] [/service_name][:server_mode][/instance_name][?connection properties]

    Examples:

    • jdbc:oracle:thin:@mydbhost:1521/mydbservice
    • jdbc:oracle:thin:@tcp://mydbhost:1521/mydbservice
    • jdbc:oracle:thin:@tcp://mydbhost1,mydbhost2:1521/mydbservice
    • jdbc:oracle:thin:@tcp://mydbhost1:5521,mydbhost2:1521/mydbservice
    • jdbc:oracle:thin:@tcp://mydbhost1:5521/mydbservice:dedicated
    • jdbc:oracle:thin:@mydbhost1:5521/mydbservice?oracle.net.httpsProxyHost=myproxy&oracle.net.httpsProxyPort=80
    • jdbc:oracle:thin:@tcps://mydbhost1:5521/mydbservice?wallet_location=/work/wallet
    • jdbc:oracle:thin:@tcps://mydbhost1:5521/mydbservice?wallet_location=/work/wallet&ssl_server_cert_dn="Server DN"
  • TNS URL Format

    This format is a more structured way of specifying the connect options and more descriptive.

    Syntax:

    jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=<protocol>) (HOST=<dbhost>)(PORT=<dbport>)) (CONNECT_DATA=(SERVICE_NAME=<service-name>))

    Examples:

    • jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP) (HOST=mydbhost)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=mydbservice))
    • jdbc:oracle:thin:@(DESCRIPTION= (LOAD_BALANCE=on) (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host1) (PORT=1521)) (ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=5221))) (CONNECT_DATA=(SERVICE_NAME=orcl)))
    • jdbc:oracle:thin:@(DESCRIPTION= (ADDRESS= (PROTOCOL=TCPS)(PORT=1522)(HOST=myhost)) (CONNECT_DATA=(SERVICE_NAME=dbservicename)) (SECURITY=(ssl_server_cert_dn="CN=testcert.test.com, O=org,L=somecity,ST=state,C=US")))
  • TNS Alias Format

    To use the alias configured in the tnsnames.ora, use the below format. The location of the tnsnames.ora file can be configured using TNS_ADMIN parameter.

    Syntax:

    jdbc:oracle:thin:@<alias_name>

    Examples:

    • jdbc:oracle:thin:@prod_db?TNS_ADMIN=/work/tns/
    • jdbc:oracle:thin:@inst1?TNS_ADMIN=/work/tns/
1
String host = <host name>
String port = <port>
String service = <service name>
String dbName = <db schema>+"."+service
String url = "jdbc:oracle:thin:@"+host+":"+"port"+"/"+dbName
0
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());         
connection = DriverManager.getConnection("jdbc:oracle:thin:@machinename:portnum:schemaname","userid","password");
0

I'm not a Java developer so unfortunatly I can't comment on your code directly however I found this in an Oracle FAQ regarding the form of a connection string

jdbc:oracle:<drivertype>:<username/password>@<database>

From the Oracle JDBC FAQ

http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#05_03

Hope that helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.