新建 *.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);

}

}

JDBCTool的更多相关文章

  1. 比较满意设计的一次作业 JDBC宠物管理

    package com.dao; import java.util.List; import com.entity.Master; public interface MasterDao { publi ...

  2. JavaFX——简单的日记系统

    前言 在学习Swing后,听老师说使用Java写界面还可以使用JavaFX.课后,便去了解.JavaFX是甲骨文公司07年推出的期望应用于桌面开发领域的技术.在了解了这个技术几天后,便使用它完成Jav ...

  3. Java实现的并发任务处理实例

    本文实例讲述了Java实现的并发任务处理方法.分享给大家供大家参考,具体如下: public void init() { super.init(); this.ioThreadPool = new T ...

  4. AlanShan数据库课程设计报告

    目    录 1.绪论.... 2 1.1前言... 2 1.2社会背景... 2 1.3超市背景... 3 2.系统可行性研究.... 4 2.1 技术可行性研究... 4 2.2 经济可行性研究. ...

随机推荐

  1. Mondiran创建连接

    曾经使用jdbc创建连接的时候使用的url是这种形式:jdbc:mysql://hostname:port/database?key1=value1&key2=value2,在URL须要以&q ...

  2. Git以及github的使用方法(一)安装并设置git用户

    最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和Window ...

  3. sql的一些知识_order by

    注释 SELECT name FROM userinfo #这是注释 /* 这是多行注释 */ order by 排序() SELECT username FROM userinfo ORDER BY ...

  4. leetCode(40):Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. Amazon SNS移动推送更新——新增百度云推送和Windows平台支持

    Amazon SNS(Simple Notification Service)是一种基于云平台的消息通知和推送服务. SNS提供简单的 Web 服务接口和基于浏览器的管理控制台让用户可以简易设置.执行 ...

  6. 简单的HTML5音乐播放器(带歌词滚动)

      // // 0) { this.lrcArr.push(item); } } frag = document.createDocumentFragment(); for(i = 0,len = t ...

  7. 多线程(C++)临界区Critical Sections

    一 .Critical Sections(功能与Mutex相同,保证某一时刻只有一个线程能够访问共享资源,但是不是内核对象,所以访问速度比Mutex快,但是没有等待超时的功能,所以有可能导致死锁,使用 ...

  8. Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树

    小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ...

  9. Learning Scrapy 中文版翻译 第二章

    为了从网页中提取信息,你有必要对网页的结构做一些了解.我们将快速学习HMTL,HTML数状结构以及用XPath在网页上提取信息 HTML, DOM树结构以及XPath 让我们花一点时间来了解当用户在浏 ...

  10. Android Button Maker(在线生成android shape xml文件的工具),真方便!

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/scry5566/article/details/25379275        直接上地址:http ...