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

📄 db8manager.java.svn-base

📁 一套DB2 7到8版本的程序。内附创建ZIP的JAVA类
💻 SVN-BASE
字号:
package com.highsoft.database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;



/**
 * @author 张毅
 * @exception MyException
 */
public class DB8Manager {

	/**
	 * 数据库的连接对象
	 */
	private Connection con;

	/**
	 * 查询数据库返回的结果集对象
	 */
	private ResultSet rs;

	/**
	 * 操作数据库对象
	 */
	private Statement st;
	
	private Properties pro ;

	public DB8Manager(){
		OpenDB();
	}

	/**
	 * 数据库连接方法
	 * @throws MyException 
	 */	
	public void OpenDB(){
		pro= new Properties();
		try {
			pro.load(DB8Manager.class.getResourceAsStream("jhsy.properties"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			System.out.println(e1.getMessage());
		}

		try {
			Class.forName(pro.getProperty("dist.className"));
			con = java.sql.DriverManager.getConnection(pro.getProperty("dist.url"), pro.getProperty("dist.username"), pro.getProperty("dist.password"));
			st = con.createStatement();
			System.out.println("DB2 8数据库连接成功");
		} catch (ClassNotFoundException e) {
			// TODO 自动生成 catch 块
			//log.error("OpenDB_数据库驱动不存在"+e);
			//throw new MyException("driver");
			System.out.println("ClassNotFoundException="+e.getMessage());
		} catch (SQLException e) {
			//log.error("OpenDB_获取数据库连接出错"+e);
			//throw new MyException("error");
			System.out.println("SQLException="+e.getMessage());
		} 
	}
	
	/**
	 * 对数据库执行添加、删除语句时调用此方法 
	 * @param strSql 完整的SQL语句
	 * @return boolean 布尔型值 true为执行成功,false时失败。
	 * @throws MyException 
	 */

	public boolean executeDB(String strSql){
		boolean flag=false;
		try {
			st = con.createStatement();
			st.execute(strSql);
			flag=true;
		} catch (SQLException ex) {
			//log.error("executeDB_操作失败SQL" + strSql);
			//log.error("executeDB_操作失败!" + ex.toString());
			//throw new MyException("executeerror");
			System.out.println(ex.getMessage());
		}finally{
//			if(st!=null)
//			{
//				try {
//					st.close();
//				} catch (SQLException e) {
//					// TODO 自动生成 catch 块
//					//throw new MyException("exestclose");
//					System.out.println(e.getMessage());
//				}
//			}
//			if(con!=null)
//			{
//				try {
//					con.close();
//				} catch (SQLException e) {
//					// TODO 自动生成 catch 块
//					//throw new MyException("execonclose");
//					System.out.println(e.getMessage());
//				}
//			}
		}
		return flag;
	}
	
	/**
	 * 关闭数据库所有连接对象时调用这个close()方法.
	 * @throws MyException 
	 */
	public void close(){
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException ex) {
				//log.error("关闭RS失败" + ex.toString());
				//throw new MyException("rsclose");
				System.out.println(ex.getMessage());
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException ex1) {
				//log.error("关闭ST失败" + ex1.toString());
				//throw new MyException("stclose");
				System.out.println(ex1.getMessage());
			}
		}
		if (con != null) {
			try {
				con.close();
			} catch (SQLException ex2) {
				//log.error("关闭CON失败" + ex2.toString());
				//throw new MyException("conclose");
				System.out.println(ex2.getMessage());
			}
		}
	}
	
	/**
	 * 执行事务操作时 调用此方法。
	 * 
	 * @param strSqlList ArrayList类型集合 可有一条或多条SQL语句在内
	 * @return boolean 布尔型值 true为执行成功,false时失败。
	 * @throws MyException 
	 */
	public boolean exeDbTran(List strSqlList){
		boolean bl = false;
		try {
			st = con.createStatement();
			con.setAutoCommit(false);
			//System.out.println(strSqlList.size());
			for (int i = 0; i < strSqlList.size(); i++) {
				if (strSqlList.get(i) != null) {
					System.out.println(strSqlList.get(i).toString());
					st.execute(strSqlList.get(i).toString());
				}
			}
			con.commit();
			bl = true;
		} catch (SQLException e) {
			if(con!=null)
			{
				try {
					con.rollback();
				} catch (SQLException e1) {
					// TODO 自动生成 catch 块
					System.out.println(e1.getMessage());
				}
			}
			System.out.println(e.getMessage());
		} finally {
			try {
				con.setAutoCommit(true);
			} catch (SQLException e) {
				// TODO 自动生成 catch 块
				System.out.println(e.getMessage());
			}
			if(st!=null)
			{
				try {
					st.close();
				} catch (SQLException e) {
					// TODO 自动生成 catch 块
					System.out.println(e.getMessage());
				}
			}
			if(con!=null)
			{
				try {
					con.close();
				} catch (SQLException e) {
					// TODO 自动生成 catch 块
					System.out.println(e.getMessage());
				}
			}
		}
		return bl;
	}
	public static void main(String[] args) {
		DB8Manager db=new DB8Manager();
	}
}

⌨️ 快捷键说明

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