• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to a database

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the properties file, you typed jbdc instead of jdbc - look carefully, the b and the d have been exchanged, for the keys.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic