⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.java

📁 使用jsp开发的一个基于HSQLDB的快餐订购管理系统
💻 JAVA
字号:
package com.uiwz.db;

import java.sql.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Database
{
	private static final Log log = LogFactory.getLog(Database.class);
	
	public Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	
	public Database()
	throws SQLException
	{
		conn = getConnection();
		conn.setAutoCommit(true);
		stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
	}
	
	public Database(boolean isCreateStatement)
	throws SQLException
	{
		conn = getConnection();
		if(isCreateStatement)
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
	}
	
	public static Connection getConnection()
	throws SQLException
	{
		try
		{
			return DBCPConnectionPool.getInstance().getConnection();
		}
		catch(Exception e)
		{
			//log.error("Failed to get a connection: ", e);
			throw new SQLException("Failed to get a connection.");
		}
	}
	
	public ResultSet query(String sqlStr)
	throws SQLException
	{
		log.debug(sqlStr);
		return stmt.executeQuery(sqlStr);
	}
	
	public void insert(String sqlStr)
	throws SQLException
	{
		log.debug(sqlStr);
		stmt.executeUpdate(sqlStr);
	}
	
	public void update(String sqlStr)
	throws SQLException
	{
		log.debug(sqlStr);
		stmt.executeUpdate(sqlStr);
	}
	
	public void delete(String sqlStr)
	throws SQLException
	{
		log.debug(sqlStr);
		stmt.executeUpdate(sqlStr);
	}

	public boolean execute(String sqlStr)
	throws SQLException
	{
		log.debug(sqlStr);
		return stmt.execute(sqlStr);
	}

	public PreparedStatement prepareStatement(String sqlStr)
	throws SQLException
	{
		log.debug(sqlStr);
		return conn.prepareStatement(sqlStr) ;
    }
	
	public void beginTrans() 
	throws SQLException
	{
		if(conn != null)
			conn.setAutoCommit(false);
	}
	
	public void commitTrans() 
	throws SQLException
	{
		if(conn != null)
		{
			conn.commit();
			conn.setAutoCommit(true);
		}
	}
	
	public void rollback() 
	throws SQLException
	{
		if(conn != null)
		{
			conn.rollback();
			conn.setAutoCommit(true);
		}
	}

	public void close()
	throws SQLException
	{
		try
		{
			if(rs != null)
				rs.close();
	
			rs = null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		try
		{
			if(stmt != null)
				stmt.close();
			
			stmt = null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		try
		{
			if(conn != null && !conn.isClosed())
				conn.close();
			
			conn = null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static void close(Database db){
		if(db != null){
			try{
				db.close();
				db = null;
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public static void close(ResultSet rs){
		if(rs != null){
			try{
				rs.close();
				rs = null;
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public static void close(Statement stmt){
		if(stmt != null){
			try{
				stmt.close();
				stmt = null;
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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