java实现三级联动查询_Jquery实现三级联动源代码(Java)1. 数据库建表语句:
create table Province(
pid int(11) not null primary key,
pname varchar(100)
)
create table city(
cid int(11) not null primary key,
cname varchar(100),
pid int(11)
)
create table district(
did int(11) not null primary key,
dname varchar(100),
cid int(11)
)
2. 建⽴VO 实现类:
3. 建⽴数据库驱动连接:
//MySQL数据库连接驱动
public class SjLDriver {
private final String DRIVER="sql.jdbc.Driver";
private final String URL="jdbc:mysql://localhost:3306/test";
private final String USER="root";
private final String PWD="guoliang";
private Connection conn;
public SjLDriver(){
try {
Class.forName(DRIVER);
conn = Connection(URL, USER, PWD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.printStackTrace();
}
}
public Connection getConnection(){
;
}
}
4. 建⽴DAO 接⼝:
public interface SjLDAO {
public List getPro()throws SQLException; //省份public List getCity(int pid)throws SQLException; //城市public List getDistrictVO(int cid)throws SQLException; //地区}
5. 建⽴DAOImpl接⼝实现类:
public class SjLDAOImpl implements SjLDAO {
private Connection conn;
private PreparedStatement ppt;
private ResultSet res;
public List getCity(int pid) throws SQLException {
// TODO 城市
SjLDriver sb = new SjLDriver();
Connection();
List list = new ArrayList();
String sql="select cid ,cname,pid from city where pid=?";
ppt=conn.prepareStatement(sql);
ppt.setInt(1,pid);
jquery源码在线uteQuery();
()){
CityVO cb = new CityVO();
cb.Int("cid"));
cb.String("cname"));
cb.Int("pid"));
public List getDistrictVO(int cid) throws SQLException {
// TODO 区域
SjLDriver sb = new SjLDriver();
Connection();
List list = new ArrayList();
String sql ="select did,dname,cid from district where cid=?"; ppt=conn.prepareStatement(sql);
ppt.setInt(1,cid);
uteQuery();
()){
DistrictVO pp = new DistrictVO();
pp.Int("did"));
pp.String("dname"));
pp.Int("cid"));
list.add(pp);
}
return list;
}
public List getPro() throws SQLException {
// TODO 省份
SjLDriver sb = new SjLDriver();
Connection();
List list = new ArrayList();
String sql="select * from province";
ppt=conn.prepareStatement(sql);
uteQuery();
()){
ProvinceVO pp = new ProvinceVO();
pp.Int("pid"));
pp.String("pname"));
}
6. 创建⼯⼚
public class SjLDFactory {
public static SjLDAO getDisplay(){
return new SjLDAOImpl();
}
}
9. 创建servlet实现Http
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String action= Parameter("action");
if("pro".equals(action)){
SjLDAO hhp = new SjLDAOImpl();
try {
List list = Pro();
StringBuffer sb = new StringBuffer();
sb.append("");
for(int i = 0;i
sb.append(""+(i).getPname()+"");
}
sb.append("");
String s = sb.toString();
PrintWriter out = Writer();
response.setContentType("text/xml;UTF-8");
out.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if("changPro".equals(action)){
String pid =Parameter("pid");
int j = Integer.parseInt(pid);
SjLDAO hhp = new SjLDAOImpl();
try {
List list = City(j);
StringBuffer sb = new StringBuffer();
sb.append("");
for(int i=0;i
sb.append(""+(i).getCname()+"");
}
sb.append("");
String s = sb.toString();
PrintWriter out = Writer(); response.setContentType("text/xml;UTF-8"); response.setCharacterEncoding("UTF-8"); out.println(s);
out.flush();
out.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if("duanPro".equals(action)){
String cid = Parameter("cid");
int j= Integer.parseInt(cid);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论