sri bharath
sri bharath

Reputation: 83

Java JDBC Connection ClassNotFoundException Error

I am a beginner in java and have a problem in connection of JDBC connections I get a "java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver" Error running the code. here's my source code


 import java.sql.*;
    public class Connect 
    {
        public static void main(String[] args) 
        {
            try
            {
                Class.forName("oracle.jdbc.OracleDriver");
                System.out.println("Drivers Loaded");
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:SYSTEM/rambabu@localhost:8081:XE");
                System.out.println("Connection established");
                con.close();
            }
            catch(Exception e)
                {
                    System.out.println(e);
                }
    }
}

Upvotes: 2

Views: 2510

Answers (5)

ChadNC
ChadNC

Reputation: 2503

In the projects menu, right click on the libraries folder, select add jar/folder, then select the ojdbc jar and it will be added to the projects library and you should be able to use the drivers.

Try something simple like the following to test the connection

Connection conn = null;
try{
  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  conn = DriverManager.getConnection("dburl","username", "password");
  if(conn != null){
    System.out.println("Connection to Phoenix unsuccessful");
  }else{
    System.out.println("Connection to Phoenix successful");
  }

}catch(SQLException e){
  System.out.println("Exception creating DB connection: " + e);
  for(StackTraceElement ste : e.getStackTrace())
    System.out.println(ste.toString());
}

Upvotes: 0

EvenLisle
EvenLisle

Reputation: 4812

It could be that the .jar-file containing the jdbc-driver is not in the "referenced libraries". If you're developing in Eclipse, you can just right click the project > Build path > configure build path > Libraries tab > add external jars > locate and add your version of the jdbc driver.

Hope it helps.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Try it this way....

public class DataBaseClass {

    Connection conn;

    public void receivedConnection() {



        try {
            conn = getConnection();
            System.out.println("I GOT THE CONNECTION");



        } catch (SQLException e) {

            System.out.println("I DID NOT GET THE CONNECTION");
            e.printStackTrace();
        }

        try {

            Statement stat = conn.createStatement();
            stat.executeUpdate("DROP TABLE VIVEK_DA_TABLE");
        } catch (SQLException e) {
            System.out.println("Table didnt exist");
            //e.printStackTrace();
        }

    }


    public static Connection getConnection() throws SQLException{

        String drivers = "com.mysql.jdbc.Driver";
        String url    = "jdbc:mysql://localhost:3306/test";
        String username = "root";
        String password = "vicky";

        System.setProperty(drivers,"");
        return DriverManager.getConnection(url,username,password);

    }

    public static void main(String[] args) throws SQLException{

        DataBaseClass db = new DataBaseClass();
        db.receivedConnection();
    }

}

Upvotes: 0

aaa
aaa

Reputation: 786

You will need the Oracle JDBC driver in your classpath.

If you dont have it you can download it from http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503270

You just need to put the Oracle driver jar file in your classpath. For example:

java -cp oracle.jar Connect

(I don't know what the jar file is called off-hand, but presumably you have one...)

Upvotes: 2

Related Questions