forked from JavaDevTeam/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-jdbc.java
More file actions
385 lines (325 loc) · 11.1 KB
/
java-jdbc.java
File metadata and controls
385 lines (325 loc) · 11.1 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
------------------------------
JDBC |
------------------------------
# JDBC是由一些接口构成的API(Java Data Base Connectivity,java数据库连接);
# 连接数据库的步骤
①注册驱动(只做一次)
②建立连接(Connection)
③创建执行SQL语句(Statement)
④执行语句
⑤处理执行结果(ResultSet)
⑥释放资源
# MySql——url格式
* JDBC:子协议:子名称//主机名:端口/数据库名?属性名=属性值&
* 本机连接:"jdbc:mysql://www.cqsic.com:3366/wuyedb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true"
------------------------------
JDBC-API |
------------------------------
DriverManager
# 静态方法
Connection getConnection(String url, String user, String password) ;
* 根据URL用户密码,获取一个连接
Connection
# 代表一个数据库连接
# 实例方法
Statement createStatement();
* 创建一个Statement对象
PreparedStatement prepareStatement(String sql);
* 根据SQL出创建一个PreparedStatement对象
prepareStatement(String sql, int autoGeneratedKeys)
* 同上,多了一个设置,可以返回自动增加的key
CallableStatement prepareCall(String sql);
* 创建一个CallableStatement对象来调用数据库存储过程
setAutoCommit(boolean transaction);
* 如果该值为 false 则开启手动事务,需要手动执行 commit 才会提交
commit();
* 提交事务
rollback();
* 回滚事务
Statement
# 是一个接口,可以使用它来执行SQL语句
# 实例方法
boolean execute(String sql) ;
* 执行SQL
ResultSet executeQuery(String sql);
* 执行给定的 SQL 检索 语句,该语句返回单个 ResultSet 对象。
int executeUpdate(String sql)
* 执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。
* 返回受影响的行数
void addBatch(String sql);
* 添加一条批处理sql
int[] executeBatch()
* 执行添加的批处理语句
void clearBatch()
* 清空批处理
# 静态变量
int RETURN_GENERATED_KEYS = 1;
* 返回自动增长的key
|-PreparedStatement
# 预编译SQL的,statement,可以防止SQL注入
# 实例方法
setString(int index, String param);
* 给第一个问号赋值
* 有N多重载(setXxx),可以赋值不同的数据类型
* 注意'?'号的角标是从1开始,而不是0
void setCharacterStream(int parameterIndex,java.io.Reader reader, int length);
* 设置字符流
void setBinaryStream(int parameterIndex, java.io.InputStream x,long length
* 设置字节流
boolean execute()
* 执行SQL语句该语句可以是任何种类的 SQL 语句。
ResultSet executeQuery();
* 执行检索,并返回该查询生成的 ResultSet 对象。
int executeUpdate()
* 执行插入或者修改语句
ResultSet getGeneratedKeys() throws SQLException;
* 在执行 insert 后,可以执行该方法获取自增id
if (preparedStatement.executeUpdate() > 0) {
resultSet = preparedStatement.getGeneratedKeys();
if (resultSet.next()) {
int id = resultSet.getInt(1);
System.out.println(id);
}
}
* 创建 PreparedStatement 的时候需要有个额外的设置:cnnection.prepareStatement("..",Statement.RETURN_GENERATED_KEYS); 否则抛出异常
void addBatch()
* 添加当前编译后的语句到批处理
* 执行该方法后,会把当前编译的语句添加到批处理缓冲区。并且重置参数指针
|-CallableStatement
# 调用存储过程的 statement
ResultSet
# SQL执行结果集
# 实例方法
first()
* 将光标移动到此 ResultSet 对象的第一行。
next();
* 指针移到下一行,如果有下一行返回 true,反之返回 false
previous();
* 指针移到上一行
getObject(int columnIndex);
* 获取指定 index 列的数据,以Object形式返回
getObject(String columnLabel);
* 获取指定 列名称 的数据,以Object形式返回
getString(String columnName);
* 获取指定字段的String类型值
getString(int columnIndex);
* 获取指定索引的String类型值
InputStream getBinaryStream(String columnLabel)
* 获取二进制流
Reader getCharacterStream(String columnLabel);
* 获取字符流
* 有大量的 getXxx();存在,包含了JAVA各种基本数据类型的返回值
Blob
# 二进制数据对象
# 构造
Blob blob = new SeriaBlob(byte[] bytes);
# 实例方法
InputStream blob.getBinaryStream();
* 获取该 Blob 对象中的读取流
------------------------------
JDBC-操作二进制数据 |
------------------------------
# "标准SQL"中提供了如下类型来保存大的二进制数据类型
tinyblob 256B -- 二进制(字节)
blob 64k
mediumblob 16M
longblob 4G
# "标准SQL"中提供了如下类型来保存大的文本数据类型
tinyclob 256B
clob 645KB
mediumclob 16MB
longclob 4G
# "MYSQL"中定义的大数据字符类型
tinytext 256B
text 64KB
mediumtext 16MB
longtext 4G
* 一般,往MYSQL中存入过大数据的时候需要在在my.ini中最后添加配置
* max.allowed packet=10485760
* 表示设置MYSQL的数据存储大小
# JAVA操作二进制数据
* 主要是 Blob blob = new SeriaBlob(byte[] bytes); 对象
* 存入
public static void set()throws Exception
{
Connection conn = JDBCUtils.getConnection();
String sql = "insert into tab_bin values(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 1); //数据库ID字段
pstmt.setString(2, "测试.mp3"); //名称
/**
* 需要得到Blob对象
* 1,有文件,目标是Blob
* 3,把文件变成byte[]数组
* */
File f = new File("D:\\Demo.mp3");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
byte[] bytes = new byte[in.available()];//创建文件大小的字节数组
in.read(bytes);
Blob blob = new SerialBlob(bytes);
pstmt.setBlob(3, blob);//二进制文件
pstmt.executeUpdate();
}
* 读取
public static void get()throws Exception
{
//获得连接对象
Connection conn = JDBCUtils.getConnection();
String sql = "select * from tab_bin";
PreparedStatement pstmt = conn.prepareStatement(sql);
//pstmt执行查询得到ResultSet
ResultSet rs = pstmt.executeQuery();
//获取rs中名为Date的列数据
if(rs.next())
{
Blob blob = rs.getBlob("data");
/**
* 把 blob变回硬盘上的文件
* */
InputStream in = blob.getBinaryStream();
BufferedOutputStream bufr = new BufferedOutputStream(new FileOutputStream(new File("C:\\test.mp3")));
byte[] b = new byte[1024];
int len = 0;
while((len = in.read(b)) != -1)
{
bufr.write(b, 0, len);
bufr.flush();
}
}
}
------------------------------
JDBC-批处理 |
------------------------------
# Statement执行批处理
* 优点:
可以向数据库发送不同的SQL语句
* 缺点
SQL没有预编译
仅参数不同的SQL,需要重复写多条SQL
Connection connection = null;
Statement statement = connection.createStatement();
String sql1 = "UPDATE users SET name='zhongfucheng' WHERE id='3'";
String sql2 = "INSERT INTO users (id, name, password, email, birthday) VALUES('5','nihao','123','[email protected]','1995-12-1')";
//将sql添加到批处理
statement.addBatch(sql1);
statement.addBatch(sql2);
//执行批处理
statement.executeBatch();
//清空批处理的sql
statement.clearBatch();
# PreparedStatement批处理
* 优点:
SQL语句预编译了
对于同一种类型的SQL语句,不用编写很多条
* 缺点:
不能发送不同类型的SQL语句
Connection connection = UtilsDemo.getConnection();
String sql = "INSERT INTO test(id,name) VALUES (?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i = 1; i <= 205; i++) {
preparedStatement.setInt(1, i);
preparedStatement.setString(2, (i + "zhongfucheng"));
//添加到批处理中
preparedStatement.addBatch();
if (i %2 ==100) {
//执行批处理(200条记录)
preparedStatement.executeBatch();
//清空批处理【如果数据量太大,所有数据存入批处理,内存肯定溢出】
preparedStatement.clearBatch();
}
}
//不是所有的%2==100,剩下的再执行一次批处理(5条记录)
preparedStatement.executeBatch();
//再清空
preparedStatement.clearBatch();
UtilsDemo.release(connection, preparedStatement, null);
------------------------------
JDBC-大文本的读写 |
------------------------------
# 插入大文本数据
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = "INSERT INTO test2 (bigTest) VALUES(?) ";
preparedStatement = connection.prepareStatement(sql);
// 获取到文件的路径
String path = Main.class.getClassLoader().getResource("BigTest").getPath();
File file = new File(path);
FileReader fileReader = new FileReader(file);
// 第三个参数,由于测试的Mysql版本过低,所以只能用int类型的。高版本的不需要进行强转
preparedStatement.setCharacterStream(1, fileReader, (int) file.length());
if (preparedStatement.executeUpdate() > 0) {
System.out.println("插入成功");
}
# 读取大文本数据
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = "SELECT * FROM test2";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
//从结果集获取reader
Reader reader = resultSet.getCharacterStream("bigTest");
//目的文件
FileWriter fileWriter = new FileWriter("d:\\abc.txt");
//缓冲区
char[] chars = new char[1024];
int len = 0;
while ((len = reader.read(chars)) != -1) {
fileWriter.write(chars, 0, len);
fileWriter.flush();
}
fileWriter.close();
reader.close();
}
------------------------------
JDBC-大的二进制数据读写 |
------------------------------
# 插入大的二进制数据
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = "INSERT INTO test3 (blobtest) VALUES(?)";
preparedStatement = connection.prepareStatement(sql);
//获取文件的路径和文件对象
String path = Main.class.getClassLoader().getResource("1.wmv").getPath();
File file = new File(path);
//调用方法
preparedStatement.setBinaryStream(1, new FileInputStream(path), (int)file.length());
if (preparedStatement.executeUpdate() > 0) {
System.out.println("添加成功");
}
# 读取大的二进制数据
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql = "SELECT * FROM test3";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
//如果读取到数据,就把数据写到磁盘下
if (resultSet.next()) {
InputStream inputStream = resultSet.getBinaryStream("blobtest");
FileOutputStream fileOutputStream = new FileOutputStream("d:\\aa.jpg");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) > 0) {
fileOutputStream.write(bytes, 0, len);
}
fileOutputStream.close();
inputStream.close();
}
------------------------------
获取到自增长的字段 |
------------------------------
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "root");
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `test`(`name`) VALUES(?)",Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, "name");
if (preparedStatement.executeUpdate() > 0) {
ResultSet resultSet = preparedStatement.getGeneratedKeys();
if (resultSet.next()) {
int id = resultSet.getInt(1);
System.out.println("自增id="+id);
}
}