C3P0---多条件查询
C3P0---多条件查询
一、多条件查询实现原理介绍
1、使用c3p0连接池技术,首先创建一个c3p0连接池。(c3p0连接池实现的原理是通过读取配置文件,实例化ComboPooledDataSource()类创建DataSource类型的连接池)
2、写一个查询功能的servlet,接受用户查询的条件-------执行查询------将结果封装到List<Bean>中转发到前端页面展示
3、写一个javaBean包含数据表格字段名称,实现servlet查询到的结果封装到javaBean中,同时提供jsp 展示页面读取javaBean中的数据。
4、写一个jsp页面实现用户提交查询条件和接收servlet返回的结果,遍历结果集展示查询的数据。
二、多条件查询的结构
三、1:先创建C3p0配置文件
c3p0-config.xml配置文件的名称和路径要和上面多条件查询截图一致,名称不可以随意写,位置必须在ClassPath路径。下面是配置文件内容:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<c3p0-config><!-- 配置的根元素 -->
-
<!-- 默认配置,如果没有指定则使用这个配置 -->
-
<default-config>
-
<!-- 连接的超时间时间,以秒 -->
-
<property name="checkoutTimeout">3000</property>
-
<!-- 空闲检查间隔,检查是否connection没有人用 -->
-
<property name="idleConnectionTestPeriod">1000</property>
-
<!-- 初始化的池大小 -->
-
<property name="initialPoolSize">3</property>
-
<!-- 最多有几个连接 -->
-
<property name="maxPoolSize">5</property>
-
<!-- 最少有几个连接 -->
-
<property name="minPoolSize">3</property>
-
<property name="autoCommitOnClose">true</property>
-
<!-- 如果池中数据连接不够时一次增长多少个 -->
-
<property name="acquireIncrement">2</property>
-
<!-- connection最大的空闲时间 -->
-
<property name="maxIdleTime">1000</property>
-
<!--
-
connection是从java代码到数据库的连接。所有statement都在上面传递sql语句。
-
那到这个connectoin最多可以同时让多少statement在上面传递数据呢?
-
-->
-
<property name="maxStatements">50</property>
-
<!-- 以下连接信息 -->
-
<property name="driverClass">com.mysql.jdbc.Driver</property>
-
<property name="jdbcUrl">
-
<![CDATA[jdbc:mysql://127.0.0.1:3306/long1?useUnicode=true&characterEncoding=UTF-8]]>
-
</property>
-
<property name="user">root</property>
-
<property name="password">123456</property>
-
</default-config>
-
<named-config name="myconfig">
-
<!-- 以下连接信息 -->
-
<property name="driverClass">com.mysql.jdbc.Driver</property>
-
<property name="jdbcUrl">
-
<![CDATA[jdbc:mysql://127.0.0.1:3306/long1?useUnicode=true&characterEncoding=UTF-8]]>
-
</property>
-
<property name="user">root</property>
-
<property name="password">123456</property>
-
</named-config>
-
</c3p0-config>
三、2:实现创建c3p0连接池实例:
-
package com.test.c3p0;
-
-
import java.sql.Connection;
-
import java.sql.SQLException;
-
-
import javax.sql.DataSource;
-
-
import com.mchange.v2.c3p0.ComboPooledDataSource;
-
-
public class C3p0Create {
-
-
//1、申明一个datasource变量
-
private static DataSource datasource;
-
-
//2、获取c3p0链接池对象
-
static{
-
//以下实例化comboPooledDataSource默认读取classpath下的c3p0-config.xml
-
//可以接收一个字符串,说明连接c3p0-config.xml中的哪一个命名的连接
-
datasource = new ComboPooledDataSource();
-
}
-
-
//3、创建DataSource和Connection两个连接池的方法
-
public static DataSource getDataSource(){
-
return datasource;
-
}
-
-
public static Connection getConnection(){
-
Connection con = null;
-
try {
-
con = datasource.getConnection();
-
} catch (SQLException e) {
-
-
e.printStackTrace();
-
}
-
return con;
-
}
-
}
四、创建javaBean为下面实现查询servlet做准备
1:创建一个javaBean,字段名称和数据表字段名称一致。
2:数据表格字段:
3:javaBean内容如下:
-
package com.test.bean;
-
-
public class StudentBean {
-
private Integer id;
-
private String namee;
-
private String sex;
-
private Integer birth;
-
private String department;
-
private String address;
-
-
public Integer getId() {
-
return id;
-
}
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
public String getNamee() {
-
return namee;
-
}
-
public void setNamee(String namee) {
-
this.namee = namee;
-
}
-
public String getSex() {
-
return sex;
-
}
-
public void setSex(String sex) {
-
this.sex = sex;
-
}
-
public Integer getBirth() {
-
return birth;
-
}
-
public void setBirth(Integer birth) {
-
this.birth = birth;
-
}
-
public String getDepartment() {
-
return department;
-
}
-
public void setDepartment(String department) {
-
this.department = department;
-
}
-
public String getAddress() {
-
return address;
-
}
-
public void setAddress(String address) {
-
this.address = address;
-
}
-
}
五、创建查询Servlet(核心部件)
-
package com.test.servlet;
-
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
import java.sql.SQLException;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import org.apache.commons.dbutils.QueryRunner;
-
import org.apache.commons.dbutils.handlers.BeanListHandler;
-
-
-
import com.test.bean.StudentBean;
-
import com.test.c3p0.C3p0Create;
-
-
public class QueryC3P0 extends HttpServlet {
-
-
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
response.setContentType("text/html;charset=UTF-8");
-
PrintWriter out = response.getWriter();
-
//1、设置编码格式
-
request.setCharacterEncoding("UTF-8");
-
//2、接收参数
-
String id = request.getParameter("id");
-
String namee = request.getParameter("namee");
-
String sex = request.getParameter("sex");
-
String birth = request.getParameter("birth");
-
String department = request.getParameter("department");
-
String address = request.getParameter("address");
-
//输出用户查询的条件
-
System.out.println("服务端接收的参数为:ID=="+id+"Name=="+namee+"Sex=="+sex+"Birth=="+birth+"department=="+department+"address=="+address);
-
-
-
//3、申明参数的集合类
-
List<Object> paramenter = new ArrayList<Object>();
-
//4、创建sql语句
-
String sql = "select * from student where 1=1";
-
//5、查询条件判断
-
if(id!=null && !id.trim().equals("")){
-
//sql链接符必须空格,负责sql语法报错。例如下面" and id=?" 在and前面有一个空格
-
sql+= " and id=?";
-
paramenter.add(id);
-
}
-
if(namee!=null && !namee.trim().equals("")){
-
sql+= " and namee like ?";
-
paramenter.add("%"+namee+"%");
-
}
-
if(sex!=null && !sex.trim().equals("")){
-
sql+=" and sex=?";
-
paramenter.add(sex);
-
}
-
if(birth!=null && !birth.trim().equals("")){
-
sql+=" and birth=?";
-
paramenter.add(birth);
-
}
-
if(department!=null && !department.trim().equals("")){
-
sql+=" and department like ?";
-
paramenter.add("%"+department+"%");
-
}
-
if(address!=null && !address.trim().equals("")){
-
sql+=" and address like ?";
-
paramenter.add("%"+address+"%");
-
}
-
System.out.println("查询的SQL语句为:"+sql);
-
-
//6、申明QueryRunner对象执行SQL语句,同时获取c3p0连接池
-
QueryRunner run = new QueryRunner(C3p0Create.getDataSource());
-
System.out.println("QueryRunner获取连接池对象为:==========="+run);
-
//7、执行sql语句,获取结果
-
try {
-
List<StudentBean> list = run.query(sql, new BeanListHandler<StudentBean>(StudentBean.class),paramenter.toArray());
-
//结果放到request属性中
-
request.setAttribute("parame", list);
-
-
} catch (SQLException e) {
-
-
e.printStackTrace();
-
}
-
-
-
//8、转发到前端页面
-
request.getRequestDispatcher("/jsp/query.jsp").forward(request,response);
-
-
}
-
-
}
六、创建提交查询提交和显示查询结果jsp页面
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<title>My JSP 'index.jsp' starting page</title>
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
</head>
-
<body>
-
<p>以下是查询条件:</p>
-
<form action="<c:url value='/QueryC3P0'/>" method="post">
-
编号:<input type="text" name="id"/><br/>
-
姓名:<input type="text" name="namee"/><br/>
-
性别:<select name="sex">
-
<option value="">不限</option>
-
<option value="男" <c:if test="${sex=='1'}">selected</c:if>>男</option>
-
<option value="女" <c:if test="${sex=='0'}">selected</c:if>>女</option>
-
</select>
-
<br/>
-
出生:<input type="text" name="birth"/><br/>
-
院系:<input type="text" name="department"/><br/>
-
地址:<input type="text" name="address"/><br/>
-
<input type="submit"/>
-
</form>
-
<hr/>
-
<table border="1" width="80%">
-
<tr>
-
<td>ID</td>
-
<td>Name</td>
-
<td>sex</td>
-
<td>birth</td>
-
<td>department</td>
-
<td>address</td>
-
</tr>
-
<c:forEach items="${parame}" var="p">
-
<tr>
-
<td>${p.id}</td>
-
<td>${p.namee}</td>
-
<%-- <td>${p.sex==0?'<font color="green">女</font>':'<font color="red">男</font>'}</td> --%>
-
<td>${p.sex}</td>
-
<td>${p.birth}</td>
-
<td>${p.department}</td>
-
<td>${p.address}</td>
-
</tr>
-
</c:forEach>
-
</table>
-
</body>
-
</html>
七、测试查询结果
1、输入查询地址:http://127.0.0.1:8080/test18_1/jsp/query.jsp
2、测试结果:
文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。
原文链接:brucelong.blog.csdn.net/article/details/78105341
- 点赞
- 收藏
- 关注作者
评论(0)