• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

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?
  • 4
  • 2
  • 2
  • +1
1 Solution
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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.

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.

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now