Results 1 to 2 of 2
  1. #1
    Javaca is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Arrow JTree and SQLException [90007-153]

    Hello people!
    I am newbie and i need help. Please help to create a JTree from the database. I use a database H2 and JDK 1.6.0_25.
    If "ResultSet rs2" commented, all nodes have "pid = 0" work fine and visible in the JTree.
    If "ResultSet rs2" is not commented, then JTree added only one branch nodes, and all the other nodes do not work and are not visible. SQLException writing: The object is already closed [90007-153].
    So what am I doing wrong? Where did I go wrong? Please help me.

    ----------------------------------------------------------------------------------

    Java Code:
    CREATE TABLE `primer` (
      `id` int(10) unsigned NOT NULL auto_increment,
      `pid` int(10) NOT NULL default '0',
      `name` varchar(120) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ;
    
    INSERT INTO `primer` (`id`, `pid`, `name`) VALUES
    (1, 0, 'Books'),
    (2, 0, 'Magazines'),
    (3, 0, 'Audio'),
    (4, 0, 'Video'),
    (5, 1, 'Bible'),
    (6, 1, 'Tutorial Java'),
    (7, 2, 'Chip'),
    (8, 2, 'Hacker'),
    (9, 3, 'English lang'),
    (10, 3, 'German lang'),
    (11, 4, 'Terminator'),
    (12, 4, 'Terminator 2'),
    (13, 4, 'Terminator 3 ');
    ----------------------------------------------------------------------------------


    Java Code:
    import java.sql.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    
    
    class derevo
    {
    
        public static void main(String[] args) throws Exception
        {
            Class.forName("org.h2.Driver");
            Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
    		
            JFrame frame = new JFrame("Example JTree");
            Container cp = frame.getContentPane();
            frame.setSize(400,300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		DefaultMutableTreeNode defroot = null;
    		DefaultMutableTreeNode defchild = null;
    		DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root");
    		
    		try 
    		{
    		
              Statement stmt = conn.createStatement();
              ResultSet rs = stmt.executeQuery("SELECT * FROM primer WHERE pid=0");
    		  
                 while(rs.next())
                 {
    			 
    			    String root = rs.getString("name");
    				defroot = new DefaultMutableTreeNode(root);
    				node.add(defroot);
    
    				/*****/
    					ResultSet rs2 = stmt.executeQuery("SELECT * FROM primer WHERE pid = "+rs.getString("id"));
    					while(rs2.next())
    					{
    						
    						String child = rs2.getString("name");
    						defchild = new DefaultMutableTreeNode(child);
    						defroot.add(defchild);
    						
    						//JTree tree = new JTree(defroot);
    			
    					}
                    /*****/
    				JTree tree = new JTree(node);
    				cp.add(tree);
    
                 }
    
            stmt.close();
    		
    		} catch(SQLException ex) {
    			System.err.println("SQLException: " + ex.getMessage());
    		}
    		
            frame.setVisible(true);	 
    		conn.close();
    
        }
      
    }

  2. #2
    Javaca is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Problem solved. Need to create two Statement for two SQL Query.

    Working JTree example from database: (SQL table located above)

    Java Code:
    import java.sql.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    
    
    class derevo
    {
    
        public static void main(String[] args) throws Exception
        {
            Class.forName("org.h2.Driver");
            Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
    		
            JFrame frame = new JFrame("Example JTree");
            Container cp = frame.getContentPane();
            frame.setSize(400,300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
            DefaultMutableTreeNode defroot = null;
            DefaultMutableTreeNode defchild = null;
            DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root");
    		
            try 
            {
    		
              Statement stmt = conn.createStatement();
              ResultSet rs = stmt.executeQuery("SELECT * FROM primer WHERE pid=0");
              Statement stmt2 = conn.createStatement();
    		  
                 while(rs.next())
                 {
    			 
    		String root = rs.getString("name");
    		defroot = new DefaultMutableTreeNode(root);
    		node.add(defroot);
    
    		ResultSet rs2 = stmt2.executeQuery("SELECT * FROM primer WHERE pid = "+rs.getString("id"));
    		while(rs2.next())
    		{
    			String child = rs2.getString("name");
    			defchild = new DefaultMutableTreeNode(child);
    			defroot.add(defchild);
    		}
    		JTree tree = new JTree(node);
    		cp.add(tree);
    
                 }
            stmt2.close();
            stmt.close();
    		
           } catch(SQLException ex) {
    	System.err.println("SQLException: " + ex.getMessage());
           }
    		
            frame.setVisible(true);	 
            conn.close();
    
        }
      
    }

Similar Threads

  1. SQLException errors - need help!
    By DerekRaimann in forum JDBC
    Replies: 6
    Last Post: 01-27-2011, 10:00 AM
  2. Replies: 6
    Last Post: 09-01-2010, 11:29 AM
  3. connect to derby thrown SQLException
    By newbiejava in forum New To Java
    Replies: 5
    Last Post: 01-20-2010, 05:25 PM
  4. Unreported exception java.sql.SQLException
    By javamula in forum AWT / Swing
    Replies: 4
    Last Post: 09-29-2009, 02:32 PM
  5. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •