dbutils.java

来自「《j2ee开发全程实录》随书源码」· Java 代码 · 共 120 行

JAVA
120
字号
package com.cownew.ctk.database;

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

import org.apache.log4j.Logger;

public class DbUtils
{
	
	private static final Logger logger = Logger.getLogger(DbUtils.class);
	/**
	 * close the connection
	 * @param conn
	 */
	public static void close(Connection conn)
	{
		if (conn != null)
		{
			
			try
			{
				if(conn.isClosed())
				{
					return;
				}
				conn.close();
			} catch (SQLException e)
			{
				logger.error(e.getMessage(),e);
			}
		}
	}

	/**
	 * close the connection and Statement
	 * @param conn
	 * @param stmt
	 */
	public static void close(Connection conn, Statement stmt)
	{
		if (stmt != null)
		{
			try
			{
				stmt.close();
			} catch (SQLException e)
			{
				logger.error(e.getMessage(),e);
			}
		}
		close(conn);
	}

	/**
	 * close the connection Statement and ResultSet
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Connection conn, Statement stmt, ResultSet rs)
	{
		if (rs != null)
		{
			try
			{
				rs.close();
			} catch (SQLException e)
			{
				logger.error(e.getMessage(),e);
			}
		}
		close(conn, stmt);
	}
	
	/**
	 * close the connection Statement and ResultSet
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Statement stmt, ResultSet rs)
	{
		if (rs != null)
		{
			try
			{
				rs.close();
			} catch (SQLException e)
			{
				logger.error(e.getMessage(),e);
			}
		}
		close(stmt);
	}

	/**
	 * close the connection Statement
	 * 
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Statement stmt)
	{
		if (stmt != null)
		{
			try
			{
				stmt.close();
			} catch (SQLException e)
			{
				logger.error(e.getMessage(),e);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?