Sun's JDBC Tutorial. Run Into a Problem

im working through suns JDBC tutorial but ive run into a problem. when i try to run the application i get a whole lot of exceptions. heres my class for working through with the tutorial:

package JavaPhonebook;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

publicclass JDBCTutorial{

publicstaticvoid main(String[] args){

Connection con;

Statement stmt;

ResultSet srs;

try{

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

con = DriverManager.getConnection("jdbc.derby.COFFEES");

stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

srs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");

while(srs.next()){

String name = srs.getString("COF_NAME");

float price = srs.getFloat("PRICE");

System.out.println(name +" " + price);

}

}catch(ClassNotFoundException ex){

ex.printStackTrace();

}catch(SQLException ex){

ex.printStackTrace();

}

}

}

when i run it i get these exceptions:

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:169)

at JavaPhonebook.JDBCTutorial.main(JDBCTutorial.java:30)

Message was edited by:

Alex1989

[2945 byte] By [Alex1989a] at [2007-11-27 4:12:33]
# 1
You have to include the driver in the class path.Check the tutorial to see where you can download this driverand how to set the classpath when running your application.
rym82a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 2
Need to download the derby package from apache website and add the jar files to teh CLASSPATH.
profilemia at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 3
i downloaded Java DB 10.2.2 and registered it with NetBeans 5.5here is a picture of the drivers that are available to me: http://www.myonlineimages.com/serveFile.aspx?af=10947so im guessing this aint enoughMessage was edited by: Alex1989
Alex1989a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 4

> i downloaded Java DB 10.2.2 and registered it with

> NetBeans 5.5

>

Do you know what a classpath is?

How are you running this program? In netbeans.

Did you resolve your previous question? http://forum.java.sun.com/thread.jspa?threadID=5171937 (posted earlier today on same overall topic in JDBC forum)

cotton.ma at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 5

> Do you know what a classpath is?

nope

> How are you running this program? In netbeans.

well i registered java db by going to tools -> options -> advanced options -> ide configuration -> server and external tool settings -> java db database and then choosing the java db location and database location

> Did you resolve your previous question?

yea i did thnx :)

Alex1989a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 6

> > Do you know what a classpath is?

>

> nope

>

Well this (classpath) is the crux of your problem.

> How are you running this program? In netbeans.

>

> well i registered java db by going to tools ->

> options -> advanced options -> ide configuration ->

> server and external tool settings -> java db database

> and then choosing the java db location and database

> location

... this seems to be something netbeansy.

But this did not answer my question really.

All I can tell you is that you need to have all the directories and/or jarfiles your program needs to run in your runtime classpath. Runtime as opposed to compile time. I suspect that this "registration" process of which you speak is related to your compile-time classpath but either not your runtime classpath or you are not running in Netbeans.

cotton.ma at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 7

> Well this (classpath) is the crux of your problem.

d amn. so what exactly is a classpath?

> All I can tell you is that you need to have all the

> directories and/or jarfiles your program needs to run

> in your runtime classpath. Runtime as opposed to

> compile time. I suspect that this "registration"

> process of which you speak is related to your

> compile-time classpath but either not your runtime

> classpath or you are not running in Netbeans.

im not too sure what u just said there haha, but when i double-clicked on javadb-10_2_2_0-windows.exe it extracted a folder called "javadb" which had a whole lot of other folders and files inside of it. this is what i registered to netbeans 5.5

here is a picture of the folders inside javadb:

http://www.MyOnlineImages.com/serveFile.aspx?af=10956

Message was edited by:

Alex1989

Alex1989a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 8

I'll do google for you.

Here is the Tutorial on what is classpath and how to set in Windows

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html

Here is the Tutorial on how to set classpath in Netbeans

http://www.netbeans.org/kb/41/using-netbeans/project_setup.html#manageclasspath

rym82a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 9
im still really confused. i dnt want to add a whole project i just want to add a folder. aint there a all-in-one package that i can dl that has everything configured correctly?
Alex1989a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 10

> im still really confused. i dnt want to add a whole

> project i just want to add a folder. aint there a

> all-in-one package that i can dl that has everything

> configured correctly?

No. It does not work this way. You, the developer must configure it correctly. The advantage of this is that it is easy to add and/or change different pieces and not have to include "everything". Which would be impossible anyway.

What specifically is confusing you?

A recommendation, as given on this site many many times now. Put the IDE aside even for just a few minutes and learn to compile, run and set your classpaths and Jar manifests according by yourself. This will not take days and days to learn, it will not even take hours and hours and the knowledge you gain from this process will be most beneficial.

cotton.ma at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 11

so a classpath is just used to add a project to another project?

i dont think that my problem lies in my classpath. all of the classes in my project run perfectly but its just this database stuff that gives me the exceptions. i went to the properties of one of my databases and it says that its driver is "org.apache.derby.jdbc.ClientDriver".

like i said before, Java DB 10.2.2 has been registered with my NetBeans 5.5 (http://www.myonlineimages.com/serveFile.aspx?af=10947), i have 3 drivers avaliable, my databases can connect but i dont know what driver to use because Class.forName("org.apache.derby.jdbc.EmbeddedDriver")

, doesnt want to work.

im really annoyed that the official tutorial doesnt cover stuff well enough

Alex1989a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 12

> so a classpath is just used to add a project to another project?

No. It's used to tell the compiler or JVM where to find the classes it needs to compile or run the code.

[url=http://wiki.java.net/bin/view/Javapedia/ClassPath]Javapedia: Classpath[/url]

> im really annoyed that the official tutorial doesnt cover stuff well enough

While I understand your frustration, the problem is that you didn't have a firm foundation in the basics (i.e., how to set up your environment) before moving off into more advanced territory (i.e., JDBC).

~

yawmarka at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 13
i changed my projects properties and added my Java DB folder to the compile-time libraries but it still didnt work http://www.MyOnlineImages.com/serveFile.aspx?af=11054
Alex1989a at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 14
> i changed my projects properties and added my Java DB> folder to the compile-time libraries but it still> didnt workIn order to find the driver at runtime, the driver needs to be in the runtime classpath.~
yawmarka at 2007-7-12 9:18:33 > top of Java-index,Java Essentials,Java Programming...
# 15
ok i just added the folder to compile, run, compile tests and run tests and it still doesnt work
Alex1989a at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 16

> ok i just added the folder to compile, run, compile

> tests and run tests and it still doesnt work

If you're still getting a ClassNotFoundException, it's because you haven't yet added the appropriate class or JAR file to the runtime classpath. If "doesn't work" means you're getting some other exception, it's incumbent upon you to provide the actual error message.

~

yawmarka at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 17

i added the JARs 1 by 1 which seems to have helped, now i get:

java.sql.SQLException: No suitable driver found for jdbc.derby.COFFEES

at java.sql.DriverManager.getConnection(DriverManager.java:602)

at java.sql.DriverManager.getConnection(DriverManager.java:207)

at JavaPhonebook.JDBCTutorial.main(JDBCTutorial.java:33)

Alex1989a at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 18

> No suitable driver found for jdbc.derby.COFFEES

That means your database URL is incorrect. For information on how to properly craft a Derby URL, check the following documentation:

http://db.apache.org/derby/docs/dev/getstart/getstart-single.html#rgsquck30197

Hint: A period and a colon are not the same thing.

~

yawmarka at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 19

i checked the properties of my COFFEEBREAK database and it says that its driver is org.apache.derby.jdbc.ClientDriver. i changed its driver to org.apache.derby.jdbc.EmbeddedDriver but then it couldnt connect

aha i see my mistake, thnx for clearing that up for me. but now, as usual, more exceptions arise:

java.sql.SQLException: Database 'COFFEES' not found.

at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)

at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)

at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)

at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)

at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)

at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)

at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(Unknown Source)

at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(Unknown Source)

at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)

at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)

at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)

at java.sql.DriverManager.getConnection(DriverManager.java:582)

at java.sql.DriverManager.getConnection(DriverManager.java:207)

at JavaPhonebook.JDBCTutorial.main(JDBCTutorial.java:33)

Caused by: java.sql.SQLException: Database 'COFFEES' not found.

at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)

at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)

... 14 more

Message was edited by:

Alex1989

Alex1989a at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 20
This error means you have connected to the DB (great!!)But with this user, Database 'COFFEES' not found.Have you tried using the same user and allowed to access this database on a DB Client ?
rym82a at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 21

> This error means you have connected to the DB

> (great!!)

kief finally! :D

> But with this user, Database 'COFFEES' not found.

> Have you tried using the same user and allowed to

> access this database on a DB Client ?

what is a user? whats is a DB Client? sorry im really new to this stuff

Alex1989a at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 22

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String,%20java.lang.String,%20java.lang.String)

I think you should provide url (DB URL) with the username and password.

With the user and password, you will have privileges to do different task.

e.g. Access different DB, Tables, Views, Stored Procedure, etc...

Just like different login ID in windows.

DB Client is A client program which allows you to access your DB with GUI.

e.g. DB Visualizer, TOAD, etc..

rym82a at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...
# 23

WOOHOO IT WORKS! thnx guys :D

i just had 2 change this:

con = DriverManager.getConnection("jdbc:derby://localhost:1527/COFFEEBREAK");

instead of using this:

con = DriverManager.getConnection("jdbc:derby:COFFEEBREAK");

Message was edited by:

Alex1989

Alex1989a at 2007-7-21 21:03:30 > top of Java-index,Java Essentials,Java Programming...