省市区 地址联动 数据怎么创建

省市区 地址联动 数据怎么创建,第1张

1、在做这个小项目前的准备工作:
11、分析:
由于省、市、县城(区)这些数据是存储到了MySQL数据库中的,我们要通过后台servlet获取数据库中的数据,然后再通过转发或者重定向的方式将数据呈现到前台页面中。
12、需要导入的jar包有:
mysql驱动包:mysql-connector-Java-517-binjar
c3p0数据库连接池:c3p0-0921jar、mysql-connector-java-517-binjar(c3p0依赖包)
前台c标签(需要通过遍历的方式呈现——c:forEach):jstl-102jar、standard-101jar(jstl依赖包)
将集合或者数组转换成json数据格式(Jackson包):jackson-annotations-221jar、jackson-core-221jar、jackson-databind-221jar
前台页面需要用到jQuery,故还需要导入jquery-172js库文件
13、该小项目用到的技术:
jdbc的知识、servlet的知识、jquery的知识、Ajax的知识(局部刷新)、标签的知识、EL表达式的知识、JSP的知识
2、开发过程:
21、准备数据源
创建一个数据库,命名为thereaction并创建三个表——province(省)、city(市)、county(县/区)
22后台开发
创建三个Javabean分别是Province、City、County。(由于太过简单,这里就不粘代码了)
创建Java类和c3p0连接池实现与数据库的连接:DAOjava(实现获取数据库数据的功能)、jdbctoolsjava(实现获取数据库连接、释放连接的功能)、c3p0-configxml
jdbctoolsjava代码如下:
[java] view plain copy
package comxiaojiedao;

import javaioIOException;
import javasqlConnection;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;

import javaxsqlDataSource;

import commchangev2c3p0ComboPooledDataSource;

public class Jdbctools {
private static DataSource ds=null;
//数据库连接池应只被初始化一次
static{
ds=new ComboPooledDataSource("helloc3p0");
}
//获取数据库连接
public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException{
return dsgetConnection();
}
public static void shifanglianjie(Connection ct, PreparedStatement ps,ResultSet rs) {
if(rs!=null){
try {
rsclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
if(ps!=null){
try {
psclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
if(ct!=null){
try {
ctclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}

}
DAOjava的代码如下
[java] view plain copy
package comxiaojiedao;

import javaioIOException;
import javasqlConnection;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javautilArrayList;
import javautilList;

import comxiaojiebeansCity;
import comxiaojiebeansCounty;
import comxiaojiebeansProvince;

public class DAO {
public List<Province> getprovince(String sql ,Objectargs) throws ClassNotFoundException, SQLException, IOException{
List<Province> provinces=new ArrayList<Province>();
//准备去查数据库
Connection ct=null;
ct=JdbctoolsgetConnection();
Systemoutprintln("获取到数据库的连接了");
PreparedStatement ps=null;
ResultSet rs=null;
ps=ctprepareStatement(sql);
for(int i=0;i<argslength;i++){
pssetObject(i+1, args[i]);
}
rs=psexecuteQuery();
while(rsnext()){
provincesadd(new Province(rsgetInt("province_id"),rsgetString("province_name")));
}
Jdbctoolsshifanglianjie(ct, ps, rs);
return provinces;

}
public List<City> getcity(String sql ,Objectargs) throws SQLException, ClassNotFoundException, IOException{
List<City> cities=new ArrayList<City>();
//准备去查数据库
Jdbctools jt=new Jdbctools();
Connection ct=null;
ct=jtgetConnection();
PreparedStatement ps=null;
ResultSet rs=null;
ps=ctprepareStatement(sql);
for(int i=0;i<argslength;i++){
pssetObject(i+1, args[i]);
}
rs=psexecuteQuery();
while(rsnext()){
citiesadd(new City(rsgetInt("city_id"),rsgetString("city_name")));
}
jtshifanglianjie(ct, ps, rs);
return cities;

}
public List<County> getcounty(String sql,Objectargs ) throws SQLException, ClassNotFoundException, IOException{
List<County> counties=new ArrayList<County>();
//准备去查数据库
Jdbctools jt=new Jdbctools();
Connection ct=null;
ct=jtgetConnection();
PreparedStatement ps=null;
ResultSet rs=null;
ps=ctprepareStatement(sql);
for(int i=0;i<argslength;i++){
pssetObject(i+1, args[i]);
}
rs=psexecuteQuery();
while(rsnext()){
countiesadd(new County(rsgetInt("county_id"),rsgetString("county_name")));
}
jtshifanglianjie(ct, ps, rs);
return counties;

}
}
c3p0-configxml的代码如下:
[html] view plain copy
<xml version="10" encoding="UTF-8">
<c3p0-config>
<named-config name="helloc3p0">
<!-- 连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password"></property>
<property name="driverClass">commysqljdbcDriver</property>
<property name="jdbcUrl">jdbc:mysql:///thereaction</property>

<!-- 若数据库中连接数不足时,一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
<!-- c3p0数据库连接可以维护的statement的个数 -->
<property name="maxStatements">20</property>
<!-- 每个连接同时可以使用的statement对象的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
创建servletjava 文件
[java] view plain copy
package comxiaojieservlet;

import javaioIOException;
import javalangreflectMethod;
import javasqlSQLException;
import javautilList;

import javaxservletServletException;
import javaxservletannotationWebServlet;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;

import comfasterxmljacksondatabindObjectMapper;
import comxiaojiebeansCity;
import comxiaojiebeansCounty;
import comxiaojiebeansProvince;
import comxiaojiedaoDAO;
/
Servlet implementation class ThreeactiondServlet
/
@WebServlet("/threeactiondServlet")
public class ThreeactiondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodname=requestgetParameter("method");
try {
Method method=getClass()getDeclaredMethod(methodname,HttpServletRequestclass,HttpServletResponseclass);
methodinvoke(this, request,response);//调用各自的方法
} catch (Exception e) {
eprintStackTrace();
}
}
private DAO dao=new DAO();
protected void province(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException {
Systemoutprintln("province的servlet进入了");
String sql="select province_id,province_name from province";
List<Province> provinces=daogetprovince(sql);
requestsetAttribute("provinces", provinces);
Systemoutprintln(provinces);
//注意:这里不能用重定向的形式,因为我们好不容易在request请求域中存储了省的信息,目的是在前台页面中能够从请求域中获取到我们存在数据库中的值
//故这里只能用转发的方式
requestgetRequestDispatcher("/index2jsp")forward(request, response);
}
protected void city(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException {
Systemoutprintln("city的servlet进入了");
String province_id=requestgetParameter("province_id");
String sql="select city_id,city_name from city where province_id=";
List<City> cities=daogetcity(sql,IntegerparseInt(province_id));
ObjectMapper mapper=new ObjectMapper();
String result=mapperwriteValueAsString(cities);
Systemoutprintln(result);
responsesetContentType("text/javascript");
responsegetWriter()print(result);
}
protected void county(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException {
Systemoutprintln("county的servlet进入了");
String city_id=requestgetParameter("city_id");
String sql="select county_id,county_name from county where city_id=";
List<County> counties=daogetcounty(sql,IntegerparseInt(city_id));
ObjectMapper mapper=new ObjectMapper();
String result=mapperwriteValueAsString(counties);
Systemoutprintln(result);
responsesetContentType("text/javascript");
responsegetWriter()print(result);
}
}

以上就是关于省市区 地址联动 数据怎么创建全部的内容,包括:省市区 地址联动 数据怎么创建、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9476154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存