5

Error in the following code ![error occured][1]

import java.sql.*;

public class DBConnect{
public static void main(String a[]) throws SQLException{
    // *package oracle.jdbc.driver does not exist*
    Driver d=new oracle.jdbc.driver.OracleDriver();
    DriverManager.registerDriver(d);
    System.out.println("Driver is registered");
    }
}
user1560820
  • 65
  • 2
  • 2
  • 6
  • 1
    Do you have your Oracle JDBC driver in your class path? – Edwin Dalorzo Oct 31 '12 at 17:40
  • did you add driver jar file to class path – someone Oct 31 '12 at 17:41
  • i have dwnlded ojdbc14.jar and added this path to CLASSPATH but still the error persists – user1560820 Oct 31 '12 at 17:46
  • For starters, the interpretation of "classpath" is dependent on the way how exactly your program is executed. So it might be helpful if you elaborate in detail how exactly you're executing your program, so that we can tell you the right way to set the classpath. You sound like as if you've edited the environment variable `%CLASSPATH%`. This is however **ignored** when your program is executed in an IDE like Eclipse/Netbeans, or when you're executing it as a JAR file by `java -jar` command. – BalusC Oct 31 '12 at 17:49
  • i have downloded the ojdbc14.jar file and placed it in the folder in which i have written this code. and in the environmental variables->system variables i have added this path as CLASSPATH. But still the error persists – user1560820 Oct 31 '12 at 17:53
  • yes i have edited the environmental varible %classpath% . if it is ignored how can i set the classpath then? – user1560820 Oct 31 '12 at 17:56
  • if your jar file and java source is in same location. Use a command prompt and changed directory to that location. and execute following javac -classpath ocjdbc14.jar DBConnect.java – someone Oct 31 '12 at 18:09
  • i am not using any IDE. i will try it later – user1560820 Oct 31 '12 at 18:12
  • my jar file and java source file r in the same location/directory.. still the same error.. – user1560820 Oct 31 '12 at 18:13
  • can you copy past your exception here ? – someone Oct 31 '12 at 18:26

4 Answers4

6

You have to add ocjdbc jar in to your class path and try it like this.

if your jar file and java source is in same location. Use a command prompt and changed directory to that location. and execute following

 javac -classpath ocjdbc14.jar DBConnect.java

and see.

import java.sql.*;
import oracle.jdbc.driver.OracleDriver;

public class DBConnect{
 public static void main(String a[]) {
   try{
    Driver d=new OracleDriver();
    DriverManager.registerDriver(d);
    System.out.println("Driver is registered");
  }catch(SQLException e){
     System.out.println("Error occured "+e.getMessage());
     }
}
 }
someone
  • 6,577
  • 7
  • 37
  • 60
  • Using a full qualified class name XOR using an import does not matter. Adding a jar of course might help... – A.H. Oct 31 '12 at 17:48
  • Note that this isn't the idiomatic way of locaing a JDBC driver (OP's own code also not, but you should preferably not continue on the wrong idiom). – BalusC Oct 31 '12 at 17:49
  • Note that OP's own code had a `throws SQLException` clause which makes the try-catch superfluous, so that's definitely not "the solution". – BalusC Oct 31 '12 at 17:52
3

You need to Add an oracle driver jar to the project build path,

Download Ojdbc14.jar file and put it in your classpath.

vikiiii
  • 9,246
  • 9
  • 49
  • 68
1

You need to do following steps if you are using intellij

  1. Download jdbc7 or any version
  2. Add this jar on following path File>>Project Structure>>Libraries>> Image 1
  3. Click on Modules and add jar there too if you still face issue then you must see following problem Image 2
  4. Now click on problems>>fix>>add to dependency as given below Image 3 Hope this will fix your issue
-3

First run the program in netbeans and add Ojdbc14.jar file into the library of the program and then it will surely execute.

After executing in NetBeans, click Clean & Build Project.... This will create a jar file and then path like java -jar "C:\Users\s\Documents\NetBeansProjects\jdbcTest_course\dist\jdbcTest_course.jar" will be provided.

Enter this into a command prompt (cmd) and it will run.

Gus
  • 3,534
  • 1
  • 30
  • 39
Sid
  • 1