java mysql数据库链接与资源关闭
/**
* Created by Clear on 2018/8/11.
* here provide the kind of connections from mysql database,and close the resources of the mysql
* there are
* load driver
* use properties file
* use xml file
* use tomcat
* and... so on
*
*
*
*/
public class MysqlUtil {
/**
* 链接数据库
*/
/**
* 方法一:
* 加载驱动的方法不止一种,但这种最常用
*/
public static Connection getConnectionOne(String database,String username,String password){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+database,username,
password);
return connection;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 方法二:
* 利用properties文件
* ::::: 在Web 编程时 文件难以定位
*/
public static Connection getConnectionTwo() {
/**
* 建立文件
*/
Properties pro = new Properties(); InputStream in = MysqlUtil.class.getClassLoader().getResourceAsStream("mysqllog.properties");
try {
pro.load(in);
Class.forName(pro.getProperty("driver"));
String username = pro.getProperty("user");
String password = pro.getProperty("password");
String database = pro.getProperty("database");
String url = pro.getProperty("url"); Connection connection = DriverManager.getConnection(url+database,username,password);
return connection;
} catch (Exception e) {
e.printStackTrace();
} return null;
}
// Connection ,Statement, ResultSet 这几个资源的关闭是有顺序的
public static void close (Object...objects) throws MysqlCloseException {
Map<String,Object> map = new HashMap();
for(Object o : objects){
if(o instanceof ResultSet){
map.put("ResultSet",o);
}else if(o instanceof Connection){
map.put("Connection",o);
}else if(o instanceof Statement){
map.put("Statement",o);
}else if(o instanceof PreparedStatement){
map.put("PreparedStatement",o);
}else{
throw new MysqlCloseException("关闭异常,不能处理");
}
}
Object obj = map.get("ResultSet");
if(obj!=null){
ResultSet r = (ResultSet)obj;
try {
r.close();
map.remove("ResultSet");
} catch (SQLException e) {
e.printStackTrace();
}
}
obj = map.get("PreparedStatement");
if(obj!=null){
PreparedStatement p = (PreparedStatement)obj;
try {
p.close();
map.remove("PreparedStatement");
} catch (SQLException e) {
e.printStackTrace();
}
}
obj = map.get("Statement");
if(obj!=null){
Statement s = (Statement)obj;
try {
s.close();
map.remove("Statement");
} catch (SQLException e) {
e.printStackTrace();
}
}
obj = map.get("Connection");
if(obj!=null){
Connection c = (Connection)obj;
try{
c.close();
map.remove("Connection");
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
java mysql数据库链接与资源关闭的更多相关文章
- Java JDBC 数据库链接小结随笔
Java JDBC 数据库链接小结随笔 一.链接数据库的步骤 二.关于Statement 和 PrepareStatement 两者区别 用法 三.关于 ResultSet 的一些小结 四.自定义 ...
- Mysql数据库的打开和关闭
Mysql数据库的打开和关闭: 选择计算机(win7)-右键管理 在新窗口选择--服务 5 找到mysql,然后右键-启动(停止)
- Java JDBC数据库链接
好久没有编写有关数据库应用程序啦,这里回顾一下java JDBC. 1.使用Java JDBC操作数据库一般需要6步: (1)建立JDBC桥接器,加载数据库驱动: (2)连接数据库,获得Connect ...
- Windows下MySQL数据库的安装与关闭开机自启动
我在学习java,安装数据库时找了很多教程,现在在这里总结一下我安装数据库的过程,我安装的是mysql-5.6.42-winx64版本的. 数据官方下载地址:https://dev.mysql.com ...
- SpringBoot-(8)-配置MySQL数据库链接,配置数据坚挺拦截,创建默认数据表
一,链接mysql数据库 # 数据源基本配置 spring.datasource.username=root spring.datasource.password=123456 spring.data ...
- django用MySQL数据库链接
在使用的过程中出现了没有mysqld.sock这个文件的情况,无法连接到mysql数据库. 几经周折,设置路径,改文件夹的权限,也都无济于事,只有重新安装mysql服务器,第一次尝试还是失败,服务器安 ...
- java mysql 数据库
1. jdbc 驱动名还是数据库 String driver = "com.mysql.jdbc.Driver"; //URL指向要访问的数据库名mydataString url ...
- Java之MySql数据库链接
一 下载MySql驱动包,下载途径很多,随便Google或度娘一下就有,我下载的是mysql-connector-java-5.1.26版本,下载后把它解压到指定路径 二 在Eclipse中新建项目T ...
- mysql数据库链接与创建
有童鞋问到说,环境搭建好了,mysql也安装了,但是就是进不去数据库,也启动不了,一直报错,那么下面这边就说下如何用Navicat链接上创建的数据库 首先 1)在xshell里进入mysql,命令是: ...
随机推荐
- 自制操作系统Antz(3)——进入保护模式 (中) 直接操作显存
Antz系统更新地址: https://www.cnblogs.com/LexMoon/category/1262287.html Linux内核源码分析地址:https://www.cnblogs. ...
- Python爬虫(一)——开封市58同城租房信息
代码: # coding=utf-8 import sys import csv import requests from bs4 import BeautifulSoup reload(sys) s ...
- JS设计模式(13)状态模式
什么是状态模式? 定义:将事物内部的每个状态分别封装成类,内部状态改变会产生不同行为. 主要解决:对象的行为依赖于它的状态(属性),并且可以根据它的状态改变而改变它的相关行为. 何时使用:代码中包含大 ...
- 【问题解决:SFL4J】启动时SLF4J报错
问题描述 启动时报错 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Default ...
- react中使用antd遇到的问题
1.less使用报错 less配置修改一般都是1个修改1个增加 test: /\.(css|less)$/, // 修改 // 增加 { loader: require.resolve('less-l ...
- 1_bytes和str
数据运算全跳过,语言都一样,格式差异 bytes/str的区别 Python3不会以任意隐式的方式混用bytes和str,不能拼接字符串和字节包也无法在字节包里搜索字符串(反之亦然) 二进 ...
- js 数组原型
Array.isArray( Array.prototype ) // A. true // B. false // C. error // D. other 答案是A. 其实 Array.proto ...
- rabbitMQ 的三种Exchange
rabbitMQ 的Exchange有3种路由方式: direct.fanout.topic ,以下为详细说明 1. Direct Exchange 处理路由键.需要将一个队列绑定到交换机上,要求 ...
- 聊聊Flume和Logstash的那些事儿
在某个Logstash的场景下,我产生了为什么不能用Flume代替Logstash的疑问,因此查阅了不少材料在这里总结,大部分都是前人的工作经验下,加了一些我自己的思考在里面,希望对大家有帮助. 本文 ...
- blob对象的应用
demo:https://pan.baidu.com/s/1hsq2vgK 最近在学习blob,利用blob编写了两个业务场景,详情请下载demo查看 1:大文件分片下载,服务器端使用.net接收客户 ...