本文源碼:GitHub·點這里 || GitEE·點這里
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)服務(wù)商,為中小企業(yè)提供做網(wǎng)站、成都做網(wǎng)站服務(wù),網(wǎng)站設(shè)計,網(wǎng)站托管等一站式綜合服務(wù)型公司,專業(yè)打造企業(yè)形象網(wǎng)站,讓您在眾多競爭對手中脫穎而出創(chuàng)新互聯(lián)。
C3P0是一個開源的JDBC連接池,應(yīng)用程序根據(jù)C3P0配置來初始化數(shù)據(jù)庫連接,可以自動回收空閑連接的功能。
<dependency>
<groupId>MySQL</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
配置文件位置:放在resources
目錄下,這樣C3P0組件會自動加載該配置。
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<!-- 核心參數(shù)配置 -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/servlet-jdbc</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123</property>
<!-- 池參數(shù)配置 -->
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
</c3p0-config>
該工具類用來獲取數(shù)據(jù)庫連接,和釋放相關(guān)連接。
public class C3P0Pool {
private static DataSource dataSource = new ComboPooledDataSource();
public static DataSource getDataSource() {
return dataSource ;
}
/**
* 獲取連接
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
/**
* 釋放連接
*/
public static void close(ResultSet resultSet, PreparedStatement pst, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public class UserJdbcInsert {
public static void insertUser (UserInfo userInfo){
try {
Connection connection = C3P0Pool.getConnection();
String sql = "INSERT INTO user_info (user_name,user_age) VALUES (?,?)" ;
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,userInfo.getUserName());
statement.setString(2,userInfo.getUserAge().toString());
statement.execute() ;
C3P0Pool.close(null, statement, connection);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void batchInsertUser (List<UserInfo> userInfoList){
try {
Connection connection = C3P0Pool.getConnection();
String sql = "INSERT INTO user_info (user_name,user_age) VALUES (?,?)" ;
PreparedStatement statement = connection.prepareStatement(sql);
for (UserInfo userInfo:userInfoList){
statement.setString(1,userInfo.getUserName());
statement.setString(2,userInfo.getUserAge().toString());
statement.addBatch();
}
statement.executeBatch() ;
C3P0Pool.close(null, statement, connection);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class UserJdbcQuery {
public static UserInfo queryUser (String userName){
UserInfo userInfo = null ;
try {
Connection connection = C3P0Pool.getConnection();
String sql = "SELECT * FROM user_info WHERE user_name=?" ;
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,userName);
ResultSet resultSet = statement.executeQuery() ;
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("user_name");
int age = resultSet.getInt("user_age");
System.out.println("ID:"+id+";name:"+name+";age:"+age);
userInfo = new UserInfo(name,age) ;
}
C3P0Pool.close(resultSet, statement, connection);
} catch (Exception e) {
e.printStackTrace();
}
return userInfo ;
}
}
public class UserJdbcUpdate {
public static void updateUser (String name,Integer age,Integer id){
try {
Connection connection = C3P0Pool.getConnection();
String sql = "UPDATE user_info SET user_name=?,user_age=? WHERE id=?" ;
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,age);
statement.setInt(3,id);
statement.executeUpdate() ;
C3P0Pool.close(null, statement, connection);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class UserJdbcDelete {
public static void deleteUser (Integer id){
try {
Connection connection = C3P0Pool.getConnection();
String sql = "DELETE FROM user_info WHERE id=?" ;
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate() ;
C3P0Pool.close(null, statement, connection);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class JdbcServletImpl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("userName") ;
UserInfo userInfo = UserJdbcQuery.queryUser(userName) ;
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("用戶信息:"+userInfo);
}
}
測試訪問:
http://localhost:6003/jdbcServletImpl?userName=LiSi
頁面打印:
用戶信息:UserInfo{userName='LiSi', userAge=22}
GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent
當(dāng)前標(biāo)題:JavaEE基礎(chǔ)(06):Servlet整合C3P0數(shù)據(jù)庫連接池
標(biāo)題URL:http://chinadenli.net/article12/gispdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、域名注冊、企業(yè)建站、外貿(mào)建站、網(wǎng)站導(dǎo)航、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)