ResultSet

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
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
 
public class ResultSetTest {
    private String driver;
    private String url;
    private String user;
    private String pass;
 
    public void initPara(String paraFile) throws Exception {
        Properties props = new Properties();
        props.load(new FileInputStream(paraFile));
        driver = props.getProperty("driver");
        url = props.getProperty("url");
        user = props.getProperty("username");
        pass = props.getProperty("password");
    }
 
    public void qurey(String sql) throws Exception {
        Class.forName(driver);
        try (Connection cnn = DriverManager.getConnection(url, user, pass);
                PreparedStatement pstmt = cnn.prepareStatement(sql,
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
                ResultSet rs = pstmt.executeQuery()) {
            rs.last();
            int rowCount = rs.getRow();
            for (int i = rowCount; i > 0; i--) {
                rs.absolute(i);
                System.out.println(rs.getString(1) + "\t" + rs.getString(2)
                        + "\t" + rs.getString(3) + "\t" + rs.getString(4));
                rs.updateString(2,"b"+i);
                rs.updateRow();
             
            }
        }
    }
     
     
    public static void main(String[] args) throws Exception{
        ResultSetTest rt = new ResultSetTest();
        rt.initPara("Mysql.ini");
        rt.qurey("select * from user");
    }
 
}

 

posted @   32ddd  阅读(154)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示