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 4 of 4

Thread: write object to sql db

  1. #1
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Question write object to sql db

    i was wondering if there was some way to write an entire object to an sql db .
    not like save all of the info , but ,idk , maybe a serialized version of the object with all of its variables
    Programming: the art that fights back


  2. #2
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: write object to sql db

    Yes there is. For my Project we used DatabaseDerby that comes with the NetBeans IDE. First you must connect to the database manually before running the program, then its really easy, its just some repetitive code followed by the statement of what you want to do, like insert, delete etc. Here is a part of my project that did some database executions.

     
    if(addMoneyToAcc == true)               //if user pressed "Add Money to Account" button
                {
                    // This line of code creates an instance of the driver for the database
                    Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                    // Connects to the database
                    myConnection =
                    DriverManager.getConnection("jdbc:derby://localhost:1527/BillsDatabase",
                            "root","root");
                    stmt = myConnection.createStatement();
     
                    testQuery="select account_balance from transmitters where transmitter_id=" +
                            transmitterIDNum;
                    //above statement creates an SQL string to be executed
     
                    results=stmt.executeQuery(testQuery);   //executes the query
     
                    stmt.close();
                    results.close();
                    myConnection.close();  // it is important to do all the closing, otherwise u will get an error
    }


    So basically you repeat all the code except the part that is the query, that is what actually alters the database, everything else can be copied and pasted. You have to know how to work with database statements.

  3. #3
    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: write object to sql db

    You should be able to do this by just serializing the object. But if you want a proper object database I'd recommend you use something like db4o :: Java & .NET Object Database ? Open Source Object Database, Open Source Persistence, Oodb

    // Json

  4. The Following User Says Thank You to Json For This Useful Post:

    Bryan (May 18th, 2010)

  5. #4
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: write object to sql db

    Quote Originally Posted by Json View Post
    You should be able to do this by just serializing the object. But if you want a proper object database I'd recommend you use something like db4o :: Java & .NET Object Database ? Open Source Object Database, Open Source Persistence, Oodb

    // Json
    I checked db4o out and have read the documentation(well up to chapter 5 or so) and I was amazed! This is truly a beautiful object db. And the "Interface"(db.store() and alike) is also very logical what I really like

    A database per file wich can go up to 254GB a file and its fast! Its perfect this way its easier for me to back it up. And i'll be using multiple files ofcourse. Users, shop and those stuff will get their own file

    I will have records though, this will be pure data(and alot), not objects so I think I'll still be using PostgreSQL wich I regret. This data will be point calculating(adding or reducing points). But maybe you guys have a better solution?
    Last edited by Bryan; May 18th, 2010 at 10:08 AM.

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 04:33 PM
  2. How to Write and Read Binary Data
    By neo_2010 in forum File Input/Output Tutorials
    Replies: 3
    Last Post: January 4th, 2010, 02:38 PM
  3. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 08:36 AM
  4. How to write above a JPanel
    By bruno88 in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2009, 06:16 PM
  5. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM