新建 *.properties属性文件,内容如下:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/dbName

username=root

password=root

public class DbTool {
private static String db_driver=null;
private static String db_url=null;
private static String db_user=null;
private static String db_password=null;
public static Connection getConnection() throws ClassNotFoundException, IOException{
Connection conn=null;
InputStream in=DbTool.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
db_driver=properties.getProperty("driver");
db_url=properties.getProperty("url");
db_user=properties.getProperty("user");
db_password=properties.getProperty("password");
try {
Class.forName(db_driver);
conn=DriverManager.getConnection(db_url,db_user,db_password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated catch block
// TODO Auto-generated catch block
return conn;
}

public static void close(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(PreparedStatement prst){
if(prst!=null){
try {
prst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(PreparedStatement prst,Connection conn){
close(prst);
close(conn);
}
public static void close(ResultSet rs,PreparedStatement prst,Connection conn){

close(rs);
close(prst);
close(conn);

}

}

最新文章

  1. TFS 安装错误
  2. 很强大的HTML+CSS+JS面试题(附带答案)
  3. asp.net 网页中播放 flash 和flv
  4. Greenlets间如何实现互相通信?
  5. git组成结构
  6. git 10.8
  7. oc-19-成员变量修饰符
  8. 模板:多Case输入处理
  9. [selenium webdriver Java]使用自定义条件同步测试
  10. LINQ Enumerable 续
  11. 精致的外观Request
  12. 第二阶段第十次spring会议
  13. Scala环境(集成idea)
  14. USI和USCI的区别
  15. [administrative][archlinux][clonezilla][disk cloning] 一块 windows 10 硬盘的备份
  16. SQLyog中创建数据表及相关查询方法
  17. selenium在操作隐藏元素时会报错,怎么判断元素是隐藏的?
  18. 2018.10.13 bzoj4008: [HNOI2015]亚瑟王(概率dp)
  19. 高效的数据压缩编码方式 Protobuf
  20. 自定义网站的icon和收藏夹图标

热门文章

  1. MySQL常用SQL整理
  2. 查看java中的线程个数名称
  3. codefoeces B. Friends and Presents
  4. Cts框架解析(15)-任务运行完
  5. 云舒网络译:Rancher1.0正式版公布
  6. [RFC] Simplifying kernel configuration for distro issues
  7. Active Directory的LDAP协议与DN(Distinguished Name)详解
  8. tween用户使用指南
  9. Spring中的JDBC操作
  10. webstrom编辑react语法报错解决