Aki T
Aki T

Reputation: 666

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver when using Maven Dependency

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>7.0.47</version>
        </dependency>

Above is the maven dependency I'm using.

    PoolProperties p = new PoolProperties();

    p.setUrl("jdbc:oracle:thin:@//ip:port:ora11g");
    p.setDriverClassName("oracle.jdbc.OracleDriver");
    p.setUsername("un");
    p.setPassword("pw");
    p.setJmxEnabled(true);
    p.setTestWhileIdle(false);
    p.setTestOnBorrow(true);
    p.setValidationQuery("SELECT 1");
    p.setTestOnReturn(false);
    p.setValidationInterval(30000);
    p.setTimeBetweenEvictionRunsMillis(30000);
    p.setMaxActive(100);
    p.setInitialSize(10);
    p.setMaxWait(10000);
    p.setRemoveAbandonedTimeout(60);
    p.setMinEvictableIdleTimeMillis(30000);
    p.setMinIdle(10);
    p.setLogAbandoned(true);
    p.setRemoveAbandoned(true);
    p.setJdbcInterceptors(
            "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
                    "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
    DataSource datasource = new DataSource();
    datasource.setPoolProperties(p);

    Connection con = null;
    try {
        con = datasource.getConnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from user");
        int cnt = 1;
        while (rs.next()) {
            System.out.println((cnt++)+". Host:" +rs.getString("Host")+
                    " User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
        }
        rs.close();
        st.close();
    } finally {
        if (con!=null) try {con.close();}catch (Exception ignore) {}
    }

And above is my database querying test code snippet.

When I'm executing the program I'm getting "java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver" exception.

I searched for the issue and read that I have to "make sure oracle jdbc jar is in the classpath". I'm not sure why do I have to set it manually or is it actually required.

Upvotes: 0

Views: 5940

Answers (3)

Nirmala
Nirmala

Reputation: 1338

Note that you can download the 19.3 JDBC drivers from central maven

Upvotes: 0

Aki T
Aki T

Reputation: 666

I resolved the issue by adding the corresponding ojdbc.jar to the project.

It can be resolved by adding the mentioned maven dependency too (mentioned by Joy).

Upvotes: 1

Joy
Joy

Reputation: 424

Use below dependency

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>12.2.0.1</version>
</dependency>

and use below code to connect with your oracle database.

Class.forName("oracle.jdbc.OracleDriver");

String dbURL1 = "jdbc:oracle:thin:{USER}/{PASSWORD}@{URL}:{PORT}:{DBNAME}";
//e.g. String dbURL1 = "jdbc:oracle:thin:tiger/scott@localhost:1521:productDB";
Connection  conn1 = DriverManager.getConnection(dbURL1);
if (conn1 != null) {
    System.out.println("Connected with connection #1");
}

Upvotes: 2

Related Questions