Java determines whether a specified column name exists in the database result set resultset

Source: Internet
Author: User


Recently the most recent project, the persistence part of the project is spring JDBC, the query out of the result set to map to an object, each time in the method of querying SQL to define a private mapper variable, the internal implementation of the Maprow method, it seems a waste to define a common maprow implementation, Save code for easy maintenance.


In the process of writing, found a problem, is related to a very large object, the property is very many, but each query out of the properties are not the same, such a public maprow how to write, can be based on the result set whether there is a property, so that its mapping, no longer map it?


Open the JDK API immediately, find resultset, find and find, did not find the corresponding method.

But there is a way in the JDK that can be leveraged, what method?

Findcolumnint Findcolumn (String ColumnLabel)               throws SqlException maps the given ResultSet column label to its ResultSet column index. Parameter:    ColumnLabel-the column label specified using the SQL as clause. If the SQL as clause is not specified, the label is returned by the column name:    the column index of the given column name is thrown:     SQLException-If the ResultSet object does not contain a column marked as ColumnLabel, a database access error occurred or on a closed result set Call this method

The Findcolumn method in ResultSet returns the index of the specified column name in the result set, starting with the 1 index value.

If the returned result set contains id,name,age in turn, the

Findcolumn ("id")//1findColumn ("name")//2findColumn ("age")//3

What happens if I go through findcolumn to find a nonexistent column name? Throws SqlException.

The approach described here is to combine findcolumn and SQLException to form a method.

/** * Determine if a column exists in the query result set * @param rs Query result set * @param columnName column name * @return true exists; False */public Boolean Isexistcolumn (ResultSet rs, String columnName) {try {if (Rs.findcolumn (ColumnName) > 0) {RE Turn true;} }catch (SQLException e) {return false;} return false;}


if condition, if rs.findcolumn (columnName) > 0 , the column index in the resultset result set starts at 1, and differs from 0 for arrays and lists, so if a column is found, Then its index value must be greater than 0, if not found, then there will be SqlException exception thrown, we will use this SqlException exception, if you enter the exception block, the column is not found, then directly return false.


So in the Maprow, if a number of properties, each can be judged, whether the ResultSet query out of the column, query out to do mapping, no query out, then do not do specific things, thus better common maprow.


Java determines whether a specified column name exists in the database result set resultset

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.