In Java, map resultset to objects and queues and other helper functions

Source: Internet
Author: User

About Object Relational Mapping (ORM) is the most used in database access, in Java, many libraries are trying to map a resultset to a custom Java Bean object or queue, and here's my implementation

1 reading data from resultset

The first is how to correctly read the data you want from the resultset, including the case of known column numbers and known column names.

The column ordinal is known, reading the data value in the ResultSet:

public static <T> T ReadValue (class<t> T, ResultSet set, int columnindex) throws SQLException;

Known column name, read data value in resultset:

public static <T> T ReadValue (class<t> T, ResultSet set, map<string, integer> columns, String field)

Columns stores this column index in relation to the column name, and if the argument is empty, create a queue yourself.

Here is the implementation

/** * Gets the value in resultset * @param t * @param set * @param columnindex * @return * @throws SQLException */@SuppressWarnings ("UN Checked ") public static <T> T ReadValue (class<t> T, ResultSet set, int columnindex) throws SQLException {if (t = = NULL | | set = = null) {return null;} if (t = = Object.class) {return (T) set.getobject (columnindex);} if (t = = Integer.class) {Object val = set.getint (columnindex); return (T) Val;} if (t = = Short.class) {Object val = set.getshort (columnindex); return (T) Val;} if (t = = Boolean.class) {Object val = Set.getboolean (columnindex); return (T) Val;} if (t = = Long.class) {Object val = Set.getlong (columnindex); return (T) Val;} if (t = = Float.class) {Object val = set.getfloat (columnindex); return (T) Val;} if (t = = Double.class) {Object val = set.getdouble (columnindex); return (T) Val;} if (t = = String.class) {Object val = set.getstring (columnindex); return (T) Val;} if (t = = Java.sql.Date.class) {Object val = set.getdate (columnindex); return (T) Val;} if (t = = Java.sql.Time.class) {Object val = set.gettime (columnindex); return (T) Val;} if (t = = Java.sql.Timestamp.class) {Object val = set.gettimestamp (columnindex); return (T) Val;} if (t = = Byte.class) {Object val = set.getbyte (columnindex); return (T) Val;} Return (T) set.getobject (columnindex);}

  

/** * Get the value in resultset * @param t * @param set * @param columns * @param field * @return * @throws SQLException */public STA Tic <T> t ReadValue (class<t> T, ResultSet set, map<string, integer> columns, String field) throws Sqlexc eption {if (columns = = null) {columns = Getfieldindex (set);} if (columns = = NULL) {return null;} if (!columns.containskey (field)) {return null;} int index = columns.get (field), Return ReadValue (T, set, index);}

  

/** * Gets the column index corresponding to the column name * @param set * @return * @throws SQLException */public static map<string, integer> Getfieldindex (Re Sultset set) throws SQLException {if (set = = null) {return null;} ResultSetMetaData meta = set.getmetadata (); if (meta = = null) {return null;} map<string, integer> map = new hashmap<string, integer> (); int count = Meta.getcolumncount (); for (int i = 0; i < count; i++) {map.put (Meta.getcolumnname (i), i);} return map;}

In fact, Getfieldindex can still be extended, such as the type that contains the data, temporarily no need, and does not extend.

2 mapping of data to objects

Here is the data-to-object mapping

Interface:

/** * Load Object according to ResultSet * @param cls * @param t * @param set * @param columns * @return * @throws SQLException */public Stat IC <T> boolean fillobject (class<t> cls, T T, ResultSet Set, map<string, integer> columns) throws Sqlexce Ption

Realize:

/** * Load Object according to ResultSet * @param cls * @param t * @param set * @param columns * @return * @throws SQLException */public Stat IC <T> boolean fillobject (class<t> cls, T T, ResultSet Set, map<string, integer> columns) throws Sqlexce ption {if (CLS = = NULL | | t = = NULL | | set = NULL) {return false;} if (columns = = null) {columns = Getfieldindex (set);} Final map<string, integer> cols = Columns;return Hicbo.fillobjectex (t, CLS, new Ieventret8param<object, String&  gt; () {@Overridepublic final Object OnEvent (String v) {try {return ReadValue (Object.class, set, cols, V);} catch (Exception Ex) {ex.printstacktrace (); return null;}});}

  

/** * Create and load Object data * @param CLS * @param set * @return */public static <T> T CreateObject (class<t> CLS, ResultSet Set, map<string, integer> columns) {try{t T = cls.newinstance (); Fillobject (CLS, T, set, columns); return t;} catch (Exception ex) {ex.printstacktrace (); return null;}}

Note: In CreateObject, if T does not have a default constructor, there is a problem and there is no error during compile time, which needs to be noted.

Get queue:

/** * Get the list of Set * @param CLS * @param set * @return * @throws SQLException */public static <T> list<t> Getresul Tslist (class<t> cls, ResultSet set) throws SQLException {if (CLS = = NULL | | set = NULL) {return null;} map<string, integer> columns = Getfieldindex (set), if (columns = = NULL) {return null;} if (!set.first ()) {return null;} list<t> list = new arraylist<t> (); T t = GetFirst (CLS, set, columns); if (t! = null) {List.add (t);} while (Set.next ()) {T it = CreateObject (CLS, set); if (it = null) {List.add (it);}} return list;}

  

In Java, map resultset to objects and queues and other helper functions

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.