Code reuse in JDBC

Source: Internet
Author: User

When we connect to the database using JDBC, we use executeQuery (String SQL) to obtain a result set. When the database structure changes or other database table result sets are obtained, we need to re-traverse the ResultSet result set based on different data structures.
How can we establish a JDBC connection unrelated to the database structure? We can use the ResultSetMetaData () method to obtain the table structure. Then, use the Object [] array to traverse the result set. You can use the Iterator to obtain the expected results. The result can be retrieved by traversing the iterator.
The following is a method I wrote:
1 import java. math. BigDecimal;
2 import java. SQL. Connection;
3 import java. SQL. DriverManager;
4 import java. SQL. ResultSet;
5 import java. SQL. ResultSetMetaData;
6 import java. SQL. SQLException;
7 import java. SQL. Statement;
8 import java. util. ArrayList;
9 import java. util. Iterator;
10 import java. util. List;
11
12 public class newJdbc {
13 private String url = "jdbc: Oracle (large website database platform): 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;
22
23 private ResultSet rs = null;
24
25 public newJdbc (){
26 try {
27 Class. forName ("Oracle (large website database platform). jdbc. driver. Oracle (large website database platform) Driver ");
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.exe cuteQuery (SQL );
73 // obtain the database table structure
74 ResultSetMetaData rsm = rs. getMetaData ();
75 // obtain the number of columns in the database
76 int col = rsm. getColumnCount ();
77 // generate an array of objects with col Length
78 Object [] obj = new Object [col];
79 // traverse the result set and save the result to 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.exe cuteUpdate (SQL );
99} catch (SQLException e ){
100 & nbs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.