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

📄 dbtools.java

📁 基于Struts的通讯录系统
💻 JAVA
字号:
package addressbook.database.utils;

import java.sql.*;
import java.util.*;

public class DBTools
{
	public Connection conn=null;

	/**
   	 * 构造函数
	 * @throws SQLException 
     */
	public DBTools() {

		try
		{
			Driver driver = (Driver)(Class.forName(Global.DB_DRIVER).newInstance());
			DriverManager.registerDriver(driver);
        	conn=DriverManager.getConnection(Global.DB_URL,Global.DB_USER_NAME,Global.DB_PASSWORD);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	/**
   	 * 得到一个数据库连接
     * @return 数据库连接
     */
	public Connection getDbConnection()
	{
		return conn;
	}

	/**
   	 * 根据要查询的sql语句,获得查询结果
     * @param sql 要执行的数据库sql语句
     * @return 查询结果保存到一个向量中
	 * @throws SQLException 
     */
	public synchronized Vector select(String sql) throws SQLException
	{
		Statement stmt=null;
		ResultSet rs=null;

		Vector vret=new Vector(15,5);
		String colName="";
		int iCol=0;

		try
		{
			stmt=conn.createStatement();
			rs=stmt.executeQuery(sql);

			ResultSetMetaData rsmd=rs.getMetaData();
			iCol=rsmd.getColumnCount();

			while(rs.next())
			{
				Hashtable ht=new Hashtable();
				for(int i=1;i<=iCol;i++)
				{
					colName=rsmd.getColumnName(i).toLowerCase();
					Object obj = rs.getObject(colName);

					if(obj==null)
					{
						ht.put(colName,"");
					}
					else
					{
						ht.put(colName,obj);
					}
				}
				vret.addElement(ht);
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new SQLException();
		}
		finally
		{
			try
			{
				if(rs!=null) rs.close();
				if(stmt!=null) stmt.close();
			}
			catch(SQLException sqlex)
			{
				sqlex.printStackTrace();
			}
		}
		return vret;
	}

	/**
   	 * 根据要查询的sql语句,获得查询结果
     * @param sql 要执行的数据库sql语句
     * @param max_rows 查询要返回的最大记录数目
     * @return 查询结果
	 * @throws SQLException 
     */
	public synchronized Vector select(String sql,int max_rows) throws SQLException
	{
		Statement stmt=null;
		ResultSet rs=null;

		Vector vret=new Vector(10,5);
		String colName="";
		int iCol=0;

		try
		{
			stmt=conn.createStatement();
			stmt.setMaxRows(max_rows);
			rs=stmt.executeQuery(sql);

			ResultSetMetaData rsmd=rs.getMetaData();
			iCol=rsmd.getColumnCount();

			while(rs.next())
			{
				Hashtable ht=new Hashtable();
				for(int i=1;i<=iCol;i++)
				{
					colName=rsmd.getColumnName(i).toLowerCase();
					Object obj = rs.getObject(colName);

					if(obj==null)
					{
						ht.put(colName,"");
					}
					else
					{
						ht.put(colName,obj);
					}
				}
				vret.addElement(ht);
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new SQLException();
		}
		finally
		{
			try
			{
				if(rs!=null) rs.close();
				if(stmt!=null) stmt.close();
			}
			catch(SQLException sqlex)
			{
				sqlex.printStackTrace();
			}
		}
		return vret;
	}

	/**
   	 * 根据要查询的sql语句,获得查询单个字段的结果
     * @param sql 要执行的数据库sql语句
     * @return 查询结果
	 * @throws SQLException 
     */
	public synchronized String selectSingle(String sql) throws SQLException
	{
		Statement stmt=null;
		ResultSet rs=null;
		String tmp="";

		try
		{
			stmt=conn.createStatement();
			rs=stmt.executeQuery(sql);
			if(rs.next())
			{
				tmp=rs.getString(1);
			}
			if(tmp==null) tmp="";
		}
		catch(Exception ex)
		{
			tmp="";
			throw new SQLException();
		}
		finally
		{
			try
			{
				if(rs!=null) rs.close();
				if(stmt!=null) stmt.close();
			}
			catch(SQLException sqlex)
			{
				sqlex.printStackTrace();
			}
		}
		return tmp;
	}

	/**
   	 * 执行数据库更新语句
     * @param sql 要执行的数据库sql语句
     * @return 更新结果
	 * @throws SQLException 
     */
	public synchronized int update(String sql) throws SQLException
	{
		Statement stmt=null;
		ResultSet rs=null;
		int ret=0;

		try
		{
			conn.setAutoCommit(false);
			stmt=conn.createStatement();
			stmt.executeUpdate(sql);
			conn.commit();
			ret=0;
		}
		catch(Exception ex)
		{
			try
			{
				conn.rollback();
			}
			catch(SQLException sqlex1)
			{
				sqlex1.printStackTrace();
			}
			ret=-1;
			throw new SQLException();
		}
		finally
		{
			try
			{
				if(rs!=null) rs.close();
				if(stmt!=null) stmt.close();
			}
			catch(SQLException sqlex2)
			{
				sqlex2.printStackTrace();
			}
		}
		return ret;
	}

	/**
   	 * 执行数据库插入语句
     * @param sql 要执行的数据库sql语句
     * @return 插入结果
	 * @throws SQLException 
     */
	public int insert(String sql) throws SQLException
	{
		return update(sql);
	}

	/**
   	 * 执行数据库删除语句
     * @param sql 要执行的数据库sql语句
     * @return 删除结果
	 * @throws SQLException 
     */
	public int delete(String sql) throws SQLException
	{
		return update(sql);
	}

	/**
   	 * 关闭打开的数据库连接
	 * @throws SQLException 
     */
	public void close() throws SQLException
	{
		try
		{
			if(conn!=null) conn.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new SQLException();
		}
	}

	/**
   	 * 测试程序
     */
	/*
	public static void main(String args[])
	{
		try
		{
			DBTools db=new DBTools();
			Vector vret=db.select("select * from account");
			for(int i=0;i<vret.size();i++)
			{
				Hashtable ht=(Hashtable)vret.get(i);
				System.out.println(ht.get("uid")+"    "+ht.get("username")+"  "+ht.get("password"));
			}
			vret.clear();
			db.close();		
		}
		catch(Exception ex)		{
			ex.printStackTrace();
		}
	}
	*/
      
}

⌨️ 快捷键说明

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