首页>代码>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();
				}
			}
		}
	}

}
最近下载更多
asddwh  LV12 2023年12月29日
去码头整点薯条  LV3 2023年11月15日
ds9009  LV7 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  LV19 2023年6月13日
微信网友_6470947892727808  LV1 2023年5月11日
kxjh星辰  LV6 2022年12月28日
最近浏览更多
一起加油  LV3 12小时前
 LV7 3月29日
tkggddm  LV3 1月27日
xiongwei11231  LV8 1月15日
Kaiaahh  LV1 1月5日
2131234536546  LV7 1月4日
susitian  LV2 2023年12月28日
asddwh  LV12 2023年12月25日
ddzfgh  LV1 2023年12月25日
颜菜菜  LV2 2023年12月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友