设计功能的实现:
----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象
----采用MVC设计模式
----采用到的技术
。MVC设计模式,JSP,Servlet,POJO
。数据库使用mysql
。数据库连接池需要使用C3P0数据库连接池
。页面上的提示需要使用jQuery
----技术难点
。多个请求如何使用一个Servlet
。如何模糊查询
。如何在创建和修改的情况下,验证用户信息是否已被使用,并给出提示
---------------------------------------------------------------------------------------------------
1.首先数据库连接池C3P0的一些实现需要导五个开源的架包:
c3p0-0.9.1.2.jar
commons-dbcp-1.4.jar
commons-dbutils-1.3.jar
commons-pool-1.5.5.jar
mysql-connector-java-5.1.6-bin.jar
---------------------------------------------------------------------------
2.连接数据库连接池的文件为:在src目录下建立XML文件c3p0-config.xml;
root
lxn123
com.mysql.jdbc.Driver
jdbc:mysql:///test
5
10
10
50
20
5
---------------------------------------------------------------------------
3.连接数据库连接池的类:JdbcUtils
public class JdbcUtils {
//释放connection连接
public static void releaseConnection(Connection connection){
try {
if (connection!=null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//该静态代码块只执行一次
private static ComboPooledDataSource dataSource=null;
static{
//通过配置文件实现数据库的连接,只能被创建一次
dataSource=new ComboPooledDataSource("mvcapp");
}
//获取数据库连接,返回数据源的Connection对象
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
}
---------------------------------------------------------------------------
4.建立一个测试类(建立方法类似于class的建立,在src,所在包下建立,建立时找java下的JUnit,建立就行了):JdbcUtilsTest类,对数据库连接池进行测试,看是否连接成功!!!
public class JdbcUtilsTest {
//测试类包,测试数据库是否连接成功
@Test
public void testGetConnection() throws SQLException{
Connection connection=JdbcUtils.getConnection();
System.out.println(connection);
}
}
---------------------------------------------------------------------------
5.java是以面向对象的思想编程的,所以建立customer类,CriteriaCustomer类,对数据库里面的属性进行封装处理,便于后边功能调用的实现
customer类:对数据库里面的属性进行封装处理
public class Customer {
//建立了一个Customer类,对id,name,address,phone这些属性进行封装
private Integer id;
private String name;
private String address;
private String phone;
public Customer() {
super();
}
public Customer( String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
public Customer(Integer id, String name, String address, String phone) {
super();
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", phone=" + phone + "]";
}
}
-------------------
CriteriaCustomer类:模糊查询的类
//模糊查询的类
public class CriteriaCustomer {
private String name;
private String address;
private String phone;
public CriteriaCustomer() {
super();
}
public CriteriaCustomer(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
if (name==null) {
name="%%";
}
else{
name="%"+name+"%";
}
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
if (address==null) {
address="%%";
}
else{
address="%"+address+"%";
}
return address;
}
public void setAddress(String address) {
this.address = address;
}