tags:

views:

142

answers:

4

I couldn't find out why it shows error. I have created table named books in Oracle priorly.

create table books(num number);

Then I wrote code in Java:

Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
    "jdbc:oracle:thin:@CF:1521:orcl",
    "scott",
    "tiger");
Statement s=con.createStatement();
s.execute("INSERT INTO BOOKS VALUES(123)");
s.close();
con.close();

It shows error as "No suitable driver"

A: 

You need to set your classpath to point to the jar file. The one I used years ago was "ojdbc14.jar". Find the one suitable for you here.

shinkou
+3  A: 

It seems that the oracle driver (v 1.6) is actually "called":

oracle.jdbc.OracleDriver
Bozho
A: 

If you are executing code from command line - make sure that the classpath is set using either $CLASSPATH environment variable or command line argument -cp [pathTo:]ojdbc14.jar or setting the ClassPath: attribute in Manifest.mf of the current executing jar

With Eclipse IDE : Check in the build path the presence of jar / or if build issues exist - check for them in the problems tab.

The version of the jar file (which can be determined from Manifest.mf file) will confirm if you are using the right package for the driver Check if your jar is not corrupt by doing a simple jar -tvf.

techzen
A: 

It shows error as "No suitable driver"

You apparently did System.out.println(e.getMessage()) instead of a e.printStackTrace(). Then you will indeed get that little information. But this message is recognizeable as being a SQLException which can basically have two causes:

  1. The driver is not loaded.
  2. The (wrong) JDBC URL didn't return true for Driver#acceptsURL() for any of the loaded drivers.

To fix 1, you need to ensure that you have a

Class.forName("com.example.jdbc.Driver");

in your code prior to DriverManager#getConnection() call and that you do not swallow/ignore any ClassNotFoundException which can be thrown by it by putting for example an empty catch block or just doing a System.out.println() instead of throwing it.

To fix 2, you need to ensure that the JDBC URL syntax conforms the one as specified in the JDBC driver documentation. In case of Oracle it is located here. Here's a quote:

In JDBC all url's begin with jdbc:protocol: This is the standard. After this is driver specific, and no two drivers are the same.

What is the form of a URL?

The general form of a URL is

jdbc:oracle:<drivertype>:<username/password>@<database>

The <drivertype> is one of

  • thin
  • oci
  • kprb

The <username/password> is either empty or of the form <username>/<password>

In your specific case, the URL looks okay, so you might have not loaded the right driver or plain ignored the ClassNotFoundException thrown by it.

BalusC