码迷,mamicode.com
首页 > 数据库 > 详细

JDBC工具类

时间:2017-04-18 20:18:38      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:jdbc工具类

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工具类

原文地址:http://suyanzhu.blog.51cto.com/8050189/1917010

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!