![](https://i-blog.csdnimg.cn/blog_migrate/f9a9bff84289f3de011d5b8f3cb351f2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/df39b5b75c870ed94c3b2fba51acbf1c.png)
dao层代码(城市dao层代码)
package cn.hp.dao;
import cn.hp.model.City;
import java.util.List;
public interface CityInfoDao {
public List<City> findAllCity(int pid);
}
![](https://i-blog.csdnimg.cn/blog_migrate/d375db3359d116c8595cf8c7e7114a0e.png)
package cn.hp.dao;
import cn.hp.model.City;
import cn.hp.model.Provice;
import cn.hp.util.ConnDemo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CityInfoDaoImpl implements CityInfoDao {
@Override
public List<City> findAllCity(int pid) {
Connection conn= ConnDemo.getConn();
List<City>list=new ArrayList<City>();
String sql="select * from city where pid=?";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,pid);
ResultSet rs=ps.executeQuery();
while (rs.next()){
City c=new City();
c.setCid(rs.getInt(1));
c.setCname(rs.getString(2));
c.setPid(rs.getInt(3));
list.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/57989982b03eb5e43525da1fc3320666.png)
dao层代码(省代码)
package cn.hp.dao;
import cn.hp.model.Provice;
import java.util.List;
public interface ProviceInfoDao {
public List<Provice> findAll();
}
![](https://i-blog.csdnimg.cn/blog_migrate/3b1a9ed78f0366ccb6d18308e57e31f8.png)
package cn.hp.dao;
import cn.hp.model.Provice;
import cn.hp.util.ConnDemo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProviceInfoDaoImpl implements ProviceInfoDao {
@Override
public List<Provice> findAll() {
Connection conn= ConnDemo.getConn();
List<Provice>list=new ArrayList<Provice>();
String sql="select * from provice";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
Provice p=new Provice();
p.setPid(rs.getInt(1));
p.setPname(rs.getString(2));
list.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list