Java 从数据库填充DefaultMutableTreeNode

Java 从数据库填充DefaultMutableTreeNode,java,swing,jdbc,jtree,treenode,Java,Swing,Jdbc,Jtree,Treenode,我是Java新手,我想创建一个包含映射对象的DefaultMutableTreeNode类 我编写了一个TreeNodeMap类,该类继承自更通用的TreeNode类,而TreeNode类又继承自DefaultMutableTreeNode TreeNodeMap类包含采用相当长的参数列表的方法populate。我计划通过将其中一些参数转换为单个对象并重载该方法来提高方法的可读性,这样我就不必在第一次调用中传递一组null(是递归的) 注意:如果您不喜欢我对该方法的详细解释,请从这里直接跳到代码

我是Java新手,我想创建一个包含映射对象的DefaultMutableTreeNode类

我编写了一个TreeNodeMap类,该类继承自更通用的TreeNode类,而TreeNode类又继承自DefaultMutableTreeNode

TreeNodeMap类包含采用相当长的参数列表的方法populate。我计划通过将其中一些参数转换为单个对象并重载该方法来提高方法的可读性,这样我就不必在第一次调用中传递一组null(是递归的)

注意:如果您不喜欢我对该方法的详细解释,请从这里直接跳到代码

最初我创建一个空节点,它只包含子节点,不包含数据,这是根节点。其思想是使用一种方法,允许调用者传递带有“field\u id”、“field\u label”、“parent\u id”列的任何查询。在初始调用时,查询通过Where子句传递,其中“parent_id为null”,因此获得所有没有父节点的节点。所有这些节点都将添加到根节点。在迭代Resultset时,每个节点的populate方法称为现在传递一个“Where子句”,其中parent_id=[current node id],因此获得该节点的所有子节点。这将递归发生,直到所有节点都使用层次结构创建

代码 (请记住,我是JAVA新手,因此任何反馈都非常感谢)

我看到这种方法的问题是,数据库将被查询X次,对于大数据集,我认为这将导致一些问题。我还为每个查询打开了一个连接!因此,我考虑在方法创建后(在递归调用中)将连接传递给该方法,但是我不确定如何适当地关闭它。我可以编写一个条件来检查节点是否是根节点,然后关闭连接,但如果代码在这两者之间失败,会发生什么情况

最后,我的问题是: 1-最好的方法是什么? 2-我是否应该传递连接,以便在树的填充期间只有一个连接保持打开状态?如果是,那么如何正确关闭它。
3-我是否应该将结果集缓存到ArrayList中并使用它?

您的性能问题可能会得到保证。相反

  • 实现
    TreeModel
    ,如图所示。这样,只需要查询可见节点。有一个相关的例子

  • 传递
    javax.sql.DataSource
    ,而不是
    Connection

  • 实现TreeModel的类可以封装任何缓存的数据,但也提供了一些更新过时条目的方法。在显示缓存值后,请考虑使用<代码> SWIGWORKWorks<代码>来刷新模型,如图所示。您可能希望查看
    地图,而不是
    列表


  • 非常感谢你的建议@trashgood。这里有一些东西需要消化,我已经在考虑你的建议了。我没有意识到javax.sql.Datasource,它似乎和我在DBConnection类中所做的一样,没有必要重新发明轮子,所以我很可能会使用它。我会继续读下去,等我明白你的建议后再给你回复。再次感谢!
    public void populate( boolean isRoot, String parentFieldId, String parentFieldLabel, String childFieldId, String childFieldLabel, 
                int parentId, String tableName, String whereClause, String orderByClause, String additionalColumns, List<Map<String, String>> queryParams) throws SQLException, Exception{
            ResultSet rs = null;
            Connection con = null;
            PreparedStatement ps = null;
            try{
    
                DBConnection dbConnection = new DBConnection("localhost", 3306, "root", "password", "test", DBDrivers.DBTYPE_MYSQL);
                con = dbConnection.getConnection();
    
                String treeNodeSql = "Select " + parentFieldId + ", " + parentFieldLabel + 
                                                    ", " + childFieldId + ", " + childFieldLabel;
                if(additionalColumns != null && additionalColumns.trim().length() > 0)
                    treeNodeSql +=  " ," + additionalColumns;
    
                treeNodeSql += " From " + tableName + " WHERE 1=1";
    
                if(whereClause != null && whereClause.trim().length() > 0 ){
                    treeNodeSql += " AND " + whereClause;
                }
    
                if(isRoot){
                    treeNodeSql += " AND " + parentFieldId + " is null";
                }else{
                    if(parentFieldId == null || parentFieldId.trim().length() == 0)
                        throw new Exception(" The populate() method requires a parentId when isRoot is false.");
                    treeNodeSql += " AND " + parentFieldId + " = ?";
                }
                    //order clause append
                if(orderByClause != null && orderByClause.trim().length() > 0)
                    treeNodeSql += " " + orderByClause;
    
                    //prepare statement
                 ps = con.prepareStatement(treeNodeSql); 
                int ixParam = 0;
    
                    for(Map qParam : queryParams){
                        if(qParam.get("datatype") == "int"){
                            ps.setInt(++ixParam, Integer.parseInt((String) qParam.get("value")));
                        }else if(qParam.get("datatype") == "string"){
                            ps.setString(++ixParam, (String) qParam.get("value"));
                        }
                    }
    
                out.println(treeNodeSql);
                if(parentId > 0){
                    ps.setInt(queryParams.size()+1, parentId);
                }
                rs = ps.executeQuery();
    
                while(rs.next()){
                    HashMap<String, Object> childNodeData = new HashMap<String, Object>(4);
                    childNodeData.put("parentFieldId", parentFieldId);
                    childNodeData.put("parentFieldIdValue", Integer.toString(rs.getInt(parentFieldId)));
                    childNodeData.put("parentFieldLabel", parentFieldLabel);
                    childNodeData.put("parentFieldLabelValue", rs.getString(parentFieldLabel));
                    childNodeData.put("childFieldId", childFieldId);
                    childNodeData.put("childFieldIdValue", Integer.toString(rs.getInt(childFieldId)));
                    childNodeData.put("childFieldLabel", childFieldLabel);
                    childNodeData.put("childFieldLabelValue", rs.getString(childFieldLabel));
    
                    out.println("parentId: " + rs.getInt(parentFieldId)
                                    + ", parentLabel: " + rs.getString(parentFieldLabel)
                                    + ", childId: " + rs.getInt(childFieldId)
                                    + ", childLabel: " + rs.getString(childFieldLabel));
                    TreeNodeMap childNode = new TreeNodeMap(childNodeData);
                    this.add(childNode);
                    childNode.populate(false, parentFieldId, parentFieldLabel, childFieldId, childFieldLabel, rs.getInt(childFieldId), tableName, whereClause, orderByClause, additionalColumns, queryParams);
                }
            }catch(SQLException e){
                throw e;
    
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                out.println(e.getCause());
                out.println(e.getMessage());
                e.printStackTrace();
                throw e;
            }finally {
                try { rs.close(); } catch (Exception e) {  }
                try { ps.close(); } catch (Exception e) {  }
                try { con.close(); } catch (Exception e) {  }
            }
        }   
    
    treeNode.populate(true, "supervisor_id", "supervisor", "employee_id", "employee", 0, "vw_employee", null, null, null, queryParams);