首页>代码>JSP+Servlet+MySQL数据库增删改查>/SingleJSP/src/com/test/dao/JDBCUtilSingle.java
package com.test.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *  JDBCUtilSingle:JDBC helper class
 */
public final class JDBCUtilSingle {
	static String driver = "com.mysql.jdbc.Driver";
	static String url = "jdbc:mysql://localhost:3306/website?useUnicode=true&charaterEncoding=utf-8";
	static String username = "root";
	static String password = "root";
	static Connection conn = null;
	private static JDBCUtilSingle instance = null;
	
	private JDBCUtilSingle() {
	}
	
	/**
	 * get JDBCUtilSingle instance
	 * @return
	 */
	public static JDBCUtilSingle getInstance(){
		if(instance == null){
			synchronized(JDBCUtilSingle.class){
				if(instance == null){
					instance = new JDBCUtilSingle();
				}
			}
		}
		return instance;
	}

	/**
	 * static code block register database driven to ensure registration only one 
	 */
	static{
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * get database Connection
	 * @return Connection
	 */
	public Connection getConnection(){
		try {
			conn = DriverManager.getConnection(url,username,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * close connection resource
	 * @param rs
	 * @param st
	 * @param conn
	 */
	public void closeConnection(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(Exception e){
				e.printStackTrace();
			}finally{
				try{
					if(conn != null){
						conn.close();
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}

}
最近下载更多
哪里的完整版  LV8 2024年9月3日
微信网友_7032639073292288  LV1 2024年6月11日
asddwh  LV13 2023年12月29日
去码头整点薯条  LV3 2023年11月15日
ds9009  LV8 2023年8月7日
CH小老虎  LV1 2023年7月4日
sfy_1802661689  LV2 2023年7月3日
qwqwqw12345  LV3 2023年6月20日
ericxu1116  LV24 2023年6月14日
泓鼎168  LV20 2023年6月13日
最近浏览更多
krispeng  LV14 2024年12月31日
124523 2024年12月11日
暂无贡献等级
哪里的完整版  LV8 2024年9月3日
暂无贡献等级
ma406805131  LV19 2024年6月18日
微信网友_7032639073292288  LV1 2024年6月11日
一起加油  LV5 2024年4月19日
 LV7 2024年3月29日
tkggddm  LV3 2024年1月27日
xiongwei11231  LV9 2024年1月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友