-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultSetTest.java
More file actions
78 lines (71 loc) · 1.77 KB
/
ResultSetTest.java
File metadata and controls
78 lines (71 loc) · 1.77 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
package io.github.hooj0.jdbc;
import io.github.hooj0.BasedTests;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* ResultSet操作
* @author hoojo
* @createDate 2011-5-14 下午12:58:54
* @file ResultSetTest.java
* @package com.hoo.base
* @project JavaJDBC
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class ResultSetTest extends BasedTests {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
public ResultSetTest() {
try {
DBHelper.loadParams(System.getProperty("user.dir") + "/src/jdbc.properties");
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateResultSetRowData() {
try {
conn = DBHelper.getConnection();
//创建一个可以滚动,可以更新的prepareStatement
ps = conn.prepareStatement("select * from temp", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery();
rs.last();//最后一条
int rows = rs.getRow();
//倒序循环
for (int i = rows; i > 0; i--) {
rs.absolute(i);
System.out.println(rs.getString(1) + "#" + rs.getString(2));
//修改当前记录的第二列值
rs.updateString(2, "Java" + i);
//执行修改
rs.updateRow();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
DBHelper.close();
}
}
public static void main(String[] args) {
ResultSetTest test = new ResultSetTest();
test.updateResultSetRowData();
}
}