2

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

5 Answers 5

3

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

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

8 Comments

Here's my CLASSPATH from my Environment variables.
C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar;C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar; Should i edit any further?
No change, but i added the drivers through eclipse by configuring build path. A new error rises saying "Selection does not contain the main type".
Thank u. Got the selection error problem solved. But, the "java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver" rises again
Yes got it. I think im close. Drivers got loaded but the connection does not get established. it gives an SQLEXception error which says "java.sql.SQLException: Io exception: Got minus one from a read call"
|
2

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

Comments

0

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

}

Comments

0

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.

1 Comment

Yes i added the jar file. But a new error arises while i build the project(Source code given above). Error says "Selection doesnot contain the main type". Is there any problem in the source code?
0

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

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.