单例模式建立工具类
来源:互联网 发布:linux x86 x64区别 编辑:程序博客网 时间:2024/06/03 19:22
/* * *JdbcUtilsSing.java * * 这是一个工具类,一般工具类不能被继承,定义成final * 不需要构造实例,用私有的构造方法 * 单例模式或者用static静态代码块,本例用单例模式 * * */package cn.itcast.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public final class JdbcUtilsSing { private static String url = "jdbc:mysql://localhost:3306/jdbc"; private static String user = "root"; private static String password = "123456"; private static JdbcUtilsSing instance = null; private JdbcUtilsSing() { } public static JdbcUtilsSing getInstance() { if (instance == null) { // 延迟加载 synchronized (JdbcUtilsSing.class) { // 同步锁 if (instance == null) instance = new JdbcUtilsSing(); } } return instance; } // 注册驱动 static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { throw new ExceptionInInitializerError(e); } } public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } public void free(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }}
使用这个类
package cn.itcast.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Base { public static void main(String[] args) { try { template(); } catch (Exception e) { e.printStackTrace(); } } static void template() throws Exception { Connection conn = null; Statement st = null; ResultSet rs = null; try { // 2.建立连接 conn = JdbcUtilsSing.getInstance().getConnection(); // 3.创建语句 st = conn.createStatement(); // 4.执行语句 rs = st.executeQuery("select * from user"); // 5.处理结果 while (rs.next()) { System.out.println("id:" + rs.getObject(1) + "\tname:" + rs.getObject(2) + "\tbirthday:" + rs.getObject(3) + "\tmoney:" + rs.getObject(4)); } // 6.释放资源 } finally { JdbcUtilsSing.getInstance().free(rs,st,conn); } }}
比起static代码的方式稍微麻烦一点
0 0
- 单例模式建立工具类
- jdbc工具类(单例模式)
- java工具类2---单例模式
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 在单例模式和工具类之间如何选择?
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- 使用单例模式实现自己的HttpClient工具类
- RedisUtil工具类,Java单例模式实现
- 【UIKit-110-7】#import <UIKit/UITableView.h>提前注册
- Xcode 6.3.1Mac版 V6.4.Beta3免费下载
- yum提示another app is currently holding the yum lock;waiting for it to exit
- C语言中可变长参数使用
- 韩国美女连连看核心算法
- 单例模式建立工具类
- 新手必须知道的13个Xcode小技巧
- hiho一下 第五十九周
- hdu 5486 Difference of Clustering 2015合肥网络赛 并查集 离散化 悲伤的题
- 新版本Xcode 6的视图调试详解
- Linux阻塞和非阻塞读终端
- 如何改变Xcode字体大小?
- 电脑中常用的快捷键
- Framework of Android: retrofit (2.0)