简体   繁体   中英

Using Java to connect to an Oracle database

This Java code compiles fine, but when I try to run it I get:

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

Here is my code:

import java.sql.*;

public class TestConnection {

    public static void main(String[] args) throws Exception {
        //connect to database
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String serverName = "000.000.000.000";
        String portNumber = "1521";
        String sid = "abcd";
        String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
        String username = "user";
        String password = "pass";
        Connection conn = DriverManager.getConnection(url, username, password);
    }
}

How do I get this to work? I am using Ubuntu 11.04 and JDK 6.

Thanks!

You need the Oracle jars.

You can get them from here .

If you're using Maven:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0</version>
</dependency>

Add ojdbcXX.jar -where XX is version number-to Java build path of your project. Aside from the classpath issue, requesting "oracle.jdbc.driver.OracleDriver" is deprecated. For a long time it has been recommended to use: "oracle.jdbc.OracleDriver". For some more recent driver versions, the former will not even work.

Also, you have to add those jars to your project. @Netbeans, you can easily do that at the project-properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM