File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Tryin' to create a small MS SQL Java Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Tryin Watch "Tryin New topic
Author

Tryin' to create a small MS SQL Java Application

Todd Jaspers
Greenhorn

Joined: Aug 29, 2005
Posts: 23
Hey guys,

I'm getting bummed. I'm trying to create a VERY simple Java Application that submits a select statement to MS SQL, and returns the data from a small table.

I just installed some 2005 MS SQL Beta-1 driver from the Microsoft Website that says it will allow me to connect to an MS SQL database.

I'm using Eclipse, and I'm really not sure what I'm doing.

In the instructions it says:


The following is an example of the CLASSPATH statement used for a Windows application:

CLASSPATH =.;C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.0\enu\sqljdbc.jar

The following is an example of the CLASSPATH statement used for a Unix/Linux application:

CLASSPATH =.;/home/usr1/mssqlserver2005jdbc/Driver/sqljdbc_1.0/enu/sqljdbc.jar



Where exactly am I supposed to define this?


Any help would be greatly appreciated.


Thanks!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

To compile the application the MS SQL JDBC driver files need to be in your projects build path. Look at the properties of the project in eclipse - it should become obvious if you have as poke around.

When it comes to running the application, you will need to use a classpath statement as the docs suggest. If you want oto run the applcication in Eclipse too, just create a Run configuration and have it reuse your project's build path (which is the default anyway).

(By the way - there is an IDE forum, this might have been better placed there)


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
May I suggest to begin with that you try the http://jtds.sourceforge.net/ driver instead? It is a driver for SQL server that is open source and free. I am recommending this because I have used it quite successfully for some time now and from all the problems that people have had over time with the MS drivers I would be very wary of them. Again that is just my experience I am not trying to start any MS/non-MS conflict but I just know that the JTDS driver works better.

To address your specific issue you seem to be asking how to set the classpath for your application. I can give you a long rambling explanation about classpaths but I think your question is actually better suited in the General Java Forum http://www.coderanch.com/forums/f-33/java and since the people who answer questions in there will have more experience in helping resolve classpath issues you will probably get a better answer than I can give.

Just trying though... you pass the classpath (which is all the places that the VM should look for the bits of your application) as a parameter to java or javaw at runtime.

Example...

If your program was called DBTest

java -classpath .;C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.0\enu\sqljdbc.jar DBTest
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Originally posted by Paul Sturrock:
To compile the application the MS SQL JDBC driver files need to be in your projects build path.


I know the policy here is be nice so please do not be offended when I say that that statement is incorrect.

99% of the time you do not need the driver libraries for any JDBC driver to compile. The only exception is when you are doing something driver specific for example when dealing with an Oracle BLOB. But that is a rarity most always you should not need the driver to compile.

If you do then you have sort of defeated the point of having interfaces in java.sql at all. You do need the actual driver at runtime but not compile time.
Todd Jaspers
Greenhorn

Joined: Aug 29, 2005
Posts: 23
Ok, thanks guys, sorry about that. I guess I just assumed this was the main database message board.


Thanks!

Todd
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
This is the main JDBC place yes. But your question is a more general problem that one can encounter when using any third-party jar file.

Maybe this will help you? http://mindprod.com/jgloss/classpath.html
Todd Jaspers
Greenhorn

Joined: Aug 29, 2005
Posts: 23
Thanks Maxamillion,

To be honest, if anyone could direct me to an example application that shows basically... how to connect, submit a query, and retrieve the results from even something like an MS Access database, that would really help me out.



Thanks!
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Okay just give me a few moments....

I did notice though that you also posted this question at http://forum.java.sun.com/thread.jspa?threadID=666033&tstart=0 I am not entirely sure of the policy here but that may not be a done thing around here...

Just give me 20 minutes....
Todd Jaspers
Greenhorn

Joined: Aug 29, 2005
Posts: 23
Thanks Maximillion, I really appreciate it...

Sorry for a double post, I guess I figured if it didn't belong in here, I would repost it there. I am still a "green horn" after all... heh.

I really appreciate the help... thank you...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

"Todd J.",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Todd Jaspers
Greenhorn

Joined: Aug 29, 2005
Posts: 23
"Todd J.",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.


Sorry about that, I'm usually weird about putting my full name on the internet. But I've corrected it.



Thanks,

Todd
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
All right here we go...

Now a few notes. This program uses the Jtds driver. So if you want to use yours you need to make the appropriate changes including your class path.

The database I used is SQL Server although that doesn't matter... what is important is that I created a table called tblTest with three fields. An autoincrementing int field that is my primary key called id and two varchar fields called firstname and last name.

To compile this program all you need to do is

javac DBTest.java

To run the program as is you would do

java -classpath .;jtds-0.9.jar DBTest

In my case my jar is in the directory with the program but you can make the neccessary modifications. There are plenty of comments but essentially you will need to change the connection variables and if you use a different driver than change the class name and class path as needed.




[edit: I fixed the formatting (which was scrolling off the page). Is there a preview option I am missing? ]
[ September 22, 2005: Message edited by: Maximilian Stocker ]
Todd Jaspers
Greenhorn

Joined: Aug 29, 2005
Posts: 23
Originally posted by Maximilian Stocker:
All right here we go...

Now a few notes. This program uses the Jtds driver. So if you want to use yours you need to make the appropriate changes including your class path.

The database I used is SQL Server although that doesn't matter... what is important is that I created a table called tblTest with three fields. An autoincrementing int field that is my primary key called id and two varchar fields called firstname and last name.

To compile this program all you need to do is

javac DBTest.java

To run the program as is you would do

java -classpath .;jtds-0.9.jar DBTest

In my case my jar is in the directory with the program but you can make the neccessary modifications. There are plenty of comments but essentially you will need to change the connection variables and if you use a different driver than change the class name and class path as needed.



Hey, I REALLY appreciate this... thank you very much... this will be extremely helpful. I appreciate you taking the time and writing this for me...
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Maximilian Stocker:

...
99% of the time you do not need the driver libraries for any JDBC driver to compile. The only exception is when you are doing something driver specific for example when dealing with an Oracle BLOB. But that is a rarity most always you should not need the driver to compile....


Very true, you should be making all your calls through the java.sql package anyway. You should not be importing _any_ classes from the jar. Basically make sure it is NOT in your class path at compile time, but that it IS at runtime.

Is this yoru first DB application? If so I would recommend you start with something simple like the MS JET database which is built into windows and that you don't need a driver for as the jdbc-odbc bridge will connect you just fine.

You do actually have a MS SQL server?
Todd Jaspers
Greenhorn

Joined: Aug 29, 2005
Posts: 23
Hey Max, I hope this isn't too stupid of a question....

But I'm getting an error when it attempts to load the database driver.

I downloaded the JTDS JDBC driver, but I'm unsure of where to put it, or how exactly I should define where it is...

The error I get is:




The variables that I've defined look like this:




when it says net.sourceforge.jtds.jdbc.Driver, how can I define this to Java? I noticed there is a Class Path file in the root of the project, can I manipulate that file?

I really apologize for asking this quesiton when it's probably something I should know before I even get to this level...

I guess my question is:

1 - Where do I put the JTDS Jar
2 - Where do I include / define to my project that I've put it somwhere.




Again, thank you SOOO much for the time you've taken to help me, I REALLY REALLY appreciate it.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Todd,

Since you are using eclipse, it's pretty easy. You can place the jar anywhere you want to and then just point the build path to it. Right click your project in the package explorer, select build path/add external archives.

Hope this helps,


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Todd,

Here's an MS SQL Example (replace name, user, and pass with your appropiate values.



Cheers,
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Well the general essence of the problem is that you have to tell java where to find all the parts to your program. This is what the classpath does. When you tell Java to load a class (directly or indirectly) the ClassLoader searches all the parts of the classpath to see if it can find what you want.

By default the classpath is the directory you are running from (command line/shell here) but you can add more stuff to it as you need. You should also note that the class loader only understands to look for class files unless you tell it to look for a jar.

Okay.... now that I've completely muddled you (I hope not) let's try some examples.

Let's say your Java file called DBTest.java is in C:\mystuff

Well then to compile we go

c:\ cd mystuff

c:\mystuff\javac DBTest.java

Now to keep things simple here let's say the jar file is in the same directory.

Our class path is two parts, one the name of the jar file and two the . which means the current directory. The second is important because what happens is once you set the classpath at all it doesn't do the default thing and look in the directory you are running from.

So to recap

c:\mystuff\dir

DBTest.java
DBTest.class
jtds-0.9.jar

c:\mystuff\java -classpath .;jtds-0.9.jar DBTest

The ; seperates out the different elements of your classpath. Now you can have the jar wherever you like.. just give it the right path. Also do note I am using an older driver version... you may well have a newer version with a different name.

Then there is using an IDE. I don't know eclipse so I couldn't tell you. Hopefully someone else can guide you on that. Personally though I think it's valuable knowledge to know how to deal with the classpath yourself. You can certainly have the IDE do it for you but it's still good to know... I think.

[edit: Woefully slow ]
[ September 22, 2005: Message edited by: Maximilian Stocker ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tryin' to create a small MS SQL Java Application