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 use Spring JdbcTemplate

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use Spring JdbcTemplate for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

For the record, my spring package is version 1.2.6.

Spring has many good open source data sources, connection pooling. Let's study together.

(1) DBCP connection pool

DBCP connection pooling is an open source connection pool implementation under the Apache Software Fund.

Need: implementation of commons-dbcp-1.2.1.jar / / connection pool

Dependent library for commons-pool.jar / / connection pool implementation

Dependent library for commons-collection.jar / / connection pool implementation

Code:

Package wyd.spring.datasource.dbcp

Import java.sql.Connection

Import java.sql.SQLException

Import javax.sql.DataSource

Import org.apache.commons.dbcp.BasicDataSource

Public class DBCPDataSource {

Private static BasicDataSource dataSource=null

Private static final String driver= "com.mysql.jdbc.Driver"

Private static final String url= "jdbc:mysql://localhost:3306/wyd"

Private static final String userName= "root"

Private static final String password= "root"

Public static DataSource getDataSource () {

If (dataSource==null) {

DataSource=new BasicDataSource ()

DataSource.setDriverClassName (driver)

DataSource.setUrl (url)

DataSource.setUsername (userName)

DataSource.setPassword (password)

}

Return dataSource

}

Public static Connection getConnection () throws SQLException {

Return DBCPDataSource.getDataSource () .getConnection ()

}

}

(2) C3PO connection pool

C3PO connection pooling is an excellent connection pool and is recommended. C3PO implements some of the functions of the JDBC3.0 specification, so the performance is more outstanding.

Required jar package: c3po0.902.jar

Code:

Package wyd.spring.datasource.c3po

Import java.beans.PropertyVetoException

Import java.sql.Connection

Import java.sql.SQLException

Import javax.sql.DataSource

Import wyd.spring.datasource.dbcp.DBCPDataSource

Import com.mchange.v2.c3p0.ComboPooledDataSource

Public class C3PODataSource {

Private static ComboPooledDataSource dataSource=null

Private static final String driver= "com.mysql.jdbc.Driver"

Private static final String url= "jdbc:mysql://localhost:3306/wyd"

Private static final String userName= "root"

Private static final String password= "root"

Public static DataSource getDataSource () {

If (dataSource==null) {

DataSource=new ComboPooledDataSource ()

Try {

DataSource.setDriverClass (driver)

} catch (PropertyVetoException e) {

System.out.println ("DataSource Load Driver exception!")

E.printStackTrace ()

}

DataSource.setJdbcUrl (url)

DataSource.setUser (userName)

DataSource.setPassword (password)

/ / set the maximum connection capacity of the connection pool

DataSource.setMaxPoolSize (20)

/ / set the minimum connection capacity of the connection pool

DataSource.setMinPoolSize (2)

/ / set the maximum statements object capacity for connection pooling

DataSource.setMaxStatements (100)

}

Return dataSource

}

Public static Connection getConnection () throws SQLException {

Return DBCPDataSource.getDataSource () .getConnection ()

}

}

(3) SmartDataSource connection pool

SmartDataSource inherits the DataSource interface. In addition to providing the basic methods of DataSource, let also provide an additional function to close the connection at the appropriate time. That is, connections obtained through SmartDataSource do not need to be closed.

SmartDataSource is very useful for improvement when a connection needs to be constantly reused in a program. SmartDataSource determines whether the database needs to be shut down, as shown in the following methods.

Boolean shouldClose (Connection con)

This method is implemented by the implementation class of smartDataSource. When the method returns true, the SmartDataSource real example closes the connection when the method returns true.

One implementation class of the SmartDataSource interface is the SingleConnectionDataSource class

It is a single-connected DataSource, which can be thought of as MaxPoolSize=1,MinPoolSize=1

Jar package required: spring-jdbc.jar

Spring-beans.jar

Spring-dao.jar

Spring-core.jar

Commons-logging.jar

Code:

Package wyd.spring.datasource.smart

Import java.sql.Connection

Import java.sql.SQLException

Import org.springframework.jdbc.datasource.SingleConnectionDataSource

Import org.springframework.jdbc.datasource.SmartDataSource

Public class SCDataSource {

Private static SingleConnectionDataSource datasource=null

Private static final String driver= "com.mysql.jdbc.Driver"

Private static final String url= "jdbc:mysql://localhost:3306/ws"

Private static final String userName= "root"

Private static final String password= "root"

Public static SmartDataSource getDataSource () {

If (datasource==null) {

Datasource=new SingleConnectionDataSource ()

Datasource.setDriverClassName (driver)

Datasource.setUrl (url)

Datasource.setUsername (userName)

Datasource.setPassword (password)

Datasource.setSuppressClose (false)

}

Return datasource

}

Public static Connection getConnection () throws SQLException {

Return SCDataSource.getDataSource () .getConnection ()

}

}

This is the end of the article on "how to use Spring JdbcTemplate". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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