wood burning stoves 2.0*
The moose likes JDBC and the fly likes My first ever jdbc Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "My first ever jdbc Connection" Watch "My first ever jdbc Connection" New topic
Author

My first ever jdbc Connection

Rob McBryde
Greenhorn

Joined: Dec 18, 2010
Posts: 16
Hi All,

Apologies if this question has already been posted hundreds of times before in the past. I'm trying to setup and test my first ever jdbc connection but am having difficulty with my driver.

I'm running Microsoft SQL Server 2008 and have a copy of the jtds driver. Running the following simple test code results in an "Cannot get SQL server connection
java.sql.SQLException: No suitable driver found" error.




Where <username> & <password> have been substituted with my actual username and and password


I am running on a Windows machine and therefore updated my CLASSPATH variable to include the 'jtds-1.2.2.jar' file. I know that my database, username and password are correct as I can access this database via a JavaEE application that uses the same details on my local machine. Is anyone able to advise if my code is incorrect or is it purely that I'm missing something do with allowing my code to actually access the driver/my driver is incorrect ?

I'm still new to Java and jdbc so hoping that I've not missed something really obvious.

Thanks
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

Can you try to register the driver by calling Class.forName("your driver name");

(I think it is "net.sourceforge.jtds.jdbc.Driver" in your case)


OCUP UML fundamental and ITIL foundation
youtube channel
Rob McBryde
Greenhorn

Joined: Dec 18, 2010
Posts: 16
Thanks for the help Jan,

I just tried calling what you suggested and got a ClassNotFoundException so it must be something to do with my CLASSPATH. I think I'll need to do some further reading about how to correctly amend my CLASSPATH.

I tried placing the 'jtds-1.2.2.jar' file in the same package in my code example below but still seems to get a ClassNotFoundException. I'm a bit confused about that.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

If you are running from the command prompt, try running
java -cp .;complete path to your jar\jtds-1.2.2.jar com.dbtest.dbTest

Aside: in Java, the naming convention is to start a class name with an upper case letter.
http://www.oracle.com/technetwork/java/codeconv-138413.html

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My first ever jdbc Connection
 
Similar Threads
ResultSet object returning null
Sockets
java -jar [error] unable to access jarfile postgresql-8.4-702.jdbc.3.jar
connecting to Webtrends reporting DB using JDBC-ODBC
JDBC- mysql Connectivity Problem