0

I've searched a lot and spent many time trying register JDBC driver.

First, I copied my ojdbc7.jar file (downloaded from Oracle) into directory shown below:

Driver File(s): /Users/Kamil/glassfish4/jdk7/jre/lib/ext/ojdbc7.jar
Driver Class: oracle.jdbc.OracleDriver
// this is copied from Services/Databases/Drivers/ojdbc

Then, I tried following code:

try {
    Driver myDriver = new oracle.jdbc.driver.OracleDriver();
    DriverManager.registerDriver(myDriver);
} catch (ClassNotFoundException ex) {
    System.out.println("Error: unable to load driver class!");
    System.exit(1);
}

... and this one:

try {
    Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException ex) {
    System.out.println("Error: unable to load driver class!");
    System.exit(1);
}

... and with this line instead:

Class.forName("oracle.jdbc.driver.OracleDriver");

I always get ClassNotFoundException :(

Here is code I try to run:

Connection DBconn;
String USER = "root";
String PASS = "root";
System.out.println("Connecting to database...");

DBconn = DriverManager.getConnection("mysql://localhost:3306/RestToolDatabase", USER, PASS);

System.out.println("Creating statement...");
Statement stmt = DBconn.createStatement();
String sql;
sql = "select surname, id, age\n"
        + "from customers \n"
        + "where name = \"maria\" \n"
        + "order by id;";
ResultSet rs = stmt.executeQuery(sql);

I've also read about setting the classpath as described here:

  • Right-click your Project.

  • Select Properties.

  • On the left-hand side click Libraries.

  • Under Compile tab - click Add Jar/Folder button.

    but there is no "Properties/Libraries" option in NetBeans...

I use Maven and there is following dependency added by some library:

<dependency>
    <groupId>ojdbc</groupId>
    <artifactId>ojdbc</artifactId>
    <version>14</version>
    <type>pom</type>
</dependency>

Maybe there is some workaround, or other way to add it automatically? It SHOULD be simple but I'm unexperienced and wasted many time on this. Please help.

EDIT: Thank you for replies, yes, I use MySQL Server at localhost:3306 [root]. I have MySQL JDBC connector installed here:

/Applications/NetBeans/NetBeans 8.0.app/Contents/Resources/NetBeans/ide/modules/ext/mysql-connector-java-5.1.23-bin.jar

When I go to "Services" --> "Drivers" --> "MySQL (Connector/J driver)" there is Driver Class path exactly as you suggested, so I use Class.forName("com.mysql.jdbc.Driver") now. I right-clicked on "MySQL (Connector/J driver)" driver and went to "Connect Using..." --> "localhost, port 3306, user, password". And it is connected now, I see new connection. But still I get ClassNotFoundException.

EDIT 2 - this solution worked for me:

I added following to dependencies in pom.xml:

    </dependency>
        <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.23</version>
</dependency>

... and builded application; the driver was downloaded and installed. As simple as that... I spent many time on this... It works - yeah! :)

Community
  • 1
  • 1
kiedysktos
  • 3,910
  • 7
  • 31
  • 40
  • 1a) don't use `lib/ext`, 1b) are you sure that is the JVM that is running your application, 2) are you connecting to Oracle, or MySQL – Mark Rotteveel Aug 04 '14 at 15:45
  • Re your edit: That is only internal to Netbeans, when executing an application, the driver needs to be **on the classpath of the application**. I'd say the _related_ sidebar should have sufficient questions with same or similar problems and solutions. – Mark Rotteveel Aug 05 '14 at 08:23
  • OK, I see. May I ask where is the app class path? Is this ProjectName/ folder or ProjectName/src/ or ProjectName/target/? I assume I should remove driver from NetBeans internal classpath. – kiedysktos Aug 05 '14 at 10:46
  • When running from within Netbeans, it is the build path configured for the project inside Netbeans. Outside of Netbeans it depends on the deployment etc and is not something explained in a short answer. And no, you shouldn't remove it from NetBeans internal classpath, because most likely it is **used inside Netbeans** if you use its internal database access. – Mark Rotteveel Aug 05 '14 at 14:21

4 Answers4

1

You are using an Oracle JDBC driver on a MySQL database, you should use

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

Edit: Thanks to the comments from @duffymo and @Mark-Rotteveel who noticed that the URL of the connection is also wrong, the correct connection is:

    Connection DBconn;
    String USER = "root";
    String PASS = "root";
    DBconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/RestToolDatabase?" + "user="+USER+"&password="+PASS);
Zaid Malhis
  • 588
  • 4
  • 18
  • Interesting - you're inferring this from the port number 3306, but there's no other indication that the database vendor being used is MySQL. – duffymo Aug 04 '14 at 14:02
  • @duffymo The url in the code is also `mysql://localhost:3306/` (which misses `jdbc:` btw) – Mark Rotteveel Aug 04 '14 at 15:42
  • missed it, thank you. that's not the correct URL for MySQL, either - it's missing the leading bit. jdbc:mysql://localhost:3306/test – duffymo Aug 04 '14 at 15:45
  • @duffymo the port number was the first thing that made me think that it is a MySQL database, then I noticed the mysql part of the URL which made me sure. Also feel free to edit my answer and add the correct URL as you noticed it first :) – Zaid Malhis Aug 04 '14 at 16:48
  • No worries, Zaid, you deserve the credit for being so sharp-eyed. – duffymo Aug 04 '14 at 18:21
  • Thanks @duffymo, I've edited the answer to include the correct URL. – Zaid Malhis Aug 04 '14 at 19:17
  • Thank you for kind replies, please see the edit at the end of question. – kiedysktos Aug 05 '14 at 08:22
1

Setting the classpath :

  1. Download the JAR JDBC Connector/Driver
  2. Copy file in subfolder of project (ex: libs) (not 'src')
  3. Project->Properties->Libraries->Add Jar/Folder->Select file

And packaging dependent librairies with relative path in 'dist' :

  1. Project->Build->Packaging->Check "Copy Dependent Librairies"
Fhamtaom
  • 96
  • 1
  • 3
0

I don't think the JDBC JAR belongs in that directory. The CNF exception backs me up.

I would recommend putting that JAR in your project WEB-INF/lib, repackaging the WAR, and trying again.

This line is correct:

Class.forName("oracle.jdbc.driver.OracleDriver");

The full class name has to match that in the JAR. This is the only one that does.

You have to use the driver that matches your database: Oracle for Oracle, MySQL for MySQL. What database are you using?

duffymo
  • 305,152
  • 44
  • 369
  • 561
0
</dependency>
    <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>

This worked for me. Since there's no Property and Library in Netbeans apache. Good luck.