JFrame killed when connecting to database

I have DB2 installed on NT, and a data source SAMPLE connecting to it. I also made a JFrame that displays a split pane. On the right is a button, and left of which, a tree. I made connection to database, and try to read the result set and use the result to populate the tree nodes.


(1) the JFrame comes up for only a few seconds and automatically exits;

(2) If I comment out the section that reads the result set ( rs.getString(..) ), the JFrame stays, but if disappears after one click on it;

(3) If I comment out all the operation that attempts to connect to the database, the JFrame stays as normal. Of course there is no data, and this is not very useful.

My friend run this on his computer and everything seems fine. Could anyone tell me what could went wrong? Is it due to any wrong settings on my computer, e.g. ODBC?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Very odd...
Are you getting any exceptions thrown?
Any catch(Exception e) {} lines in there they may be hiding an exception?
Any conditional System.exit(#) lines that would make the interpretor exit?
Do you use a different JRE than your friend?
Whats the differences between your friends CPU and your's?
Can you post the relevent section of code?
angelanyAuthor Commented:
My friend's machine uses the same java
version as mine. His machine is 366. Mine is 450. I don't think this matters though.

java -version
java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)

To run the code,
java ServerCmdsGui

The complete output is inside the quote:
up to here

There is not exception printed.

The code segments are here.

// This is the snipplet relating to the tree

public class CmdTreePane extends JPanel {

      public CmdTreePane () {
            top = new DefaultMutableTreeNode ("Command Root");
            loadCmdTree (top);
            tree = new JTree (top);
            add ( new JScrollPane(tree) );

      private void loadCmdTree ( DefaultMutableTreeNode top ) {
            top.add(new DefaultMutableTreeNode("what"));
            Vector v = getData();

            int vSize = v.size();
            for (int i=0; i<vSize; i++) {
                  top.add ( new DefaultMutableTreeNode (v.elementAt(i).toString()) );

      public Vector getData() {
            Vector v = new Vector();
            String s;
            try {

                  Connection conn = DriverManager.getConnection("jdbc:odbc:sample", "dwang", "dwang");
                  Statement stat = conn.createStatement();
                  ResultSet rs = stat.executeQuery ("select * from department");
                  System.out.println("up to here");
                  while ( rs.next() ) {
                        s = rs.getString("DEPTNAME");
                        v.addElement (s);
                  return v;
            catch (Exception e) {
                  return v;

      final private JTree tree;
      private DefaultMutableTreeNode top;

// This is the main JFrame

public class ServerCmdsGui extends JFrame {

      public static void main(String args[])
            JFrame frame = new ServerCmdsGui("Server commands");

            frame.addWindowListener (new WindowAdapter() {
                  public void windowClosing (WindowEvent e) {

             frame.pack ();
            frame.setVisible (true);
// if i had this while(true) statement, the JFrame would last a little longer.
// o/w it just flashes and disappears.

      public ServerCmdsGui () {
            getContentPane().add(new CmdEditorPane());

      public ServerCmdsGui (String title) {

// command editor pane
public class CmdEditorPane extends JPanel {

      public CmdEditorPane () {
            Dimension minSize = new Dimension(100,40);
            Dimension preferredSize = new Dimension(250,80);

            CmdTreePane   cmdTreePane   = new CmdTreePane ();
            cmdTreePane.setMinimumSize (minSize);

            CmdDetailPane cmdDetailPane = new CmdDetailPane ();
            cmdDetailPane.setMinimumSize (minSize);

            JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                      cmdDetailPane );
            split.setPreferredSize (preferredSize);
//            split.setOneTouchExpandable (true);
//            split.setDividerLocation (40);

// command detail pane
public class CmdDetailPane extends javax.swing.JPanel {

      public CmdDetailPane () {
            add (new JButton("Command Details"));
angelanyAuthor Commented:
Adjusted points to 500
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

angelanyAuthor Commented:
Urgent! Please, anyone help!
Although Im probably missing something the code looks ok to me.
Except for the extra show() and the while(true) loop.  both of which were probably attempts to get the JFrame to come up.

I would probably put the SQL stuff in a separate thread and use invokeLater() to add the DefaultMutableTreeNode to the JTree, since so many errors can happen when dealing with JDBC.  That would also assure that your GUI is created and stays regardless of JDBC problems (ie its not holding up the creation of GUI components).

Having said that.  You said another person runs it just fine.  That leads me to believe that something is wrong with something other than the code. You are both using JDK1.2.2. The datasource seems to be created ok since your getting the appropriate Vector.

For 500 points you should be able to receive working code.  I cant do it right now (maybe someone else?).  You may want to try to reinstall the JDK.  Maybe something is corrupt??  

I would try putting a dummy DefaultMutableTreeNode in instead of making it from SQL. If that works then put the creation of the DefaultMutableTreeNode in a separate thread.  when that thread is done then add it to the JTree that has already been created.

Thats my thoughts on it, anyway.

Conick (as usual..long winded with little info)
I could not even compile it without errors. I got 7 of them, but it could be my mistake. You might tey using setVisible(true) instead of show(). Should be of no difference, but who knows?
.. You could also try adding this row to your windowlistener's end like this:

frame.addWindowListener (new WindowAdapter() {
                   public void windowClosing (WindowEvent e) {

//put this line here
Hi there,

Did you try printing the variables after the getData() in the loadCmdTree()? Check if this is coming out ok.

Its always a good idea, to create a JTree and a DefaultTreeModel. Then add nodes to the model and then set this model for the tree. The code is something like this...

tree = new JTree();
top = new DefaultMutableTreeNode("Top");
model = new DefaultTreeModel(top);

Also in you database section of your code, before the conn.close() close both the resultset and the statement in that order. Sometimes the connection.close creates some vagues problems. The closing of the resultset and the statement should fix this incase you have any.

Let me know if this works.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
angelanyAuthor Commented:
Thanks to you guys who offered help. I tried everything but it did not seem to work. After all, the same code, same version of VM, on different machines should not behave differently. I suspect this is a hardware problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.