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 Connecting to a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting to a database" Watch "Connecting to a database" New topic
Author

Connecting to a database

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I am trying to establish a connection to a HSQL database using jdbc.
I am loosely following an example from Horstmann's book, Big Java.

I have my properties file database.properties, where i am pretty sure the problem lies. I am not sure what i want to add to the jdbc.url line for it to connect.


The other files include SimpleDataSource which reads the properties from the properties file



And then i attempt to establish a connection to the database.



When i run this test file i get this error

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

In the properties file, you typed jbdc instead of jdbc - look carefully, the b and the d have been exchanged, for the keys.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

The "url" variable is null, which is what the error message says.

That's because nothing ever assigns it a value. And that's because the only code which might assign it a value, the code in your init() method, is never called.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I didn't spot the spelling mistake, changed it now, it made no differene to the result though.
"Paul Clapham"
I thought i was assigning the string url the property of jbdc.url, and then in TestDB i am calling the getConnection() method which uses the url in it. Or am i missing something?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

tom davies wrote:I thought i was assigning the string url the property of jbdc.url


Well, no, you're not doing that. The init() method would be doing it, if it were ever called. But nothing ever calls the init() method, so none of the code in it ever runs.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Paul Clapham wrote:
tom davies wrote:I thought i was assigning the string url the property of jbdc.url


Well, no, you're not doing that. The init() method would be doing it, if it were ever called. But nothing ever calls the init() method, so none of the code in it ever runs.

Ahh, I see the problem now, i might re write the SimpleDataSource file as well, so i will write back if i have any further problems.
Thank you!
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I thought i had the answer.
I added this line


Into the TestDB file, so that it actually reads the file first. I still get the same error message though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting to a database