0

I want to compile this Java Code. so I can connect myself to a local Oracle database. But my code doesn't work correcty. It fails at:

Driver myDriver = new oracle.jdbc.driver.OracleDriver();

Could you please tell me how i replace this line ?

package DB_Oracle_Connection;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class dbconf {

private String connstr;
private Connection connect;
public Connection getConnection() throws SQLException {
        connstr = "jdbc:oracle:thin:@localhost:1521:orcl";

        try {
                String uname = "scott";
                String pass = "tiger";

                Driver myDriver = new oracle.jdbc.driver.OracleDriver();
                DriverManager.registerDriver( myDriver );                  


                connect = DriverManager.getConnection(connstr, uname, pass);

        } catch (Exception e) {
            System.out.println(e.toString());
        }

            return connect;
    }
}
2
  • 2
    What's the error message exactly? Commented Jan 16, 2020 at 11:38
  • As suggested by @ShivamPuri, add the stacktrace of the error from the application log. Commented Jan 16, 2020 at 11:54

2 Answers 2

1

Driver's class path should be oracle.jdbc.OracleDriver() whereas you have written it as oracle.jdbc.driver.OracleDriver()

Sign up to request clarification or add additional context in comments.

1 Comment

I have change thje value to Driver myDriver = oracle.jdbc.OracleDriver(); But the problem is the same.
0

Use Class.forName to load the driver. See the below code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Dbconf {

    private String connstr;
    private Connection connect;

    public Connection getConnection() throws SQLException {
        connstr = "jdbc:oracle:thin:@localhost:1521:orcl";

        try {
            String uname = "scott";
            String pass = "tiger";

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

            connect = DriverManager.getConnection(connstr, uname, pass);

        } catch (Exception e) {
            System.out.println(e.toString());
        }

        return connect;
    }

3 Comments

I have changed my code Class.forName("oracle.jdbc.driver.OracleDriver"); This solves my complilation problem. But when i execute my program i get the error java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Add below JAR to your classpath. java2s.com/Code/Jar/o/Downloadojdbc6jar.htm
Hello I had to add ojdbc8.jar in eclipse for my project. Now I can connect to my database. Thank you !

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.