Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to briefly describe Spring JDBC

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to briefly describe Spring JDBC. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Without saying much, let's take a look at it.

Here is a record of my study of the Spring JDBC framework. Because Spring JDBC and my previous work have a lot in common, learn from the classic Framework design, learn from each other, and use it for me. Here, first admire Rod JohnSon, he has a very deep understanding of the database, JDBC. Looking at the design and source code of the Spring jdbc framework brings me a lot of things I didn't think of before. We know that the main goal of Spring JDBC is to simplify JDBC programming and make it easier for us to build robust applications. One of its basic design concepts here is to separate the changed from the immutable in JDBC programming. What is changing in JDBC? There is no doubt that SQL statements change. So what doesn't change? The correct way to use JDBC remains the same.

Look at a piece of code first. (instead of using your own code as a demonstration, take a look at the Rod book first.)

Public List getAvailableSeatlds (DataSource ds, int performanceld

Int seatType) throws ApplicationException {

String sql = "SELECT seat_id AS id FROM available_seats" +

"WHERE performance_id =? AND price_band_id =?"; List seatlds = new LinkedList ()

Connection con = null

PreparedStatement ps = null

ResultSet rs = null

Try {

Con = ds.getConnection () / / 1. Establish Connection

Ps = con.prepareStatement (sql); / / 2. Create preparedStatement

Ps.setlnt (1, performanceld); / / 3. Set the parameters of ps

Ps.setlnt (2, seatType)

Rs = ps.executeQuery () / / 4. Execute query

While (rs.next ()) {/ / 5. Parsing ResultSet

Int seatld = rs.getlnt (1)

Seatlds.add (new Integer (seatld))

}

Rs.close (); / / 6. Close the resources and do a good job in dealing with the aftermath. Rs,ps,connection

Ps.close ()

}

Catch (SQLException ex) {

Throw new ApplicationException ("Couldn't run query [" + sql + "]", ex)

}

Finally {

Try {

If (con! = null)

Con.close (); / / if there is no connection pool, do not turn it off easily. Connection is a resource consuming:)

}

Catch (SQLException ex) {

/ / Log and ignore

}

}

Return seatlds

}

From above, what remains the same. First of all, our way of using JDBC is good, correct, and unchanged, that is, workflow is unchanged. Second, many of the operations are unchanged, such as closing resources and handling exceptions.

◆, what has changed? Setting the parameters of PreparedStament is variable, using PreparedStatement to do what is changed.

◆, what else has changed? Getting Connection can be varied, either from ConnectionPool or naked from Database.

◆, what else has changed? In addition to the main workflow, you can also set some properties on PreparedStament. Such as fetchSize and so on.

◆, what else has changed? Parsing ResultSet is changeable. But it can be abstract, all getting what you want from the result set.

Fine. After analysis, we will naturally think of the Template design pattern. Use the template method to describe our workflow. For fixed operations, we model it as helper classes that are used to complete fixed operations, which are called in the Template method.

For which methods can be changed. We also found that, in fact, it is intended to achieve the same function. Abstractly, we can use some interfaces to describe these functions. Such as the function of database connection management.

Design depends on the depth of the problem we consider and the granularity of our division of the process.

The above is how to briefly describe Spring JDBC, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report