Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java DB embedded

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java DB embedded

    Hi,

    I am trying to create a Java based quiz game on Netbeans IDE. What I've been trying to do is to have questions and answers in a database, draw information from there whenever it's required and print the questions/answers in labels in a jFrame. Now, I've tried it with the local MySQL Server and it's working fine. However, to distribute this application, I need to have an embedded DB.

    I would be highly grateful if someone could give me a step by step guide or point me to a detailed tutorial on how exactly I should do that (all the tutorials I've encountered have been for CRUD Desktop Applications...not useful for me). I would request you all not give me vague replies (like just use Java Derby!! duh!!). I'm kinda a newbie so specifics would be great like what files to import in the library and everything. I have The Netbeans 7.0 ALL BUNDLE and standard JDK. I would also be very very grateful if the instructions were very basic - like -

    1. Start with opening the services tab in Tools.

    I am new to JDBC and this forum so please forgive me if I haven't adhered to certain forum protocols.

    Thanks a lot,
    Please do reply,
    P

    Crossposted on http://www.coderanch.com/t/545853/JD...va-DB-embedded
    Last edited by potatostar; July 20th, 2011 at 06:33 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Java DB embedded

    If you're interested in using an embedded database which supports JDBC you could give H2 Database a try.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java DB embedded

    Hi Json,

    Thanks for replying, I'll definitely have a look at H2 database. However, right now I need to use Derby. Basically what I need is guide on how to use this tutorial as base to run an embedded java application with text fields, labels and code, not just use a wizard to create a java desktop application.

    I know that on this forum, spoon-feeding is a strictly discouraged but please do help me in this case.

    Thanks again,
    P

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Java DB embedded

    All you should need to do is add the derby.jar library to your project. From there, you should be to obtain a database connection using JDBC (see Trail: JDBC(TM) Database Access (The Java™ Tutorials) ). See the following for different ways to point to your local database location: Database connection examples
    Be sure to use the create=true flag if you need to initially create the database. From there, you can use the connection to create your table structure and insert the data.

  5. The Following User Says Thank You to copeg For This Useful Post:

    potatostar (July 20th, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java DB embedded

    Hi copeg,

    Thanks for the tips. Funnily enough I had arrived at the same conclusion and so that's what I had tried earlier. I added derby.jar in the library for my app and then created a table in the APP schema of a database called db1 from the services tab of Netbeans IDE. Then I tried to print it's contents and it gave me the error

    java.sql.SQLSyntaxErrorException: Table/View 'APP.TB1' does not exist.

    Here is the code of the function

    void see()
        {
            try{ 
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    }catch(ClassNotFoundException e){
                System.out.println(e);
            }
     
             try{
           Connection con = DriverManager.getConnection("jdbc:derby:db1","test","test");
            Statement stmt = con.createStatement();
     
            ResultSet rs = stmt.executeQuery("SELECT * FROM APP.TB1");
     
            while (rs.next()) {
            String s = rs.getString("Name");
            float n = rs.getFloat("Id");
            System.out.println(n + "   " + s);
        }
            }catch(SQLException e){
                System.err.println(e);
            }      
     
     
            con.close();
     
        }

    Please let me know what I'm doing wrong.

    Also I tried to create a table using the following code which worked fine and it said table created but I don't know under which schema the table actually is. I also tried printing the data in the table Employee11(the one I created in the following code) and the output box remained unchanged.

    void see2()
        {
            try{ 
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                }
            catch(ClassNotFoundException e){
                System.out.println(e);
            }
     
            try{
                Connection con = DriverManager.getConnection("jdbc:derby:db1","test","test");
      Statement st = con.createStatement();
      String table = 
      "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))";
      st.executeUpdate(table);
      System.out.println("Table created successfully!");
     
      }
      catch(SQLException s){
      System.out.println("Table already exists!");
      }
     
     
            con.close();
     
        }

    Please do reply
    P
    Last edited by potatostar; July 20th, 2011 at 01:40 AM.

  7. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java DB embedded

    Quote Originally Posted by potatostar View Post
    Also I tried to create a table using the following code which worked fine and it said table created but I don't know under which schema the table actually is. I also tried printing the data in the table Employee11(the one I created in the following code) and the output box remained unchanged.
    Alright sorry bout that, what an idiot, I never entered any any data in the employee11 table so no output. And I don't need to know the schema or anything!

    So anyways I can do whatever I wanted to with the above code, I really don't need to go to the services tab at all and create a table there!! YAY!

    Thanks and problem resolved!! (esp to copeg for leading me back to the right track)

    Oh and sorry for crossposting. I had posted there before here and didn't get a reply!

    Thanks again
    P

Similar Threads

  1. Managing an embedded database
    By jstn455 in forum JDBC & Databases
    Replies: 1
    Last Post: April 24th, 2011, 10:06 AM
  2. Help with JavaDB embedded
    By jstn455 in forum JDBC & Databases
    Replies: 2
    Last Post: March 17th, 2011, 03:27 PM

Tags for this Thread