When using JDBC to connect to the database, we will use executeQuery(String sql) to obtain a result set. When the database structure changes or when other database table result sets are obtained, we need to re-traverse the ResultSet result set according to different data structures.
How can I establish a JDBC connection that is independent of the database structure? We can get the table structure by using the ResultSetMetaData() method. Then use the Object[] array to iterate over the result set. When we want to get the corresponding results, we can use Iterator. Just traverse the iterator to get the result.
Here is a method I wrote:
1import java.math.BigDecimal;
2import java.sql.Connection;
3import java.sql.DriverManager;
4import java.sql.ResultSet;
5import java.sql.ResultSetMetaData;
6import java.sql.SQLException;
7import java.sql.Statement;
8import java.util.ArrayList;
9import java.util.Iterator;
10import java.util.List;
11
12public class newJdbc {
13 private String url = "jdbc:oracle:thin:@localhost:1521:nitpro";
14
15 private String dbUserName = "scott";
16
17 private String dbUserPassword = "tiger";
18
19 private Connection conn = null;
20
21 private Statement stmt = null;
twenty two
23 private ResultSet rs = null;
twenty four
25 public newJdbc() {
26 try {
27 Class.forName("oracle.jdbc.driver.OracleDriver");
28 } catch (ClassNotFoundException e) {
29 e.printStackTrace();
30}
31}
32
33 public Connection getConnection() {
34 try {
35 conn = DriverManager.getConnection(url, dbUserName, dbUserPassword);
36 } catch (SQLException e) {
37 e.printStackTrace();
38 }
39 return conn;
40}
41
42 public void close(ResultSet rs, Statement stmt, Connection conn) {
43 if (rs != null) {
44 try {
45 rs.close();
46 } catch (SQLException e) {
47 e.printStackTrace();
48 }
49 }
50 if (stmt != null) {
51 try {
52 stmt.close();
53 } catch (SQLException e) {
54 e.printStackTrace();
55 }
56 }
57 if (conn != null) {
58 try {
59 conn.close();
60 } catch (SQLException e) {
61 e.printStackTrace();
62 }
63}
64}
65
66 public List query(String sql) {
67 List list = new ArrayList();
68
69 conn = this.getConnection();
70 try {
71 stmt = conn.createStatement();
72 rs = stmt.executeQuery(sql);
73 //Get the database table structure
74 ResultSetMetaData rsm = rs.getMetaData();
75 //Get the number of columns in the database
76 int col = rsm.getColumnCount();
77 //Generate Object array of col length
78 Object[] obj = new Object[col];
79 //Traverse the result set and store the results in the Object array
80 while (rs.next()) {
81 for (int i = 0; i < col; i++) {
82 obj[i] = rs.getObject(i + 1);
83}
84 list.add(obj);
85}
86 } catch (SQLException e) {
87 e.printStackTrace();
88 } finally {
89 this.close(rs, stmt, conn);
90}
91 return list;
92 }
93
94 public void update(String sql) {
95 try {
96 conn = this.getConnection();
97 stmt = conn.createStatement();
98 stmt.executeUpdate(sql);
99 } catch (SQLException e) {
100 e.printStackTrace();
101 }
102 }
103
104 public static void main(String args[]) {
105 newJdbc nj = new newJdbc();
106 String sql = "select * from users";
107 List list = nj.query(sql);
108 //Return the iterator of list
109 Iterator it = list.iterator();
110 //Traverse the iterator and get the result
111 while (it.hasNext()) {
112 Object[] o = (Object[]) it.next();
113 int id = ((BigDecimal) o[0]).intValue();
114 System.out.println(id);
115 }
116
117 }
118}
http://blog.csdn.net/xcl6996/archive/2007/06/22/1662633.aspx