This discussion is archived
7 Replies Latest reply: Aug 16, 2012 2:32 AM by gimbal2 RSS

new using databases in Java

955780 Newbie
Currently Being Moderated
First of all hello, this is my first post in the Java oracle forum,


I know basic knowledge of java(school) and some mysql database(self thought) too.

I wanted to make a java app where i could use a database inside the same jar, but i am not pretty sure how to do this, i saw in a webpage where it said that if I used a database from a jar file the database wouldnt be writable just readable, is this true?


can someone point me out in how to use a database for a executable jar?

THX!

PS: using netbeans
  • 1. Re: new using databases in Java
    DrClap Expert
    Currently Being Moderated
    952777 wrote:
    if I used a database from a jar file the database wouldnt be writable just readable, is this true?
    There are so many possible ambiguities in this question, it's ridiculous. What exactly does it mean to "use a database from a jar file"? Does it mean the database was supposed to be in the jar, and you want to use it? Or does it mean that your code was in the jar, and you imagine it being in the jar and using the database, which isn't in the jar? Or what?
    can someone point me out in how to use a database for a executable jar?
    Presumably using a database "for" a jar file is not the same as using it "from" a jar file. Or is it?

    Perhaps you could produce a new version of your question without ambiguous prepositions. Or perhaps it would be better if you didn't try to learn about jar files and databases at the same time.
  • 2. Re: new using databases in Java
    955780 Newbie
    Currently Being Moderated
    Im sorry if i didn't explain myself, basically i want to know if i can have an executable jar, a single file, which will open my application that will connect to a database, this database is meant to be locally but saved inside the same jar, so if i wanted to send that jar file to a peer he would see what the database is populated with.

    thanks and sorry
  • 3. Re: new using databases in Java
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!
    >
    Im sorry if i didn't explain myself, basically i want to know if i can have an executable jar, a single file, which will open my application that will connect to a database, this database is meant to be locally but saved inside the same jar, so if i wanted to send that jar file to a peer he would see what the database is populated with.
    >
    No - you cannot directly update an existing jar file with anything using Java standard functionality. I don't know why you would want to put the database data inside your executable jar file anyway; you can easily distribute the data separately in another jar or zip file.

    You have at least two choices:

    1. use a file-based database like JavaDB - update the DB with data and then send the DB files to your peer. You could run a small batch script that would zip/jar the DB files.

    2. use JDBC and create a client-side cache result set. This is a result set that can be streamed to a file and you could then send the file to your peer and they could use Java code to read the streamed data into their own cache result set and manipulate it.
  • 4. Re: new using databases in Java
    955780 Newbie
    Currently Being Moderated
    Thankyou very much to you two
  • 5. Re: new using databases in Java
    gimbal2 Guru
    Currently Being Moderated
    It would probably be best if your application can create the database from scratch though - that's how I would do it anyway. Assuming you're talking about an embedded database of course; I would recommend H2 if you are as it is fast, robust and feature complete.

    http://www.h2database.com/html/main.html

    Then you only need to distribute the application jars and when the application runs and detects there is no database yet, it will generate it.

    The way I've dealt with that at one point in time is to give the user the option to choose an "installation directory". This gives the user the possibility to choose a directory that is safe and can actually be written to. In that directory I would then let the database be created - or recreated if someone would remove it by hand. There is the question of where you keep track of what directory the user picked; I'm lazy and I create a configuration file in the user's home directory (user.home Java system property) but you could also use the Preferences API for it.

    http://docs.oracle.com/javase/7/docs/api/java/util/prefs/Preferences.html


    Lots of information there, it will require some research if you're interested in doing something like this :)
  • 6. Re: new using databases in Java
    955780 Newbie
    Currently Being Moderated
    gimbal2 wrote:
    It would probably be best if your application can create the database from scratch though - that's how I would do it anyway. Assuming you're talking about an embedded database of course; I would recommend H2 if you are as it is fast, robust and feature complete.

    http://www.h2database.com/html/main.html

    Then you only need to distribute the application jars and when the application runs and detects there is no database yet, it will generate it.
    How does this work? i download the h2 database and then make my program with it, and if another person wants to use my program in his computer will he have to download it too?
  • 7. Re: new using databases in Java
    gimbal2 Guru
    Currently Being Moderated
    pllaguno wrote:
    gimbal2 wrote:
    It would probably be best if your application can create the database from scratch though - that's how I would do it anyway. Assuming you're talking about an embedded database of course; I would recommend H2 if you are as it is fast, robust and feature complete.

    http://www.h2database.com/html/main.html

    Then you only need to distribute the application jars and when the application runs and detects there is no database yet, it will generate it.
    How does this work? i download the h2 database and then make my program with it, and if another person wants to use my program in his computer will he have to download it too?
    It is simply a jar which you deliver with your application. Like any other library. If you don't understand that then you need to spend some time studying deployment of Java applications.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points