Code reuse for JDBC

Source: Internet
Author: User

When using JDBC to connect to a database, we use ExecuteQuery (String sql) to get a result set. We need to iterate over the resultset result set according to different data structures when the database structure changes or when we get other database table result sets.

How can you establish a JDBC connection that is not related to the database structure? We can get the table structure by using the ResultSetMetaData () method. The result set is then traversed using the object[] array. When we want to achieve the corresponding results, we can use the iterator iterator. The result can be retrieved as long as the iterator is traversed.

Here's 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 {


private String URL = "Jdbc:oracle:thin: @localhost: 310-301:nitpro";


14


private String dbusername = "Scott";


16


The private String Dbuserpassword = "Tiger";


18


private Connection conn = null;


20


private Statement stmt = null;


22


private ResultSet rs = null;


24


public Newjdbc () {


try {


class.forname ("Oracle.jdbc.driver.OracleDriver");


catch (ClassNotFoundException e) {


E.printstacktrace ();


30}


31}


32


public Connection getconnection () {


try {


conn = drivermanager.getconnection (URL, dbusername, Dbuserpassword);


= catch (SQLException e) {


Notoginseng e.printstacktrace ();


38}


return conn;


40}


41


The public void Close (ResultSet rs, Statement stmt, Connection conn) {


(rs!= null) {


try {


Rs.close ();


} catch (SQLException e) {


E.printstacktrace ();


48}


49}


if (stmt!= null) {


Wuyi try {


Stmt.close ();


catch (SQLException e) {


E.printstacktrace ();


55}


56}


if (conn!= null) {


try {


Conn.close ();


catch (SQLException e) {


E.printstacktrace ();


62}


63}


64}


65


public List query (String sql) {


list = new ArrayList ();


68


conn = This.getconnection ();


try {


stmt = Conn.createstatement ();


= stmt.executequery (sql);


73//Get database table structure


resultsetmetadata rsm = rs.getmetadata ();


75//Get the number of columns in the database


int col = Rsm.getcolumncount ();


77//Generation Col Length Object array


object[] obj = new Object[col];


79//traverse the result set and save the result in an object array


while (Rs.next ()) {


Bayi for (int i = 0; i < col; i++) {


Obj[i] = rs.getobject (i + 1);


83}


list.add (obj);


85}


} catch (SQLException e) {


E.printstacktrace ();


} finally {


This.close (RS, stmt, conn);


90}


the return list;


92}


93


public void Update (String sql) {


try {


conn = This.getconnection ();


stmt = Conn.createstatement ();


stmt.executeupdate (SQL);


} catch (SQLException e) {


E.printstacktrace ();


101}


102}


103


the public static void main (String args[]) {


newjdbc NJ = new Newjdbc ();


, sql = "SELECT * from users";


list = nj.query (SQL);


108//Returns the iterator for the list


109 Iterator it = List.iterator ();


110//Traversal iterator, remove results 1z0-147


(It.hasnext ()) {


112 object[] o = (object[)) It.next ();


113 int id = ((BigDecimal) o[0]). Intvalue ();


114 System.out.println (ID);


115}


116


117}


118}

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.