• JDBCUtils 工具类


    import com.alibaba.druid.pool.DruidDataSourceFactory;

    import javax.sql.DataSource;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;

    public class JDBCUtils{

      private static DataSource ds;//定义datasource类型的对象

      static{//加载文件,创建连接池

        try{

          Properties pro = new Properties();

          pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("xxxx.properties"));

    //JDBCUtils.class.getClassLoader()---此为得到文件地址:c:/xx/

    //getResourceAsStream("xx.properties")---此为获得具体文件内容

          ds = DruidDataSourceFactory.createDataSource(pro);

          //得到具体的连接池ds

        }catch(Exception e){

          e.printStackTrace();

        }

      }

      //获得链接

      public static Connection getConnection() throws SQLException{

        return ds.getConnection();

      }

      //释放资源

      public static void close(Statement stat,Connection conn){

        if(stat!=null){

          try{

          }catch(SQLException e){

            e.printStackTrace();

          }

     

        }

    if(conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

      

      }

      

    public static void close(ResultSet rs,Statement stmt, Connection conn){
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(stmt!=null){
    try {
    stmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

      public static DataSource getDataSource(){

        return ds;//返回连接池

      }

    }

  • 相关阅读:
    Node.js学习(二)----- 常用模块
    Node.js学习(一)----- 基础知识
    微信小程序开发(三)----- 云开发案例
    微信小程序开发(二)----- 云开发
    微信小程序开发(一)----- 基础知识
    简述Vue中使用Vuex
    简述前后端项目RSA+AES加解密
    简述Js中,判断对象为空对象的几种方式
    简述在Js或Vue中监听页面的刷新、关闭操作
    简述Object(ActiveX)控件遮挡Dialog、select下拉框的解决办法
  • 原文地址:https://www.cnblogs.com/fanqiexin/p/10651191.html
Copyright © 2020-2023  润新知