• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using a database in java application

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've only recently started learning java and I'm in the second course now. Subject of the moment is connecting to a database and using the data from it in an application. Here's the thing. When my application tries to initiate the Connection object it fails, and I can't really figure out why. It catches the SQLEcxeption and then it moves on but obviously after that I get a NullPointerException when it tries to do something with the Connection object which is still null. I installed FireBird and its accompanying JayBird driver, and added the JayBird-full jar package to the compile libraries in netbeans. I'm guessing that the problem lies with the URL of the database, because it's a database given by the university for me to incorporate, so the username and password were already set in the DBConst class where all the constants are stored as attributes.

I guess it's hard to formulate a question if you don't know where the problem lies, but I'll try to narrow it down.

How can I correctly define the URL of a database on my own computer?

How do I know if firebird and the jdbc is working correctly?

What else might be the problem?

Just to clear this up: I'm not cheating on a course assignment, it's just an exercise where the explanation on how to use databases is a bit limited. Might also be worth to note that I'm using Linux Mint Nadia.

OK, if someone could help me out with this in their spare time I'd sure be awfully grateful. I'm happy there's a forum where I can ask questions that might seem silly on other forums.

Oh yeah, I'm not absolutely clear on what might be relevant information here so please ask me anything, and I'll provide you the info.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oscar Bartman wrote:It catches the SQLEcxeption and then it moves on



Don't do that. Catching an exception doesn't fix what went wrong. It just transfers control to the code that you put in place to handle it. If you're going to catch an exception, you have to actually fix the problem. Otherwise just don't bother catching it.

At the very least, if you're going to catch it, call the Exception's printStackTrace() method in the catch block so you can see what went wrong and where.

How can I correctly define the URL of a database on my own computer?



That depends on which DB you've installed and how you configured it. Before you connect to it with Java, you should try connecting to it with whatever client tool came with the download.

How do I know if firebird and the jdbc is working correctly?



If you can connect, they're working correctly. If you cannot connect, one or the other may not be working correctly, but more likely you have a bug in your code, or an invalid URL, or you didn't configure the DB correctly.

What else might be the problem?



Kind of impossible to say without more details.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oscar

It would be helpful if you could show us some code and the exception message you are getting.
 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the speedy reply guys. That's very nice

Jeff: What is meant by a client tool? The DB I've installed is FireBird combined with JayBird. Does FireBird automatically come with a client or do I need to install one seperately? Thanks for the advice on handling exceptions.

I'll post the code that I'm assuming is relevant with the exception message. Some of it is in Dutch, I hope you can work your way around that.

This is the method that catches an exception:



gebruikersnaam = username, wachtwoord = password. "con" is already declared as an attribute in the class with null as its value. This private method is only meant to establish the connection.

URL is defined in DBConst:



This class is part of the same package, I didn't include the package declaration here because I thought it redundant.

Anyway, some translation:

padnaam: pathname (or just path I guess)
Drivernaam: Driver name (Dutch is easy)
URL, speaks for itself
Gebruikersnaam and wachtwoord, username and password.

That little comment above the declarations reads: "fill in the correct path to the folder which contains the projects."

The test where it all goes wrong is a static void main method:



I've translated commentary here to english.

The test class and the DBConst class came with the course material, so besides defining the Path in DBConst I haven't changed it much.

I really have a strong feeling that installed the DB or the jdbc wrongly.

So there. Again, thanks for this.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oscar

Can you also post the full exception message?
 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah yes, forgot about that:

 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first line is my own defined message in the try clause you see in the first excerpt in my former message.

The NullPointerException after that I'm assuming means that the Connection object simply didn't get initialized
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oscar

Change this code:

To this:
 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James, here is the result from printing the stack using the change you suggested:

 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do I define username and password?
 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I found the password (that apparently I set myself) in the SYSDBA file in the firebird directory. I changed it to the currently set password and ran the test again. Stack now looks like this:

 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like there is a problem with this file:

I/O error during "open" operation for file "/home/oscar/Java/ProjectenOPiJ2/Le10CDMetDB"

Does this file exist?

Have you created your database correctly?

As Jeff suggested earlier, can you successfully connect to it using a client tool instead of in Java code?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a thought. If you are using Linux, there is a Firebird client called FlameRobin which will handle all your connection, database creation, table creation and query building duties. Why not try your sql queries there first?
 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I did it. I installed FlameRobin and found out that it could make a connection without problem. So I started fiddling with the values in my code and,... here is the silly part. Apparently you do have to define the path of the DB file itself, not the directory it's in. From the instructions I was given by the uni I understood that the latter was the case.

Thanks you guys, for all the help. If I would have had to wait for the teachers to come back behind their computer it would have taken the entire weekend.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah but think of it a different way...will you ever get the path wrong again when using a FireBird database? No, so it has been of benefit to you.
 
Oscar Bartman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed you're right. The best learning process is one that involves communication.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic