static {
        new Category(1, "苏州市",
                new Category(2, "昆山",
                        new Category(3, "昆山1区",
                                new Category(4, "1组"),
                                new Category(5, "2组"),
                                new Category(6, "3组")),
                        new Category(7, "昆山2区",
                                new Category(8, "1组"),
                                new Category(9, "2组"),
                                new Category(10, "3组"),
                                new Category(11, "4组"))),
                new Category(12, "吴江",
                        new Category(13, "吴江1区"),
                        new Category(14, "吴江2区"),
                        new Category(15, "吴江3区"),
                        new Category(16, "吴江4区"),
                        new Category(17, "吴江5区")));
    } 这是构造方法:    
public Category(long id, String name, Category... children) {
    
        this.id = id;
        this.name = name;  
        this.children = new ArrayList<Category>();
        for (Category child : children) {
            this.children.add(child);
        }
        catMap.put(id, this);
      }
   我想把static中的内容换成从数据库中的取出来得地址,我该怎么做啊?求大神帮忙!
    

解决方案 »

  1.   


    //=====================================================================
    //
    //  File:    connectURL.java      
    //  Summary: This Microsoft SQL Server JDBC Driver sample application
    //      demonstrates how to connect to a SQL Server database by using
    //      a connection URL. It also demonstrates how to retrieve data 
    //      from a SQL Server database by using an SQL statement.
    //
    //---------------------------------------------------------------------
    //
    //  This file is part of the Microsoft SQL Server JDBC Driver Code Samples.
    //  Copyright (C) Microsoft Corporation.  All rights reserved.
    //
    //  This source code is intended only as a supplement to Microsoft
    //  Development Tools and/or on-line documentation.  See these other
    //  materials for detailed information regarding Microsoft code samples.
    //
    //  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
    //  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
    //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    //  PARTICULAR PURPOSE.
    //
    //===================================================================== import java.sql.*;public class connectURL { public static void main(String[] args) {

    // Create a variable for the connection string.
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
    "databaseName=AdventureWorks;integratedSecurity=true;"; // Declare the JDBC objects.
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

             try {
             // Establish the connection.
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                 con = DriverManager.getConnection(connectionUrl);
                
                 // Create and execute an SQL statement that returns some data.
                 String SQL = "SELECT  * FROM table";
                 stmt = con.createStatement();
                 rs = stmt.executeQuery(SQL);
                
                 // Iterate through the data in the result set and display it.
                 while (rs.next()) {
                 System.out.println(rs.getString(0));
                 }
             }
            
    // Handle any errors that may have occurred.
    catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
    }
    }
    }
      

  2.   

    很容易啊,简单的jdbc操作吧。把数据从数据库中查出来,然后把值放到实体的相应属性上不就行了。就像楼上做的那样。