Link to home
Start Free TrialLog in
Avatar of angelany
angelany

asked on

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.

However,

(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?
Avatar of conick
conick

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?
Avatar of angelany

ASKER

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
SPIFFY COMPUTER SERVICE DIV.
PLANNING
INFORMATION CENTER
DEVELOPMENT CENTER
MANUFACTURING SYSTEMS
ADMINISTRATION SYSTEMS
SUPPORT SERVICES
OPERATIONS
SOFTWARE SUPPORT
JJJJJJJJJJ
"

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);
            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            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()) );
            }
System.out.println("JJJJJJJJJJ");
      }

      public Vector getData() {
            Vector v = new Vector();
            String s;
            try {
                   v.addElement("AAAA");
               v.addElement("BBBB");

                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                  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");
                        System.out.println(s);
                        v.addElement (s);
                  }
                  conn.close();
                  return v;
            }
            catch (Exception e) {
                  System.out.println(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) {
                        System.exit(0);
                  }
            });

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

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


      public ServerCmdsGui (String title) {
            this();
            setTitle(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,
                                      cmdTreePane,
                                      cmdDetailPane );
            split.setPreferredSize (preferredSize);
//            split.setOneTouchExpandable (true);
//            split.setDividerLocation (40);
            add(split);
      }
}

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

      public CmdDetailPane () {
            add (new JButton("Command Details"));
      }
}
Adjusted points to 500
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) {
                   System.exit(0);
                   }
                   });

//put this line here
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
ASKER CERTIFIED SOLUTION
Avatar of rampi_2000
rampi_2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.