ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

jdbc 工具包实现,另附赠mysql-connector-java-5.1.46jar包

2022-03-18 20:36:34  阅读:162  来源: 互联网

标签:46jar 5.1 java 附赠 connection SQLException static statement null


package com.sd.utils;

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

/**
 * 
 * @author Administrator
 *  专门用来减少重复代码的
 */
public class DBUtils {
	
	
	private static Properties properties = null;
	
	private static Connection connection = null;
	
	/**
	 * 禁止实例化
	 */
	private DBUtils(){}
	
	
	
	
	static {  
		 try {
			 /**
			  * 在类被加载的时候,读取配置文件中的值
			  * 		   将配置文件中的驱动加载
			  */
			properties = FileUtil.getProperties();
			Class.forName(properties.getProperty("driver"));
			
		} catch (Exception e) { 
			e.printStackTrace();
		} 
	}
	
	
	
	/**
	 * 创建数据库链接
	 * 懒加载
	 * @return
	 * @throws SQLException 
	 */
	public static Connection getConnection() throws SQLException{
		/**
		 * 没有的时候进行船舰
		 * 有的时候则进行直接使用
		 */
		if(connection == null || connection.isClosed()){
			connection = DriverManager.getConnection(properties.getProperty("url"),properties);
		} 
		return connection;
	}
	
	
	/**
	 * 用来关闭数据库信息的
	 * @param statement
	 * @param connection
	 * @param resultSet
	 * @throws SQLException
	 */
	public static void close(Statement statement,Connection connection,ResultSet resultSet) throws SQLException{
		if(statement != null ){
			statement.close();
		}
		if(connection != null ){
			connection.close();
		}
		if(resultSet != null ){
			resultSet.close();
		}
	}
	
	
	
	/**
	 * 用来关闭数据库信息的
	 * @param statement
	 * @param connection
	 * @param resultSet
	 * @throws SQLException
	 */
	public static void close(Statement statement,Connection connection) throws SQLException{
		close(statement,connection,null);
	 
	}
	

}

  

 

 

其他代码传送门:http://www.mababa.xin/2022/03/18/454.html

标签:46jar,5.1,java,附赠,connection,SQLException,static,statement,null
来源: https://www.cnblogs.com/xiaoyu1994/p/16023418.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有