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();
}
}
}
}

最新文章

  1. 流程控制和循环.png
  2. rabbitMQ+yii2 使用
  3. (转载)Javascript定义类(class)的三种方法
  4. 好玩的Prim算法
  5. 配置无线网络的时候会提示“Enter Password for Default Keyring to Unlock”
  6. 查询数据库中表或视图或存储过程的数量 sql 语句
  7. Linux命令-tar
  8. android数据库操作之直接读取db文件
  9. java基础-基础类型包装类型
  10. Clean Code – Chapter 4: Comments
  11. 关于volatile
  12. python基础:映射和集合类型
  13. 一个在mac上编译c++程序的低级失误
  14. 关于EF 通用增删改查的封装
  15. 有两个序列A和B,A=(a1,a2,...,ak),B=(b1,b2,...,bk),A和B都按升序排列。对于1&lt;=i,j&lt;=k,求k个最小的(ai+bj)。要求算法尽量高效。
  16. Oracle客户端、服务的安装及干净卸载Oracle
  17. asp.net core 系列 9 环境(Development、Staging 、Production)
  18. springboot 出现 Connection refused: connect
  19. oracle操作字符串:拼接、替换、截取、查找、长度、判断
  20. MDI容器

热门文章

  1. MyBatis动态插入的实现
  2. JPA第三天
  3. Django学习路36_函数参数 反向解析 修改404 页面
  4. 汇编语言从键盘输入一个字符串(串长不大于80)以十进制输出字符串中非字母字符的个数(不是a to z或 A to Z)
  5. PHP mysqli_rollback() 函数
  6. MySQL(版本8.0.19)服务的启动/停止、登录/登出、修改密码
  7. 3月28日考试 题解(二分答案+树形DP+数学(高精))
  8. KMP算法-从入门到进阶
  9. Android 的Glide、TabLayout、RecyclerView(下一章补充)。
  10. boost之signal的使用