?
Solved

dynamic jtree database

Posted on 2003-03-13
6
Medium Priority
?
1,009 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 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