一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 使用JDBC實現數據訪問對象層(DAO)代碼示例

使用JDBC實現數據訪問對象層(DAO)代碼示例

2021-01-21 13:26少帥java Java教程

這篇文章主要介紹了使用JDBC實現數據訪問對象層(DAO)代碼示例,具有一定參考價值,需要的朋友可以了解下。

JAVA是面向對象的語言,開發者在操作數據的時候,通常更習慣面對一個特定類型的對象,如一個用戶就是一個User類的對象。DAO層需要做的,就是為上層提供充分的對象支持,讓上層再也看不到具體的數據,而是一個個活生生的對象。

增加,刪除,查詢和修改操作是DAO需要做的最基本的4項操作查詢一般需要提供遍歷查詢和id查詢,對于遍歷查詢,DAO需要提供User泛型的list對象,對于id查詢則提供已經裝配好數據的User對象,至于增加和修改操作,上層一般會提供一個User對象,DAO把User對象中的數據使用Insert語句插入到表格中。刪除操作則只需提供一個id即可

java" id="highlighter_864739">
?
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)代碼示例的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出,小編會及時回復大家并改正。感謝朋友們對服務器之家的支持!

原文鏈接:http://www.cnblogs.com/yzqm666/p/5910581.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲高清国产拍精品影院 | 我和寂寞孕妇的性事 | 国产好痛疼轻点好爽的视频 | 成人国产在线视频 | 日本在线小视频 | 四虎库| 邪恶肉肉全彩色无遮琉璃神社 | 亚洲福利一区 | 蜜桃久久久亚洲精品成人 | 国产玖玖在线观看 | 欧美性受xxxx88喷潮 | 继的朋友无遮漫画免费观看73 | 无限时间看片在线观看 | 久久艹影院 | 日本公乱妇视频 | 国产精品久久国产三级国电话系列 | 国内精品一区二区在线观看 | 四虎永久免费在线观看 | 99热这里只精品99re66 | 国产亚洲女在线线精品 | 日本javhd | 青草福利视频 | 9420高清完整版在线观看国语 | 国产精自产拍久久久久久 | 日韩激情视频在线观看 | 欧美人shou交在线播放 | 日韩一区二区三区精品 | 精品国产欧美一区二区三区成人 | 嫩草影院永久一二三入口 | 师尊被各种play打屁股 | 美女又爽又黄免费 | 午夜精品久久久 | 日本无翼乌漫画 | 亚洲爱视频| 成年视频在线播放 | www.天天操| 四虎影院大全 | 黄色aaa| 色综合久久日韩国产 | 果冻传媒在线观看的 | 色哟哟在线观看 |