时间:2021-07-01 10:21:17 帮助过:11人阅读
1 package com.czgo.db;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6
7 public class DBUtil
8 {
9 private static final String URL = "jdbc:mysql://127.0.0.1:3306/imooc";
10 private static final String UNAME = "root";
11 private static final String PWD = "root";
12
13 private static Connection conn = null;
14
15 static
16 {
17 try
18 {
19 // 1.加载驱动程序
20 Class.forName("com.mysql.jdbc.Driver");
21 // 2.获得数据库的连接
22 conn = DriverManager.getConnection(URL, UNAME, PWD);
23 }
24 catch (ClassNotFoundException e)
25 {
26 e.printStackTrace();
27 }
28 catch (SQLException e)
29 {
30 e.printStackTrace();
31 }
32 }
33
34 public static Connection getConnection()
35 {
36 return conn;
37 }
38 }
第四步,创建实体类(如上图,大家观察包的分配,我们将采用MVC思想设计本实例,有关于mvc的设计思想,请大家自行学习,这里不多说)代码如下:
1 package com.czgo.model;
2
3 import java.io.Serializable;
4
5 /**
6 * 实体类:女神类
7 *
8 * @author AlanLee
9 *
10 */
11 public class Goddess implements Serializable
12 {
13 private static final long serialVersionUID = 1L;
14
15 /**
16 * 唯一主键
17 */
18 private Integer id;
19 /**
20 * 姓名
21 */
22 private String name;
23 /**
24 * 手机号码
25 */
26 private String mobie;
27 /**
28 * 电子邮件
29 */
30 private String email;
31 /**
32 * 家庭住址
33 */
34 private String address;
35
36 public Integer getId()
37 {
38 return id;
39 }
40
41 public void setId(Integer id)
42 {
43 this.id = id;
44 }
45
46 public String getName()
47 {
48 return name;
49 }
50
51 public void setName(String name)
52 {
53 this.name = name;
54 }
55
56 public String getMobie()
57 {
58 return mobie;
59 }
60
61 public void setMobie(String mobie)
62 {
63 this.mobie = mobie;
64 }
65
66 public String getEmail()
67 {
68 return email;
69 }
70
71 public void setEmail(String email)
72 {
73 this.email = email;
74 }
75
76 public String getAddress()
77 {
78 return address;
79 }
80
81 public void setAddress(String address)
82 {
83 this.address = address;
84 }
85 }
第五步,dao层的实现(这里由于是小例子没有写dao接口,实际工作中大型项目应该是要写dao接口的,便于程序的维护和扩展),代码如下:
1 package com.czgo.dao;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import com.czgo.db.DBUtil;
11 import com.czgo.model.Goddess;
12
13 /**
14 * 数据层处理类
15 *
16 * @author AlanLee
17 *
18 */
19 public class GoddessDao
20 {
21 /**
22 * 查询全部女神
23 *
24 * @return
25 * @throws SQLException
26 */
27 public List<Goddess> query()
28 {
29 List<Goddess> goddessList = new ArrayList<Goddess>();
30
31 // 获得数据库连接
32 Connection conn = null;
33 ResultSet rs = null;
34 PreparedStatement ptmt = null;
35
36 try
37 {
38 conn = DBUtil.getConnection();
39
40 StringBuilder sb = new StringBuilder();
41 sb.append("select id,name,mobie,email,address from goddess");
42
43 // 通过数据库的连接操作数据库,实现增删改查
44 ptmt = conn.prepareStatement(sb.toString());
45
46 rs = ptmt.executeQuery();
47
48 Goddess goddess = null;
49
50 while (rs.next())
51 {
52 goddess = new Goddess();
53 goddess.setId(rs.getInt("id"));
54 goddess.setName(rs.getString("name"));
55 goddess.setMobie(rs.getString("mobie"));
56 goddess.setEmail(rs.getString("email"));
57 goddess.setAddress(rs.getString("address"));
58
59 goddessList.add(goddess);
60 }
61 }
62 catch (SQLException e)
63 {
64 e.printStackTrace();
65 }
66 finally
67 {
68
69 try
70 {
71 if (rs != null)
72 {
73 rs.close();
74 }
75 if (ptmt != null)
76 {
77 ptmt.close();
78 }
79 }
80 catch (SQLException e)
81 {
82 e.printStackTrace();
83 }
84 }
85
86 return goddessList;
87 }
88
89 /**
90 * 查询单个女神
91 *
92 * @return
93 * @throws SQLException
94 */
95 public Goddess queryById(Integer id)
96 {
97 Goddess g = null;
98
99 Connection conn = null;
100 PreparedStatement ptmt = null;
101 ResultSet rs = null;
102
103 try
104 {
105 conn = DBUtil.getConnection();
106
107 String sql = "" + " select * from imooc_goddess " + " where id=? ";
108
109 ptmt = conn.prepareStatement(sql);
110
111 ptmt.setInt(1, id);
112
113 rs = ptmt.executeQuery();
114
115 while (rs.next())
116 {
117 g = new Goddess();
118 g.setId(rs.getInt("id"));
119 g.setName(rs.getString("name"));
120 g.setMobie(rs.getString("mobie"));
121 g.setEmail(rs.getString("email"));
122 g.setAddress(rs.getString("address"));
123 }
124 }
125 catch (SQLException e)
126 {
127 e.printStackTrace();
128 }
129 finally
130 {
131 try
132 {
133 if (rs != null)
134 {
135 rs.close();
136 }
137 if (ptmt != null)
138 {
139 ptmt.close();
140 }
141 }
142 catch (SQLException e)
143 {
144 e.printStackTrace();
145 }
146 }
147
148 return g;
149 }
150
151 /**
152 * 添加女神
153 *
154 * @throws SQLException
155 */
156 public void addGoddess(Goddess goddess)
157 {
158 // 获得数据库连接
159 Connection conn = null;
160 PreparedStatement ptmt = null;
161
162 try
163 {
164 conn = DBUtil.getConnection();
165
166 String sql = "insert into goddess(name,mobie,email,address) values(?,?,?,?)";
167
168 ptmt = conn.prepareStatement(sql);
169
170 ptmt.setString(1, goddess.getName());
171 ptmt.setString(2, goddess.getMobie());
172 ptmt.setString(3, goddess.getEmail());
173 ptmt.setString(4, goddess.getAddress());
174
175 ptmt.execute();
176 }
177 catch (SQLException e)
178 {
179 e.printStackTrace();
180 }
181 finally
182 {
183 try
184 {
185 if (ptmt != null)
186 {
187 ptmt.close();
188 }
189 }
190 catch (SQLException e)
191 {
192 e.printStackTrace();
193 }
194 }
195 }
196
197 /**
198 * 修改女神资料
199 *
200 * @throws SQLException
201 */
202 public void updateGoddess(Goddess goddess)
203 {
204 Connection conn = null;
205 PreparedStatement ptmt = null;
206
207 try
208 {
209 conn = DBUtil.getConnection();
210
211 String sql = "update goddess set name=?,mobie=?,email=?,address=? where id=?";
212
213 ptmt = conn.prepareStatement(sql);
214
215 ptmt.setString(1, goddess.getName());
216 ptmt.setString(2, goddess.getMobie());
217 ptmt.setString(3, goddess.getEmail());
218 ptmt.setString(4, goddess.getAddress());
219
220 ptmt.execute();
221 }
222 catch (SQLException e)
223 {
224 e.printStackTrace();
225 }
226 finally
227 {
228 try
229 {
230 if (ptmt != null)
231 {
232 ptmt.close();
233 }
234 }
235 catch (SQLException e)
236 {
237 e.printStackTrace();
238 }
239 }
240 }
241
242 /**
243 * 删除女神
244 *
245 * @throws SQLException
246 */
247 public void deleteGoddess(Integer id)
248 {
249 Connection conn = null;
250 PreparedStatement ptmt = null;
251
252 try
253 {
254 conn = DBUtil.getConnection();
255
256 String sql = "delete from goddess where id=?";
257
258 ptmt = conn.prepareStatement(sql);
259
260 ptmt.setInt(1, id);
261
262 ptmt.execute();
263 }
264 catch (SQLException e)
265 {
266 e.printStackTrace();
267 }
268 finally
269 {
270 try
271 {
272 if (ptmt != null)
273 {
274 ptmt.close();
275 }
276 }
277 catch (SQLException e)
278 {
279 e.printStackTrace();
280 }
281 }
282 }
283 }
第六步,控制层的实现(控制层在此处用来模仿控制层和界面,直接在这里构建数据,如果是界面的数据则通过请求传递接收参数即可,控制层的代码大家可以根据实际情况去更改完善,这里只是给大家抛砖引玉,做个简单的测试,时间比较紧,希望大家理解),代码如下:
1 package com.czgo.action;
2
3 import java.sql.SQLException;
4 import java.util.List;
5
6 import com.czgo.dao.GoddessDao;
7 import com.czgo.model.Goddess;
8
9 /**
10 * 控制层,直接在这里构建数据,界面的数据则通过请求传递接收即可,亦是同理
11 *
12 * @author AlanLee
13 *
14 */
15 public class GoddessAction
16 {
17 /**
18 * 新增女神
19 *
20 * @param goddess
21 * @throws Exception
22 */
23 public void add(Goddess goddess) throws Exception
24 {
25 GoddessDao dao = new GoddessDao();
26 goddess.setName("苍井空");
27 goddess.setMobie("52220000");
28 goddess.setEmail("52220000@qq.com");
29 goddess.setAddress("北京红灯区");
30 dao.addGoddess(goddess);
31 }
32
33 /**
34 * 查询单个女神
35 *
36 * @param id
37 * @return
38 * @throws SQLException
39 */
40 public Goddess get(Integer id) throws SQLException
41 {
42 GoddessDao dao = new GoddessDao();
43 return dao.queryById(id);
44 }
45
46 /**
47 * 修改女神
48 *
49 * @param goddess
50 * @throws Exception
51 */
52 public void edit(Goddess goddess) throws Exception
53 {
54 GoddessDao dao = new GoddessDao();
55 dao.updateGoddess(goddess);
56 }
57
58 /**
59 * 删除女神
60 *
61 * @param id
62 * @throws SQLException
63 */
64 public void del(Integer id) throws SQLException
65 {
66 GoddessDao dao = new GoddessDao();
67 dao.deleteGoddess(id);
68 }
69
70 /**
71 * 查询全部女神
72 *
73 * @return
74 * @throws Exception
75 */
76 public List<Goddess> query() throws Exception
77 {
78 GoddessDao dao = new GoddessDao();
79 return dao.query();
80 }
81
82 /**
83 * 测试是否成功
84 *
85 * @param args
86 * @throws SQLException
87 */
88 public static void main(String[] args) throws SQLException
89 {
90 GoddessDao goddessDao = new GoddessDao();
91
92 List<Goddess> goddessList = goddessDao.query();
93
94 for (Goddess goddess : goddessList)
95 {
96 System.out.println(goddess.getName() + "," + goddess.getMobie() + "," + goddess.getEmail());
97 }
98 }
99 }
最后,让我们看一下main方法的运行结果是否成功:

这样,一个简单的java jdbc 连接mysql数据库 实现增删改查便完成了,大家可以在查询的基础上试着去做一个高级查询,也就是多条件查询来巩固jdbc的使用。时间比较紧,所以没有给大家一一测试增删改查的功能,闲着没事做蛋疼的可以都去测试一下,如果发现问题,希望能够指正小Alan,小Alan有空的时候便去修正博文中的一些错误。
补充:
小Alan在评论中和一些哥们扯了些疑问,看完文章后,记得看下面的评论,自己去思考一下,如何去完善我和哥们讨论的这些问题,技术永远是学不完的,关键是解决问题的思路和方法,这样不管遇到什么难题或者新技术都是可以迎难而上的,也是在公司工作的生存之道。
最新改动时间:2016年7月23日
可爱博主:AlanLee
转载于:http://www.cnblogs.com/AlanLee/p/5656640.html#commentform。
JDBC连接数据以及操作数据
标签:问题 add 查询 构建 row 姓名 工作 except value