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);
                }
    }
}
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
sri bharath
  • 93
  • 3
  • 8

5 Answers5

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

aaa
  • 786
  • 3
  • 14
  • Here's my CLASSPATH from my Environment variables. – sri bharath Jun 22 '12 at 17:06
  • 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? – sri bharath Jun 22 '12 at 17:07
  • 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". – sri bharath Jun 22 '12 at 17:37
  • Thank u. Got the selection error problem solved. But, the "java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver" rises again – sri bharath Jun 22 '12 at 17:51
  • 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" – sri bharath Jun 22 '12 at 18:03
  • Yes it is on port 8081 as i have Tomcat server installed for port 8080. I mentioned port 8081 while installing Oracle 10g. When i type localhost:8081 in the address bar it is directed to the ORACLE DATABASE 10g EXPRESS EDITION LICENSE AGREEMENT. – sri bharath Jun 22 '12 at 18:10
  • Yes got it. Drivers Loaded. Connection established. Thanks a lot friend!! – sri bharath Jun 22 '12 at 18:15
  • The 8081 is the EM console, not the tns listener. – J-16 SDiZ Jun 22 '12 at 18:22
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...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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();
    }

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
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.

EvenLisle
  • 4,672
  • 3
  • 24
  • 47
  • 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? – sri bharath Jun 22 '12 at 17:30
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());
}
ChadNC
  • 2,528
  • 4
  • 25
  • 39