Domanda

I get a java.lang.ClassNotFoundException, when I execute the below code. Could someone explain me why I am facing this? All I need to do is to connect to the db and fetch some values from it. Is it a problem with the eclipse that I use?

import java.sql.*;

public class test_sample {

    public static void main(String[] args) {
        try {
              System.out.println("Test1");
              DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

              Class.forName("oracle.jdbc.OracleDriver");
                  System.out.println("Test2");
              Connection con = DriverManager.getConnection(
              "jdbc:oracle:thin:@ussbazudb126.ussb.winson.net:1521/epdev", "manager", "<<PASSWORD>>");
              System.out.println("Test3");
             // Statement st = con.createStatement();
              PreparedStatement meta = con.prepareStatement("select project from isac_extract");
              System.out.println("Test4");
              ResultSet rset = meta.executeQuery();
              while (rset.next()) {
                  String project = rset.getString(1);
                  System.out.println(project);}
              }
              catch (Exception e){
                  e.printStackTrace();
                  }
              finally {System.out.println("Final Block");}

    }

}
È stato utile?

Soluzione

Download appropriate Oracle JDBC driver from here. If you are using Eclipse you need to add ojdbc14.jar which contains the OracleDriver class to your build path. It is usually located in: {ORACLE DRIVER INSTALL PATH}\jdbc\lib\ojdbc14.jar

If you are not using an IDE you need to add the path to that JAR to your -classpath option.

Altri suggerimenti

Go to Project properties (Project -> Properties) -> Java Build Path -> Libraries -> Add external JARs -> select jar with Oracle Driver from your filesystem (you can download it from here if you haven't already). That should help.

Most likely : oracle.jdbc.OracleDriver is missing from your classpath, check it.

Include Oracle JDBC driver in your classpath.

For Oracle 11g, you need to place ojdbc6.jar in Java Classpath. The ojdbc.jar can be found on Oracle home directory E:\app\shyam\product\11.2.0\dbhome_1\jdbc\lib. We can simply put ojdbc6.jar in C:\Program Files\Java\jre7\lib\ext directory instead of defining Java Classpath also.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top