失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 使用Druid连接池连接和关闭数据库

使用Druid连接池连接和关闭数据库

时间:2021-05-25 14:32:45

相关推荐

使用Druid连接池连接和关闭数据库

使用Druid连接池连接数据库,首先先写配置资源,同时需要导druid-1.1.10.jar包

以及数据库驱动mysql-connector-java-8.0.15.jar包

driverClassName=com.mysql.cj.jdbc.Driverurl=jdbc:mysql://localhost:3306/表名称?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&rewriteBatchedStatements=trueusername=数据库用户名password=数据库密码initialSize=10maxActive=10

然后读取配置资源 ,加载驱动

public class DruidConn {private static DataSource dataSource;static {try {Properties pros = new Properties();//读取文件配置资源InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("Druild.properties");//加载驱动pros.load(resourceAsStream);dataSource = DruidDataSourceFactory.createDataSource(pros);} catch (Exception e) {e.printStackTrace();}}//使用druid连接数据库public static Connection getConnection3() {Connection connection = null;try {connection = dataSource.getConnection();return connection;} catch (SQLException throwables) {throwables.printStackTrace();}return null;}}

最后关闭数据库连接池

public static void close(Connection connection, PreparedStatement preparedStatement){DbUtils.closeQuietly(connection);DbUtils.closeQuietly(preparedStatement);}

如果觉得《使用Druid连接池连接和关闭数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。