JAVA是面向对象的语言,开发者在操作数据的时候,通常更习惯面对一个特定类型的对象,如一个用户就是一个User类的对象。DAO层需要做的,就是为上层提供充分的对象支持,让上层再也看不到具体的数据,而是一个个活生生的对象。
增加,删除,查询和修改操作是DAO需要做的最基本的4项操作。查询一般需要提供遍历查询和id查询,对于遍历查询,DAO需要提供User泛型的list对象,对于id查询则提供已经装配好数据的User对象,至于增加和修改操作,上层一般会提供一个User对象,DAO把User对象中的数据使用Insert语句插入到表格中。删除操作则只需提供一个id即可
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
class User{
private long id;
private String name;
private String gender;
public User(){
super ();
}
public User( long id,String name,String gender){
super ();
this .id = id;
this .name = name;
this .gender = gender;
}
//get,set方法
}
//DAO类
public class jdbcDao{
static {
try {
Class.forName( "com.mysql.jdbc.Driver" );
} catch (Exception e){
e.printStackTrace();
}
}
private Connection getConn(){
try {
return DriverManager.getConnection( "jdbc:mysql://localhost:3306:xe" , "root" , "password" );
} catch (Exception e){
e.printStackTrace();
}
}
return null ;
}
private void release(ResultSet rs,Statement ps,Connection conn){
if (rs!= null ){
try {
rs.close();
} catch (Exception e){
e.printStackTrace();
}
}
if (ps!= null ){
try {
ps.close();
} catch (Exception e){
e.printStackTrace();
}
}
if (conn!= null ){
try {
conn.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
//用ID获取用户对象
public User getUserById( long id){
ResultSet rs = null ;
PreparedStatement ps = null ;
Connection conn = null ;
String sql = "select * from user where id = ?" ;
try {
conn = this .getConnection();
ps = conn.prepareStatement(sql);
ps.setLong( 1 ,id);
rs = ps.executeQuery();
if (rs.next()){
//如果存在,则直接构建并返回用户对象
User user = new User(rs.getLong( "id" ),rs.getString( "name" ),rs.getString( "gender" ));
return user;
}
} catch (Exception e){
e.printStackTrace();
} finally {
this .release(rs,ps,conn);
}
return null ;
}
//查询所有用户
public List<User> getAllUsers(){
List<User> list = new ArrayList<User>();
ResultSet rs = null ;
PreparedStatement ps = null ;
Connection conn = null ;
String sql = "select * from user " ;
try {
conn = this .getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//循环添加用户对象
while (rs.next()){
User user = new User(rs.getLong( "id" ),rs.getString( "name" ),rs.getString( "gender" ));
list.add(user);
}
} catch (Exception e){
e.printStackTrace();
} finally {
this .release(rs,ps,conn);
}
return list;
}
//修改用户数据
public User updateUser(User user){
PreparedStatement ps = null ;
Connection conn = null ;
String sql = "update user set id =?,name=?,gender=?" ;
try {
conn = this .getConnection();
conn.setAutoCommit( false );
ps = conn.prepareStatement(sql);
ps.setLong( 1 ,user.getId());
ps.setString( 2 ,user.getName());
ps.setString( 3 ,user.getGender());
int rst = ps.executeUpdate();
if (rst> 0 ){
return new User(user.getId(),user.getName(),user.getGender());
}
conn.commit();
} catch (Exception e){
e.printStackTrace();
try {
conn.rollback();
} catch (Exception e1){
e1.printStackTrace();
}
} finally {
this .release( null ,ps,conn);
}
return null ;
}
}
//删除用户数据
public boolean deleteUser( long id){
PreparedStatement ps = null ;
Connection conn = null ;
String sql = "delete from user where id =?;
try {
conn = this .getConnection();
conn.setAutoCommit( false );
ps = conn.prepareStatement(sql);
ps.setLong( 1 ,user.getId());
ps.setString( 2 ,user.getName());
ps.setString( 3 ,user.getGender());
int rst = ps.executeUpdate();
if (rst> 0 ){
return user;
}
conn.commit();
} catch (Exception e){
e.printStackTrace();
try {
conn.rollback();
} catch (Exception e1){
e1.printStackTrace();
}
} finally {
this .release( null ,ps,conn);
}
return null ;
}
}
//插入用户数据
public User insertUser(User user){
PreparedStatement ps = null ;
Connection conn = null ;
String sql = "insert into user values(?,?,?)" ;
try {
conn = this .getConnection();
conn.setAutoCommit( false );
ps = conn.prepareStatement(sql);
ps.setLong( 1 ,user.getId());
ps.setString( 2 ,user.getName());
ps.setString( 3 ,user.getGender());
int rst = ps.executeUpdate();
if (rst> 0 ){
return user;
}
conn.commit();
} catch (Exception e){
e.printStackTrace();
try {
conn.rollback();
} catch (Exception e1){
e1.printStackTrace();
}
} finally {
this .release( null ,ps,conn);
}
return null ;
}
}
}
}
|
总结
以上就是本文关于使用JDBC实现数据访问对象层(DAO)代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出,小编会及时回复大家并改正。感谢朋友们对快网idc的支持!
原文链接:http://www.cnblogs.com/yzqm666/p/5910581.html
相关文章
猜你喜欢
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 51
-
2025-05-29 70
-
2025-05-27 50
-
2025-05-29 49
-
2025-05-25 17
热门评论