package com.xk.demotest.tools;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; public class DaoDBConectTools {
/*
// 传统jdbc连接
private static final String driverName = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/20170626javaweb01";
private static final String user = "root";
private static final String password = "root"; public DaoDBConectTools() {
Connection connect = null;
Statement state = null;
try {
Class.forName(driverName);
connect = DriverManager.getConnection(url, user, password);
state = connect.createStatement();
String sql = "";
state.executeQuery(sql);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
state.close();
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
*/ private static final Properties pro = new Properties(); // ①创建properties对象
//加载配置文件和驱动
static {
InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); // ②引入配置文件
try {
pro.load(iStream); // ②引入配置文件
String driverName = pro.getProperty("driverName"); // ③加载驱动
Class.forName(driverName);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
} //获取连接对像并创建连接
public static Connection connection() {
String url = pro.getProperty("url");
String user = pro.getProperty("userName");
String password = pro.getProperty("password");
Connection connect = null;
try {
connect = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connect;
} //关闭数据库连接
public static void close(Connection connect, Statement state) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connect != null) {
try {
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
package com.xk.demotest.tools;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; public class DaoDBConectTools {
/*
// 传统jdbc连接
private static final String driverName = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/20170626javaweb01";
private static final String user = "root";
private static final String password = "root"; public DaoDBConectTools() {
Connection connect = null;
Statement state = null;
try {
Class.forName(driverName);
connect = DriverManager.getConnection(url, user, password);
state = connect.createStatement();
String sql = "";
state.executeQuery(sql);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
state.close();
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
*/ private static final Properties pro = new Properties(); // ①创建properties对象
//加载配置文件和驱动
static {
InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); // ②引入配置文件
try {
pro.load(iStream); // ②引入配置文件
String driverName = pro.getProperty("driverName"); // ③加载驱动
Class.forName(driverName);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
} //获取连接对像并创建连接
public static Connection connection() {
String url = pro.getProperty("url");
String user = pro.getProperty("userName");
String password = pro.getProperty("password");
Connection connect = null;
try {
connect = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connect;
} //关闭数据库连接
public static void close(Connection connect, Statement state) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connect != null) {
try {
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }

最新文章

  1. Word2010如何恢复没有保存的文件
  2. pygame学习笔记
  3. sqlserver跟踪
  4. android之多媒体篇(三)
  5. 【解决方法】VS 丢失模板
  6. SRM 504.5(2-1000pt)
  7. My SQL 练习题
  8. 【HDU1272】小希的迷宫(并查集基础题)
  9. CII-2.4指针常量和常量指针
  10. hibernate ——关联关系
  11. Flash加载ini文件!
  12. Spring框架学习笔记(3)——配置bean
  13. 总结:BGP和静态路由并存,达到故障自动倒换的目的。
  14. tesseract的编译安装
  15. Chapter 1: Plug-in programing from past to the future
  16. 启动xampp出错,Port 80 in use by "Unable to open process" with PID 4!
  17. 野(wild)指针与悬空(dangling)指针
  18. linux常见运维题
  19. css经验之谈
  20. 【LOJ】#2269. 「SDOI2017」切树游戏

热门文章

  1. RPC服务和HTTP服务对比
  2. 关于 ionic3 tabs 导航ico 点击 页面返回顶部
  3. linux 下tftpf搭建
  4. scrum学习笔记
  5. matlab之中文字体乱码处理
  6. session删除
  7. 一个关于cookie的坑
  8. Linux学习笔记之时间同步the NTP socket is in use, exiting问题
  9. Linux vim文档操作
  10. Linux 查看dns运行状态