项目列表:
CascadeServlet:
package com.jd.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.jd.imp.IAreaService;
import com.jd.service.AreaService;
public class CascadeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IAreaService areaService = new AreaService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain; charset=UTF-8");
String json = new Gson().toJson(areaService.select(""));
System.out.println(json);
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
}
}
IAreaService:
package com.jd.imp;
import java.util.List;
import com.vo.Area;
public interface IAreaService {
List<Area> select(String parentCode);
}
AreaService:
package com.jd.service;
import java.util.List;
import com.jd.dao.AreaDao;
import com.jd.imp.IAreaDao;
import com.jd.imp.IAreaService;
import com.vo.Area;
public class AreaService implements IAreaService{
private IAreaDao areaDao = new AreaDao();
public List<Area> select(String parentCode) {
if(parentCode==null) {
parentCode="";
}
return areaDao.select(parentCode);
}
}
IAreaDao:
package com.jd.imp;
import java.util.List;
import com.vo.Area;
public interface IAreaDao {
List<Area> select(String parentCode);
}
AreaDao:
package com.jd.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jd.imp.IAreaDao;
import com.tool.db.DBLink;
import com.tool.db.IRowMapper;
import com.vo.Area;
public class AreaDao implements IAreaDao{
public List<Area> select(String parentCode) {
String sql = "select * from area where parent_code=''";
final List<Area> LIST = new ArrayList<Area>();
new DBLink().select(sql,new IRowMapper() {
public void rowMapper(ResultSet rs) {
try {
while(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String code = rs.getString("code");
String parentCode = rs.getString("parent_code");
LIST.add(new Area(id,name,code,parentCode));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
return LIST;
}
}
Area:
package com.vo;
public class Area {
private String id;
private String name;
private String code;
private String parentCode;
public Area(String id, String name, String code, String parentCode) {
this.id = id;
this.name = name;
this.code = code;
this.parentCode = parentCode;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
@Override
public String toString() {
return "Area [id=" + id + ", name=" + name + ", code=" + code + ", parentCode=" + parentCode + "]";
}
}
DBLink:
package com.tool.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 org.apache.log4j.Logger;
import com.tool.PropertiesTool;
public class DBLink {
private Logger logger = Logger.getLogger(DBLink.class);
private Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
String userName = PropertiesTool.getValue("db.username");
String password = PropertiesTool.getValue("db.password");
String url = PropertiesTool.getValue("db.url");
return DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}
return null;
}
public boolean exist(String sql) {
Connection connection = null;
Statement statement =null;
ResultSet resultSet=null;
try {
connection = getConnection();
statement = connection.createStatement();
resultSet= statement.executeQuery(sql);
return resultSet.next();
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(resultSet,statement,connection);
}
return false;
}
public boolean exist(String sql, Object ...params) {
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet=null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1, params[i]);
}
resultSet= preparedStatement.executeQuery();
return resultSet.next();
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(resultSet,preparedStatement,connection);
}
return false;
}
public void select(String sql,IRowMapper rowMapper) {
Connection connection = null;
Statement statement =null;
ResultSet resultSet=null;
try {
connection = getConnection();
statement = connection.createStatement();
resultSet= statement.executeQuery(sql);
rowMapper.rowMapper(resultSet);
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(resultSet,statement,connection);
}
}
public void select(String sql,IRowMapper rowMapper,Object ...params) {
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet=null;
try {
connection = getConnection();
preparedStatement= connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1, params[i]);
}
resultSet= preparedStatement.executeQuery();
rowMapper.rowMapper(resultSet);
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(resultSet,preparedStatement,connection);
}
}
public boolean update(String sql) {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
statement = connection.createStatement();
int result = statement.executeUpdate(sql);
return result>0;
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(statement,connection);
}
return false;
}
public boolean update(String sql,Object ...params) {
Connection connection= null;
PreparedStatement preparedStatement= null;
try {
connection= getConnection();
preparedStatement= connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1, params[i]);
}
return preparedStatement.executeUpdate()>0;
} catch (Exception e) {
logger.debug(e.getMessage(), e);
if(connection!=null) {
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}finally {
close(preparedStatement,connection);
}
return false;
}
public boolean batch(String...sqls) {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
connection.setAutoCommit(false);
statement = connection.createStatement();
for (String sql : sqls) {
statement.addBatch(sql);
}
statement.executeBatch();
connection.commit();
return true;
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(statement,connection);
}
return false;
}
private void close(Statement statement,Connection connection) {
try {
if(statement!=null) {
statement.close();
}
} catch (SQLException e) {
logger.debug(e.getMessage(), e);
}
try {
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
logger.debug(e.getMessage(), e);
}
}
private void close(ResultSet resultSet,Statement statement,Connection connection) {
try {
if(resultSet!=null) {
resultSet.close();
}
} catch (SQLException e) {
logger.debug(e.getMessage(), e);
}
close(statement,connection);
}
}
IRowMapper:
package com.tool.db;
import java.sql.ResultSet;
public interface IRowMapper {
void rowMapper(ResultSet rs);
}
PropertiesTool:
package com.tool;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesTool {
private static Properties properties = new Properties();
static {
InputStream inputStream = PropertiesTool.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
return properties.getProperty(key);
}
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
<script src="./js/jquery-1.8.3.min.js"></script>
<script src="./js/json2.js"></script>
<script type="text/javascript">
$(function(){
var obj={
url:"./CascadeServlet",
datatype:"text",
success:function(data){
data=JSON.parse(data);
var options="<option value=''>---请选择---</option>";
for(var i=0;i<data.length;i++){
var obj=data[i];
options=options+"<option value='"+obj.code+"'>"+obj.name+"</option>";
}
$("#province").append(options);
}
}
$.ajax(obj);
});
function changecity(){
console.log(111);
var parentCode=$("#province").val();
if (parentCode=="") {
$("#city").empty();
$("#city").html("<option value=''>---请选择---</option>");
$("#county").empty();
$("#county").html("<option value=''>---请选择---</option>");
return;
}
var obj={
url:"./CascadeServlet?parentCode="+parentCode,
datatype:"text",
success:function(data){
data=JSON.parse(data);
var options="<option value=''>---请选择---</option>";
for(var i=0;i<data.length;i++){
var obj=data[i];
options=options+"<option value='"+obj.code+"'>"+obj.name+"</option>";
}
$("#city").empty();
$("#county").empty();
$("#county").html("<option value=''>---请选择---</option>");
console.log(options);
$("#city").append(options);
}
}
$.ajax(obj);
}
function changecounty(){
var parentCode=$("#city").val();
if (parentCode=="") {
$("#county").empty();
$("#county").html("<option value=''>---请选择---</option>");
return;
}
var obj={
url:"./CascadeServlet?parentCode="+parentCode,
datatype:"text",
success:function(data){
data=JSON.parse(data);
var options="<option value=''>---请选择---</option>";
for(var i=0;i<data.length;i++){
var obj=data[i];
options=options+"<option value='"+obj.code+"'>"+obj.name+"</option>";
}
$("#county").empty();
$("#county").append(options);
}
}
$.ajax(obj);
}
</script>
</head>
<body>
<select id="province" onchange="changecity()"></select>
<select id="city" onchange="changecounty()">
<option value=''>---请选择---</option>
</select>
<select id="county">
<option value=''>---请选择---</option>
</select>
</body>
</html>