package com.wangwang.dao;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.wangwang.db.DBmanager;
import com.wangwang.vo.City;public class CityDAO {

public City findById(int id) {
String sql = "select cityId, cityName, parentId, context from city where cityId = " + id;
DBmanager dbManager = new DBmanager();
ResultSet rs = dbManager.query(sql);
City city = null;
try {
while (rs.next()) {
city = new City(rs.getInt(1), rs.getString(2), rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dbManager.close();
}
return city;
}
/**
 * 根据父ID得到所有一级子类,如果传递为0,得到所有根节点
 * @param cityId
 * @return
 */
public List<City> findByParentId(int parentId) {

String sql = "select cityId, cityName, parentId, context from city where parentId = " + parentId;
DBmanager dbManager = new DBmanager();
ResultSet rs = dbManager.query(sql);
List<City> citys = new ArrayList<City>();
try {
while (rs.next()) {
citys.add(new City(rs.getInt(1), rs.getString(2), rs.getString(4)));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dbManager.close();
}

return citys;
}

}

解决方案 »

  1.   

    package com.wangwang.db;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;public class DBmanager {
     private  Connection connection=null; 
     private Statement statement=null;
     private ResultSet resuletSet=null;
     PreparedStatement preparedStatement=null;
             public ResultSet query(String sql){
       


     try {
    Class.forName(Config.Driver);

    connection= DriverManager.getConnection(Config.url,Config.uesr,Config.pwd);
     statement=connection.createStatement();
    System.out.println("得到连接成功!");
     resuletSet  =statement.executeQuery(sql);

     

     } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

       return resuletSet;
       
       } 
       

       
       public void close(){
    try {
    if  (resuletSet!=null)

    resuletSet.close();
         }
    if  (connection!=null)

    connection.close();
         }
    if  (preparedStatement!=null)

    preparedStatement.close();
         }

     if (statement!=null){
    statement.close();
    }
     
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       }
    }
      

  2.   

    package com.wangwang.vo;import java.util.List;public class City {
    private int cityId;
    private int parentId;
    private String cityName;
    private String context;
    /*private List<City> qus;

    public List<City> getQus() {
    return qus;
    }
    public void setQus(List<City> qus) {
    this.qus = qus;
    }*/
    @Override
    public String toString() {

    return "cityId = " + cityId + " cityName = " + cityName;
    }
    public City() {
    super();
    }
    public City(int cityId, String cityName) {
    super();
    this.cityId = cityId;
    this.cityName = cityName;

    }
    public City(int cityId, String cityName, String context) {
    super();
    this.cityId = cityId;
    this.cityName = cityName;
    this.context = context;
    }
    public City(int cityId, int parentId, String cityName, String context) {
    super();
    this.cityId = cityId;
    this.parentId = parentId;
    this.cityName = cityName;
    this.context = context;
    }
    public int getCityId() {
    return cityId;
    }
    public void setCityId(int cityId) {
    this.cityId = cityId;
    }
    public int getParentId() {
    return parentId;
    }
    public void setParentId(int parentId) {
    this.parentId = parentId;
    }
    public String getCityName() {
    return cityName;
    }
    public void setCityName(String cityName) {
    this.cityName = cityName;
    }
    public String getContext() {
    return context;
    }
    public void setContext(String context) {
    this.context = context;


    }
      

  3.   

    dwr.xml的配置
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
     
    <dwr>
        <allow>
             <create creator="new" javascript="CityDAO">
                 <param name="class" 
                value="com.wangwang.dao.CityDAO" />
             </create>
          <convert match="com.wangwang.vo.City" converter="bean">
            </convert>
         </allow>
    </dwr>