当前位置:Gxlcms > 数据库问题 > JDBC工具类

JDBC工具类

时间:2021-07-01 10:21:17 帮助过:15人阅读

package com.mycompany.util;

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

/**
 * JDBC工具类
 * @author Administrator
 *
 */
public class JDBCUtil {
	
	/**
	 * 获取Connection
	 * @return 返回JDBC的Connection对象
	 * @throws ClassNotFoundException 
	 */
	public static Connection getConnection() throws Exception{
		//加载属性文件
		InputStream inputStream = JDBCUtil.class.getClassLoader()
		.getResourceAsStream("db.properties");
		Properties properties = new Properties();
		properties.load(inputStream);
		
		String url = properties.getProperty("jdbc.url");
		String user = properties.getProperty("jdbc.user");
		String password = properties.getProperty("jdbc.password");
		String driverClass = properties.getProperty("jdbc.driverClass");
		
		Class.forName(driverClass);
		Connection connection = DriverManager.getConnection(url, user, password);
		
		
		return connection;
	}
	
	/**
	 * 释放资讯
	 * @param resultSet
	 * @param statement
	 * @param connection
	 */
	public static void release(ResultSet resultSet,Statement statement,Connection connection){
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}


本文出自 “素颜” 博客,谢绝转载!

JDBC工具类

标签:jdbc工具类

人气教程排行