扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本文源码:GitHub·点这里 || GitEE·点这里
创新互联网站建设服务商,为中小企业提供做网站、成都做网站服务,网站设计,网站托管等一站式综合服务型公司,专业打造企业形象网站,让您在众多竞争对手中脱颖而出创新互联。
C3P0是一个开源的JDBC连接池,应用程序根据C3P0配置来初始化数据库连接,可以自动回收空闲连接的功能。
MySQL
mysql-connector-java
${mysql.version}
com.mchange
c3p0
${c3p0.version}
配置文件位置:放在resources
目录下,这样C3P0组件会自动加载该配置。
jdbc:mysql://localhost:3306/servlet-jdbc
com.mysql.jdbc.Driver
root
123
3
10
2
10
该工具类用来获取数据库连接,和释放相关连接。
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 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
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流