简体   繁体   中英

Java: Problem with Code for Database Connection

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;
    }
}

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

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;
    }

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;
    }
}

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