数据库连接池Druid案例

DruidUtils工具类

由于初始化连接池是一个重复的操作,所以可以写一个工具类,加上静态代码块,初始化连接池,以后每次调用工具类即可实现对连接池的初始化和关闭资源等操作
public class DruidUtils {
/*工具类 */

private static DataSource ds;


static {
    Properties pro = new Properties();
    try {
        pro.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));//需要在DruidUtils的src下创建properties的配置文件
        ds = DruidDataSourceFactory.createDataSource(pro);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static Connection getConnection() throws SQLException {
    return ds.getConnection();


}

public static void close(Statement stmt, Connection conn) {
    if (stmt != null) {
        try {
            stmt.close();
        } 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 throwables) {
            throwables.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;
}

}

配置文件druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db1?characterEncoding=utf8&useSSL=true
username=root
password=123
initialSize=5
maxActive=10
maxWait=3000

DruidDemo

    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt2 = null;
    Connection conn = null;
    /*完成添加操作*/
    try {
        conn = DruidUtils.getConnection();
        String sql1 = "select * from jpj";
        String sql2 = "insert into jpj(city,area,airport) values(?,?,?)";
        /*获取pstmt对象*/
        pstmt1 = conn.prepareStatement(sql2);
        pstmt1.setString(1, "tokyo");
        pstmt1.setString(2, "tokyo area");
        pstmt1.setString(3, "Narita International Airport");
        int count = pstmt1.executeUpdate();
        System.out.println(count);

        pstmt2 = conn.prepareStatement(sql1);
        ResultSet rs = pstmt2.executeQuery();
        while(rs.next()){
            String city = rs.getString(1);
            String area = rs.getString(2);
            String airport = rs.getString(3);
            String timestamp = rs.getString(4);
            System.out.println(city+"---"+area+"---"+airport+"---"+timestamp);

        }


    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        DruidUtils.close( pstmt1, conn);
        DruidUtils.close( pstmt2, conn);

    }

这里向表里面添加数据,需要注意的是Statement有两个,一个是java.sql下的,一个是java.mysql.jdbc下的,需要的是第一个不要导错包。

根据表里面的数据进行添加后得到的结果

原文地址:https://www.cnblogs.com/ruonan1997/p/14069043.html