JSP复习笔记——第10章 连接数据库 之 DAO设计模式

之前的开发可以发现以下问题:
1、 所有的JDBC代码写在JSP页面之中,维护困难
2、 JSP中不应该使用任何SQL包,即不能在JSP中直接使用java.sql.*,原因是JSP只关注于数据的显示,而不关心数据是从哪儿来,或向哪里存储
3、 所有的数据库操作代码最好使用PreparedStatement

区分:J2EE的组件层次
客户端 表示层  业务层  数据层  数据库
*.jsp/servlet
DAO属于J2EE数据层的操作
即:在DAO中封装一个表在一个项目中所应该具有的所有的操作

Java代码
1.create table person
2.(
3.id varchar(32) not null primary key,
4.name varchar(20) not null,
5.password varchar(20) not null,
6.age varchar(20) not null,
7.email varchar(20) not null
8.);
create table person
(
id varchar(32) not null primary key,
name varchar(20) not null,
password varchar(20) not null,
age varchar(20) not null,
email varchar(20) not null
);


程序在变更数据库之后,前台页面不会出现过多改变?

首先需要规定出整个模块之中对person表的全部操作:
*增加
*删除
*修改
* 按ID查询
* 查询全部
* 模糊查询

按以上要求,规定出操作此张表的标准,之后只要针对于不同的数据库实现这些标准即可
在JAVA中只有通过接口可以定义出标准  DAO规定的就是一个接口

插入  针对对象插入

对象 VO、TO、POJO(值对象、传输对象、最根本的JAVA对象)
即:只包含属性和 setter、 getter方法的类
客户 – vo  DAO
VO的对象与表中的字段对应

定义好接口之后,要定义出接口的具体实现类,具体实现DAO接口中对数据库表的一切操作
可以发现以下的一个重要问题:
PersonDAO dao = new PersonDAOImpl();接口直接通过其子类实例化,直接的影响就是程序在调用时必须知道具体的子类,这样会造成修改不方便
所以,必须使用工厂设计,是前台不关注于具体子类
DAO整体设计,是采用以下模式:
调用处  DAO工厂 具体子类实现 完成数据库操作
|------------------|------------VO-----------------------|
直接的好处:前台显示与后台逻辑操作分离

Java代码
1.package org.sky.darkness.factory ;
2.
3.import org.sky.darkness.dao.* ;
4.import org.sky.darkness.dao.impl.* ;
5.
6.public class DAOFactory
7.{
8. public static PersonDAO getPersonDAOInstance()
9. {
10. return new PersonDAOImpl() ;
11. }
12.};
package org.sky.darkness.factory ;

import org.sky.darkness.dao.* ;
import org.sky.darkness.dao.impl.* ;

public class DAOFactory
{
public static PersonDAO getPersonDAOInstance()
{
return new PersonDAOImpl() ;
}
};


--PersonDAO.java

Java代码
1.package org.sky.darkness.dao ;
2.
3.import java.util.* ;
4.import org.sky.darkness.vo.* ;
5.
6.// 规定出了操作person表在此项目里的全部方法
7.public interface PersonDAO
8.{
9. // 增加操作
10. public void insert(Person person) throws Exception ;
11. // 修改操作
12. public void update(Person person) throws Exception ;
13. // 删除操作
14. public void delete(String id) throws Exception ;
15. // 按ID查询操作
16. public Person queryById(String id) throws Exception ;
17. // 查询全部
18. public List queryAll() throws Exception ;
19. // 模糊查询
20. public List queryByLike(String cond) throws Exception ;
21.}
package org.sky.darkness.dao ;

import java.util.* ;
import org.sky.darkness.vo.* ;

// 规定出了操作person表在此项目里的全部方法
public interface PersonDAO
{
// 增加操作
public void insert(Person person) throws Exception ;
// 修改操作
public void update(Person person) throws Exception ;
// 删除操作
public void delete(String id) throws Exception ;
// 按ID查询操作
public Person queryById(String id) throws Exception ;
// 查询全部
public List queryAll() throws Exception ;
// 模糊查询
public List queryByLike(String cond) throws Exception ;
}


Java代码
1.package org.sky.darkness.vo ;
2.
3.// 值对象,包含属性,setter,getter方法
4.public class Person
5.{
6. private String id ;
7. private String name ;
8. private String password ;
9. private int age ;
10. private String email ;
11.
12. // 生成getter、setter方法
13. public void setId(String id)
14. {
15. this.id = id ;
16. }
17. public void setName(String name)
18. {
19. this.name = name ;
20. }
21. public void setPassword(String password)
22. {
23. this.password = password ;
24. }
25. public void setAge(int age)
26. {
27. this.age = age ;
28. }
29. public void setEmail(String email)
30. {
31. this.email = email ;
32. }
33. public String getId()
34. {
35. return this.id ;
36. }
37. public String getName()
38. {
39. return this.name ;
40. }
41. public String getPassword()
42. {
43. return this.password ;
44. }
45. public int getAge()
46. {
47. return this.age ;
48. }
49. public String getEmail()
50. {
51. return this.email ;
52. }
53.};
package org.sky.darkness.vo ;

// 值对象,包含属性,setter,getter方法
public class Person
{
private String id ;
private String name ;
private String password ;
private int age ;
private String email ;

// 生成getter、setter方法
public void setId(String id)
{
this.id = id ;
}
public void setName(String name)
{
this.name = name ;
}
public void setPassword(String password)
{
this.password = password ;
}
public void setAge(int age)
{
this.age = age ;
}
public void setEmail(String email)
{
this.email = email ;
}
public String getId()
{
return this.id ;
}
public String getName()
{
return this.name ;
}
public String getPassword()
{
return this.password ;
}
public int getAge()
{
return this.age ;
}
public String getEmail()
{
return this.email ;
}
};


Java代码
1.package org.sky.darkness.dao.impl ;
2.import java.sql.* ;
3.import java.util.* ;
4.import org.sky.darkness.vo.* ;
5.import org.sky.darkness.dbc.* ;
6.import org.sky.darkness.dao.* ;
7.
8.// 此类需要完成具体的数据库操作,需要JDBC代码
9.public class PersonDAOImpl implements PersonDAO
10.{
11. // 增加操作
12. public void insert(Person person) throws Exception
13. {
14. String sql = "INSERT INTO person (id,name,password,age,email) VALUES (?,?,?,?,?)" ;
15. PreparedStatement pstmt = null ;
16. DataBaseConnection dbc = null ;
17.
18. // 下面是针对数据库的具体操作
19. try
20. {
21. // 连接数据库
22. dbc = new DataBaseConnection() ;
23. pstmt = dbc.getConnection().prepareStatement(sql) ;
24. pstmt.setString(1,person.getId()) ;
25. pstmt.setString(2,person.getName()) ;
26. pstmt.setString(3,person.getPassword()) ;
27. pstmt.setInt(4,person.getAge()) ;
28. pstmt.setString(5,person.getEmail()) ;
29. // 进行数据库更新操作
30. pstmt.executeUpdate() ;
31. pstmt.close() ;
32. }
33. catch (Exception e)
34. {
35. throw new Exception("操作出现异常") ;
36. }
37. finally
38. {
39. // 关闭数据库连接
40. dbc.close() ;
41. }
42. }
43. // 修改操作
44. public void update(Person person) throws Exception
45. {
46. String sql = "UPDATE person SET name=?,password=?,age=?,email=? WHERE id=?" ;
47. PreparedStatement pstmt = null ;
48. DataBaseConnection dbc = null ;
49.
50. // 下面是针对数据库的具体操作
51. try
52. {
53. // 连接数据库
54. dbc = new DataBaseConnection() ;
55. pstmt = dbc.getConnection().prepareStatement(sql) ;
56. pstmt.setString(1,person.getName()) ;
57. pstmt.setString(2,person.getPassword()) ;
58. pstmt.setInt(3,person.getAge()) ;
59. pstmt.setString(4,person.getEmail()) ;
60. pstmt.setString(5,person.getId()) ;
61. // 进行数据库更新操作
62. pstmt.executeUpdate() ;
63. pstmt.close() ;
64. }
65. catch (Exception e)
66. {
67. throw new Exception("操作出现异常") ;
68. }
69. finally
70. {
71. // 关闭数据库连接
72. dbc.close() ;
73. }
74. }
75. // 删除操作
76. public void delete(String id) throws Exception
77. {
78. String sql = "DELETE FROM person WHERE id=?" ;
79. PreparedStatement pstmt = null ;
80. DataBaseConnection dbc = null ;
81.
82. // 下面是针对数据库的具体操作
83. try
84. {
85. // 连接数据库
86. dbc = new DataBaseConnection() ;
87. pstmt = dbc.getConnection().prepareStatement(sql) ;
88. pstmt.setString(1,id) ;
89. // 进行数据库更新操作
90. pstmt.executeUpdate() ;
91. pstmt.close() ;
92. }
93. catch (Exception e)
94. {
95. throw new Exception("操作出现异常") ;
96. }
97. finally
98. {
99. // 关闭数据库连接
100. dbc.close() ;
101. }
102. }
103. // 按ID查询操作
104. public Person queryById(String id) throws Exception
105. {
106. Person person = null ;
107. String sql = "SELECT id,name,password,age,email FROM person WHERE id=?" ;
108. PreparedStatement pstmt = null ;
109. DataBaseConnection dbc = null ;
110.
111. // 下面是针对数据库的具体操作
112. try
113. {
114. // 连接数据库
115. dbc = new DataBaseConnection() ;
116. pstmt = dbc.getConnection().prepareStatement(sql) ;
117. pstmt.setString(1,id) ;
118. // 进行数据库查询操作
119. ResultSet rs = pstmt.executeQuery() ;
120. if(rs.next())
121. {
122. // 查询出内容,之后将查询出的内容赋值给person对象
123. person = new Person() ;
124. person.setId(rs.getString(1)) ;
125. person.setName(rs.getString(2)) ;
126. person.setPassword(rs.getString(3)) ;
127. person.setAge(rs.getInt(4)) ;
128. person.setEmail(rs.getString(5)) ;
129. }
130. rs.close() ;
131. pstmt.close() ;
132. }
133. catch (Exception e)
134. {
135. throw new Exception("操作出现异常") ;
136. }
137. finally
138. {
139. // 关闭数据库连接
140. dbc.close() ;
141. }
142. return person ;
143. }
144. // 查询全部
145. public List queryAll() throws Exception
146. {
147. List all = new ArrayList() ;
148. String sql = "SELECT id,name,password,age,email FROM person" ;
149. PreparedStatement pstmt = null ;
150. DataBaseConnection dbc = null ;
151.
152. // 下面是针对数据库的具体操作
153. try
154. {
155. // 连接数据库
156. dbc = new DataBaseConnection() ;
157. pstmt = dbc.getConnection().prepareStatement(sql) ;
158. // 进行数据库查询操作
159. ResultSet rs = pstmt.executeQuery() ;
160. while(rs.next())
161. {
162. // 查询出内容,之后将查询出的内容赋值给person对象
163. Person person = new Person() ;
164. person.setId(rs.getString(1)) ;
165. person.setName(rs.getString(2)) ;
166. person.setPassword(rs.getString(3)) ;
167. person.setAge(rs.getInt(4)) ;
168. person.setEmail(rs.getString(5)) ;
169.
170. // 将查询出来的数据加入到List对象之中
171. all.add(person) ;
172. }
173. rs.close() ;
174. pstmt.close() ;
175. }
176. catch (Exception e)
177. {
178. throw new Exception("操作出现异常") ;
179. }
180. finally
181. {
182. // 关闭数据库连接
183. dbc.close() ;
184. }
185. return all ;
186. }
187. // 模糊查询
188. public List queryByLike(String cond) throws Exception
189. {
190. List all = new ArrayList() ;
191. String sql = "SELECT id,name,password,age,email FROM person WHERE name LIKE ? or email LIKE ?" ;
192. PreparedStatement pstmt = null ;
193. DataBaseConnection dbc = null ;
194.
195. // 下面是针对数据库的具体操作
196. try
197. {
198. // 连接数据库
199. dbc = new DataBaseConnection() ;
200. pstmt = dbc.getConnection().prepareStatement(sql) ;
201. // 设置模糊查询条件
202. pstmt.setString(1,"%"+cond+"%") ;
203. pstmt.setString(2,"%"+cond+"%") ;
204. // 进行数据库查询操作
205. ResultSet rs = pstmt.executeQuery() ;
206. while(rs.next())
207. {
208. // 查询出内容,之后将查询出的内容赋值给person对象
209. Person person = new Person() ;
210. person.setId(rs.getString(1)) ;
211. person.setName(rs.getString(2)) ;
212. person.setPassword(rs.getString(3)) ;
213. person.setAge(rs.getInt(4)) ;
214. person.setEmail(rs.getString(5)) ;
215.
216. // 将查询出来的数据加入到List对象之中
217. all.add(person) ;
218. }
219. rs.close() ;
220. pstmt.close() ;
221. }
222. catch (Exception e)
223. {
224. throw new Exception("操作出现异常") ;
225. }
226. finally
227. {
228. // 关闭数据库连接
229. dbc.close() ;
230. }
231. return all ;
232. }
233.};
package org.sky.darkness.dao.impl ;
import java.sql.* ;
import java.util.* ;
import org.sky.darkness.vo.* ;
import org.sky.darkness.dbc.* ;
import org.sky.darkness.dao.* ;

// 此类需要完成具体的数据库操作,需要JDBC代码
public class PersonDAOImpl implements PersonDAO
{
// 增加操作
public void insert(Person person) throws Exception
{
String sql = "INSERT INTO person (id,name,password,age,email) VALUES (?,?,?,?,?)" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;

// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,person.getId()) ;
pstmt.setString(2,person.getName()) ;
pstmt.setString(3,person.getPassword()) ;
pstmt.setInt(4,person.getAge()) ;
pstmt.setString(5,person.getEmail()) ;
// 进行数据库更新操作
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
}
// 修改操作
public void update(Person person) throws Exception
{
String sql = "UPDATE person SET name=?,password=?,age=?,email=? WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;

// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,person.getName()) ;
pstmt.setString(2,person.getPassword()) ;
pstmt.setInt(3,person.getAge()) ;
pstmt.setString(4,person.getEmail()) ;
pstmt.setString(5,person.getId()) ;
// 进行数据库更新操作
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
}
// 删除操作
public void delete(String id) throws Exception
{
String sql = "DELETE FROM person WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;

// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,id) ;
// 进行数据库更新操作
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
}
// 按ID查询操作
public Person queryById(String id) throws Exception
{
Person person = null ;
String sql = "SELECT id,name,password,age,email FROM person WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;

// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,id) ;
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给person对象
person = new Person() ;
person.setId(rs.getString(1)) ;
person.setName(rs.getString(2)) ;
person.setPassword(rs.getString(3)) ;
person.setAge(rs.getInt(4)) ;
person.setEmail(rs.getString(5)) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
return person ;
}
// 查询全部
public List queryAll() throws Exception
{
List all = new ArrayList() ;
String sql = "SELECT id,name,password,age,email FROM person" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;

// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给person对象
Person person = new Person() ;
person.setId(rs.getString(1)) ;
person.setName(rs.getString(2)) ;
person.setPassword(rs.getString(3)) ;
person.setAge(rs.getInt(4)) ;
person.setEmail(rs.getString(5)) ;

// 将查询出来的数据加入到List对象之中
all.add(person) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
return all ;
}
// 模糊查询
public List queryByLike(String cond) throws Exception
{
List all = new ArrayList() ;
String sql = "SELECT id,name,password,age,email FROM person WHERE name LIKE ? or email LIKE ?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;

// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
// 设置模糊查询条件
pstmt.setString(1,"%"+cond+"%") ;
pstmt.setString(2,"%"+cond+"%") ;
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给person对象
Person person = new Person() ;
person.setId(rs.getString(1)) ;
person.setName(rs.getString(2)) ;
person.setPassword(rs.getString(3)) ;
person.setAge(rs.getInt(4)) ;
person.setEmail(rs.getString(5)) ;

// 将查询出来的数据加入到List对象之中
all.add(person) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
return all ;
}
};

定义一个数据库连接类,她只负责数据库的连接:

Java代码
1.package org.sky.darkness.dbc ;
2.import java.sql.* ;
3.
4.// 主要功能就是连接数据库、关闭数据库
5.public class DataBaseConnection
6.{
7. private final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
8. private final String DBURL = "jdbc:oracle:thin:@localhost:1521:SKY" ;
9. private final String DBUSER = "scott" ;
10. private final String DBPASSWORD = "darkness" ;
11. private Connection conn = null ;
12.
13. public DataBaseConnection()
14. {
15. try
16. {
17. Class.forName(DBDRIVER) ;
18. this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
19. }
20. catch (Exception e)
21. {
22. }
23. }
24.
25. // 取得数据库连接
26. public Connection getConnection()
27. {
28. return this.conn ;
29. }
30.
31. // 关闭数据库连接
32. public void close()
33. {
34. try
35. {
36. this.conn.close() ;
37. }
38. catch (Exception e)
39. {
40. }
41. }
42.};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值