JDBC工具类封装v1.0

JDBC工具类封装1.0版

 1     private static DataSource dataSource = null;//连接池对象
 2 
 3     static {
 4         //初始化连接池对象
 5         Properties properties = new Properties();
 6         InputStream ips = JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties");
 7         try {
 8             properties.load(ips);
 9         } catch (IOException e) {
10             throw new RuntimeException(e);
11         }
12 
13         try {
14             dataSource = DruidDataSourceFactory.createDataSource(properties);
15         } catch (Exception e) {
16             throw new RuntimeException(e);
17         }
18     }
19 
20     /*
21      * 对外提供连接的方法
22      * @return
23      */
24 
25     public static Connection getConnection(){
26 
27         return null;
28     }
29 
30     //回收连接的方法
31     public static void freeConnection(Connection connection) throws SQLException {
32         connection.close();
33     }

测试代码

 1 package com.atsyc.api.utils;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 
 6 public class JdbcCurdPart {
 7 
 8     public void testInsert() throws SQLException {
 9         Connection connection = JdbcUtils.getConnection();//提供连接
10 
11         //数据库curd动作
12 
13         JdbcUtils.freeConnection(connection);//连接回收
14     }
15 
16 }

 

posted @ 2024-03-06 19:27  白茶花约  阅读(30)  评论(0)    收藏  举报