?
Solved

dynamic jtree database

Posted on 2003-03-13
6
Medium Priority
?
1,025 Views
Last Modified: 2009-12-16
i've made a dynamic database where you can change, add and delete nodes, but i want to save it in a database. And the next time i start the application it shows the nodes in the database. Anyone has an example for me? thanks..
0
Comment
Question by:2008
5 Comments
 
LVL 1

Expert Comment

by:DavidRissato
ID: 8127550
I don't know what is the object you are using to hold your nodes, but depending on the kind of object you are using, you can persist this by using ObjectOutputStream to write all the object down to a file.

See this example:
{
     FileOutputStream ostream = new FileOutputStream("t.tmp");
     ObjectOutputStream p = new ObjectOutputStream(ostream);

     p.writeObject("Today");  // it's a string
     p.writeObject(new Date());
        MyClass myclass = new MyClass(345);
        p.writeObject(myclass);

     p.flush();
     ostream.close();
}

And read it by this way:
{
     FileInputStream istream = new FileInputStream("t.tmp");
     ObjectInputStream p = new ObjectInputStream(istream);

     String today = (String)p.readObject();
     Date date = (Date)p.readObject();
        MyClass myclass = (MyClass)p.readObject();

     istream.close();
}


Even thought you don't know exactly the kind of object you are reading (suppose it can be of type MyClass or MyOtherClass), you can load it as Object and cast it down based on the result of a "instanceof" comparation.

{}'s
David Rissato Cruz
0
 

Author Comment

by:2008
ID: 8135588
i want to put it into a mysql database. don't you have an example with ResulSet and Statement?
0
 
LVL 1

Accepted Solution

by:
DavidRissato earned 500 total points
ID: 8135982
I didn't understand what do you need? Do you need to know a way to store information in a mySql database? What kind of information do you have?

Well, here is a connection example to store data in a database:

import java.sql.*;
import java.io.*;

(...)

{
    Connection conn = null;

    // Try to find mySql driver
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    }
    catch (Exception e) {
        System.err.println("Can't load MySql driver.");
        e.printStackTrace();
    }

    try {
        // Make database connection
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb");

        // Prepare a SQL command. The question mark represents a parameter
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO Customer (CustomerName, Phone) VALUES (?,?)");

        // Here we set the parameters to "ACME" and "555-555555"
        stmt.setString(1, "ACME");
        stmt.setString(2, "555-5555");

        // Execute the command on mySQL
        stmt.executeUpdate();

        // close statement and connection
        stmt.close();
        conn.close();
    }
    catch (SQLException e) {
        System.out.println("SQLException: " + e.getMessage());
        System.out.println("SQLState:     " + e.getSQLState());
        System.out.println("VendorError:  " + e.getErrorCode());
    }
    catch (Exception e) {
       e.printStackTrace();
    }
}


{}'s
David Rissato Cruz
0
 

Expert Comment

by:CleanupPing
ID: 9058717
2008:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9762620
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to DavidRissato

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question