0
        <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.

3
  • 2
    Yes, it's required. You can't connect to a database without its driver. The driver is the piece of code which knows how to communicate with the database, because every database has its own network protocol, types, etc. Commented Jan 11, 2020 at 17:40
  • 1
    Why do you think the tomcat-jdbc dependency would give you the Oracle driver? You need to add the dependency for the Oracle JDBC driver as well. Commented Jan 11, 2020 at 17:42
  • @Aki T check my answer. Commented Jan 11, 2020 at 18:57

3 Answers 3

2

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why should he connect with that code? Using DriverManager instead of a DataSource like the OP does in his code is a bad choice in a web application
1

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).

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.