| 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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
|
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 7 API documentation
Scala Notes - My blog about Scala
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
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: 16482
|
|
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.
|
 |
 |
|
|
subject: Connecting to a database
|
|
|