Use ResultSet to write a common query method.

Source: Internet
Author: User

Use ResultSet to write a common query method.

This method is very bad and will be optimized later

/*** Common query method: SELECT */@ SuppressWarnings ({"unchecked", "rawtypes"}) public List testResultSet (String SQL) {Connection connection = null; statement statement = null; ResultSet rs = null; try {// 1. obtain Connection connection = JDBCTools. getConnection (); // 2. get Statement statement = connection. createStatement (); // 4. run the query to obtain ResultSet rs = statement.exe cuteQuery (SQL); // 5. process ResultSet List list = new ArrayList (); ResultSetMetaData metaData = rs. getMetaData (); int columnCount = metaData. getColumnCount (); while (rs. next () {Map rowData = new HashMap (); for (int I = 1; I <columnCount; I ++) {rowData. put (metaData. getColumnName (I), rs. getObject (I);} list. add (rowData) ;}for (int I = 0; I <list. size (); I ++) {System. out. print (list. get (I); System. out. println ();} return list;} catch (Exception e) {e. printStackTrace (); return null;} finally {// 6. disable the corresponding resources of the database, JDBCTools. release (rs, statement, connection );}}

I printed the result to the console to make it easier to see the result. Actually, this is not allowed.

Too lazy to write Test Methods

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.