Hibernate4 obtains the Connection and ResultSet object.

Source: Internet
Author: User

A json object is required in the project, and the column name of the data is required during encapsulation. In jdbc, you can have a ResultMetaData object to get the column name. Because I use hibernate, this framework has already encapsulated a lot, and it is generally difficult to obtain the resultset. After unremitting bing and google (as an environmentally friendly quasi-programmer, Baidu is rejected), we found that in hibernate, we can obtain the resultset object. However, it is now hibernate4, which is relatively new, and the acquisition method has changed a lot. In the previous hibernate, you can use the following code to obtain the connection and other objects. Copy the java code. SQL. connection c = null; java. SQL. preparedStatement ps = null; java. SQL. resultSet rs = null; public List method (String SQL) {List ret = new ArrayList (); try {c = SessionFactoryUtils. getDataSource (getSessionFactory ()). getConnection (); ps = c. prepareStatement (SQL); rs = ps.exe cuteQuery (); while (rs. next ()){.....} ret. add (ro) ;}} catch (Exception e) {e. printStackTrace ();} finally {cl Ose ();} return ret;} copies the code so that objects such as resultset can be used like jdbc. However, in hibernate4, the method has been changed. The SessionFactoryUtils. getDataSource (getSessionFactory (). getConnection () Statement is no longer available, but the new method is used. GetSession (). doWork (new Work () {@ Override public void execute (Connection connection) {}}); you can directly use connection in the method body. In this case, a void is returned, and what I want to use is to obtain the resultset object. Fortunately, IDE has a smart prompt and finds that there is a method that can pass the returned value, which is an extension of the above method. Directly copy the code in my project: copy the code @ Test public void tests () throws SQLException {Session session Session = HibernateSessionFactory. getSession (); ResultSet resultSet = session. doReturningWork (new ReturningWork <ResultSet> () {@ Override public ResultSet execute (Connection connection) throws SQLException {String SQL = "select * from t_auth"; PreparedStatement preparedStatement = connection. prepareStatement (SQL); ResultSet resultSe Tsung preparedstatement.exe cuteQuery (); return resultSet ;}}); while (resultSet. next () {System. out. println ("rs:" + resultSet. getString ("authid");} the copy code is the doReturnWork method, which uses an internal class to return the resultset object to the doReturnWork layer by layer, in this way, you can use hibernate like jdbc. I have some personal experiences and hope to help you.

Related Article

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.