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

📄 parentbean.java

📁 这是一个很好用的软件
💻 JAVA
字号:
package oa.main;

/*******************************************************************************
 * Copyright (C),2006-4-2, NANJIN. All rights reserved. Filename:
 * ParentBean.java Author: 王银元 Version 1.0 Date:2006-4-2
 * Description:封装与数据库类之上,Bean之下,用于选择连接,释放连接
 */
import java.util.*;
import java.sql.*;

public class ParentBean extends Object {
	//数据库处理对象*/
	protected DataBase db = new DataBase();

	//字符串处理时使用对象*/
	protected static DealString ds = new DealString();

	/** 当前连接的数据库类型 */
	public String DBType = "NULL";

	public String DBName = "";

	public static String FilesPath = "";

	public String ip = "";

	public String port = "";

	public String user = "";

	public String password = "";

	//构造函数,初始化连接*/
	public ParentBean() {
		this.DBName = "news";
		this.DBType = "SQLServer";
		this.ip = "127.0.0.1";
		this.port = "1433";
		this.user = "news";
		this.password = "news";
		this.createConn();
		this.init();

	}

	public void clean() {
		this.DBName = "";
		this.DBType = "NULL";
		this.ip = "";
		this.port = "";
		this.user = "";
		this.password = "";
	}

	public void init() {
		ResultSet rs = selectRecord("select XMMC from news.CODE_ZDB where ZDMC='上传文件存放路径'");
		Statement stmt = null;
		try {
			if (rs.next()) {
				FilesPath = rs.getString(1);
			}
		} catch (Exception e) {
			System.out.println("ParentBean::init(void)运行时出错:" + e);
		} finally {
			if (rs != null)
				try {
					stmt = rs.getStatement();
					rs.close();
				} catch (Exception e) {
					System.out.println("ParentBean::init(void)关闭记录集rs时出错" + e);
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
					System.out.println("ParentBean::init(void)关闭声明时statement出错"
							+ e);
				}
		}
	}

	public void createConn()//int type,String ip,String port,String
	// dbsrv,String dbname,String usr,String pwd)
	{
		if (this.DBType.equals("NULL")) {
			System.out.println("建立数据库参数不全,请确认后重试.");
			System.exit(1);
		} else
			db.createConn("com.microsoft.jdbc.sqlserver.SQLServerDriver",
					"jdbc:microsoft:sqlserver://" + ip + ":" + port
							+ ";DatabaseName=" + this.DBName, this.user,
					this.password);
	}

	/** 释放连接 */
	public void closeConn() {
		DBType = "NULL";
		db.releaseConn();

	}

	public void closeAllDb() {
		db.closeAll();
	}

	/** 查询记录 */
	public ResultSet selectRecord(String sql) {
		return db.QuerySQL(sql);
	}

	/** 新增记录 */
	protected int insertRecord(Vector vect) {
		/**
		 * Vector:第1项 表名(String) // 第2项
		 * 列名(Vector[Field(String),Value(String,CLOB,BLOB),Type("CHAR","NUM","TIME","CLOB","BLOB")])
		 */

		//临时变量
		String sqlField = "";//形如(F1,F2)
		String sqlValue = "";//形如(V1,V2)
		String field = "";
		String value = "";
		String type = "";

		for (int i = 1; i < vect.size(); i++) {
			//对某一个字段
			Vector v_t = (Vector) vect.get(i);
			field = (String) v_t.get(0);
			value = (String) v_t.get(1);
			if (value.indexOf("'") != -1) {
				value = value.replaceAll("'", "''");
			}
			type = (String) v_t.get(2);

			//组合字段SQL
			if (sqlField.equals(""))
				sqlField = "(";
			else
				sqlField = sqlField + ",";
			sqlField = sqlField + field;

			//组合值SQL
			if (sqlValue.equals(""))
				sqlValue = "(";
			else
				sqlValue = sqlValue + ",";
			if (value.equals(""))//为空时
			{
				sqlValue = sqlValue + "null";
			} else if (type.equals("CHAR"))//字符串
			{
				sqlValue = sqlValue + "'" + value + "'";
			} else if (type.equals("NUM"))//数值
			{
				sqlValue = sqlValue + value;
			} else if (type.equals("TIME"))//日期
			{
				sqlValue = sqlValue + "to_date('yyyy-MM-dd HH:mm:ss','" + value
						+ "')";
			} else if (type.equals("CLOB"))//clob类型
			{
				sqlValue = sqlValue + "empty_clob()";
			} else if (type.equals("BLOB"))//blob类型
			{
				sqlValue = sqlValue + "empty_blob()";
			}
		}
		sqlField = sqlField + ")";
		sqlValue = sqlValue + ")";
		String sql = "insert into news." + (String) vect.get(0) + sqlField
				+ " values" + sqlValue;
		return db.ExecuteSQL(sql);
	}

	/** 修改记录 */
	protected int updateRecord(Vector vect) {
		/**
		 * Vector:第1项 表名(String) // 第2项
		 * 列名(Vector[Field(String),Value(String,CLOB,BLOB),Type("CHAR","NUM","TIME","CLOB","BLOB")]) //
		 * 第3项 条件(String sql)
		 */

		//临时变量
		String sqlSet = "";//形如(Name='name',ID=9)
		String field = "";
		String value = "";
		String type = "";

		int i = 1;
		int n = vect.size();
		for (; i < (n - 1); i++) {
			//对某一个字段
			Vector v_t = (Vector) vect.get(i);
			field = (String) v_t.get(0);
			value = (String) v_t.get(1);
			if (value.indexOf("'") != -1) {
				value = value.replaceAll("'", "''");
			}
			type = (String) v_t.get(2);

			//组合字段SQL
			if (sqlSet.equals(""))
				sqlSet = " ";
			else
				sqlSet = sqlSet + ",";
			sqlSet = sqlSet + field + "=";
			if (value.equals("") && type.equals("NUM"))//为空时
			{
				sqlSet = sqlSet + "null";
			}
			if (type.equals("CHAR"))//字符串
			{
				sqlSet = sqlSet + "'" + value + "'";
			} else if (type.equals("NUM"))//数值
			{
				sqlSet = sqlSet + value;
			} else if (type.equals("TIME"))//日期
			{
				sqlSet = sqlSet + "to_date('yyyy-MM-dd HH:mm:ss','" + value
						+ "')";
			} else if (type.equals("CLOB"))//clob类型
			{
			} else if (type.equals("BLOB"))//blob类型
			{
			}
		}

		String sql = "update " + (String) vect.get(0) + " set " + sqlSet;
		String sqlWhere = (String) vect.get(vect.size() - 1);
		if (!sqlWhere.equals("")) {
			sql = sql + " where " + sqlWhere;
		}
		return db.ExecuteSQL(sql);
	}

	/** 删除记录 */
	protected int deleteRecord(String sql) {
		return db.ExecuteSQL(sql);
	}

	/** 执行语句 */
	protected int executeUpdate(String sql) {
		return db.ExecuteSQL(sql);
	}

	/** 取得数据集内容 */
	public Vector getResultSetData(ResultSet rs) {
		Vector vect = new Vector();
		Statement stmt = null;
		try {
			//取得列数和列名
			ResultSetMetaData rsmd = rs.getMetaData();
			int cols = rsmd.getColumnCount();
			while (rs.next()) {
				Hashtable hash = new Hashtable();
				for (int i = 1; i <= cols; i++) {
					String field = ds.toString(rsmd.getColumnName(i));
					String value = ds.toString(rs.getString(i));
					hash.put(field, value);
				}
				vect.add(hash);
			}
		} catch (SQLException e) {
			System.out
					.println("调用ParentBean.getResultSetData()函数从ResutlSet中取数据时出错:\n");
			e.printStackTrace();
		} finally {
			if (rs != null)
				try {
					stmt = rs.getStatement();
					rs.close();
				} catch (Exception e) {
					System.out
							.println("调用ParentBean.getResultSetData()函数,关闭记录集rs时出错"
									+ e);
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
					System.out
							.println("调用ParentBean.getResultSetData()函数中,关闭声明时statement出错"
									+ e);
				}
		}
		return vect;
	}

	/** 从ResultSet 中取一个列值 */
	public int getIntByResultSet(ResultSet rs, String colname) {
		String s = "0";
		Statement stmt = null;
		try {
			if (rs.next()) {
				s = ds.toString(rs.getString(colname));
			}
		} catch (Exception e) {
			System.out.println("调用ParentBean.getIntByResultSet()运行时出错:" + e);
		} finally {
			if (rs != null)
				try {
					stmt = rs.getStatement();
					rs.close();
				} catch (Exception e) {
					System.out
							.println("调用ParentBean.getIntByResultSet()运行时关闭记录集rs时出错"
									+ e);
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
					System.out
							.println("调用ParentBean.getIntByResultSet()运行时关闭声明时statement出错"
									+ e);
				}
		}
		return Integer.parseInt(s);
	}

	public String getStrByResultSet(ResultSet rs, String colname) {
		Statement stmt = null;
		String retstr = "";
		try {
			if (rs.next()) {
				retstr = ds.toString(rs.getString(colname));
			}
		} catch (Exception e) {
			System.out.println("运行时出错:" + e);
		} finally {
			if (rs != null)
				try {
					stmt = rs.getStatement();
					rs.close();
				} catch (Exception e) {
					System.out
							.println("调用ParentBean.getStrByResultSet()运行时关闭记录集rs时出错"
									+ e);
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
					System.out
							.println("调用ParentBean.getStrByResultSet()运行时关闭声明时statement出错"
									+ e);
				}
		}
		return retstr;
	}

	/** 产生唯一编号 */
	public int makeID(String table, String field1, String field2,
			String value1, boolean type1) {
		return db.makeID(table, field1, field2, value1, type1);
	}

	public int makeID(String table, String field1, String field2,
			String field3, String value1, String value2, boolean type1,
			boolean type2) {
		return db.makeID(table, field1, field2, field3, value1, value2, type1,
				type2);
	}

	public int makeID_Add1(String table, String field1, String field2,
			String value1, boolean type1) {
		return db.makeID(table, field1, field2, value1, type1);
	}

	public int makeID_Add1(String table, String field1, String field2,
			String field3, String value1, String value2, boolean type1,
			boolean type2) {
		return db.makeID(table, field1, field2, field3, value1, value2, type1,
				type2);
	}

	/** 将名称转换为编号 */
	public int toID(String table, String field1, String field2, String value1) {
		return db.toID(table, field1, field2, value1);
	}

	/** 将编号转换为名称 */
	public String toName(String table, String field1, String field2,
			String value1) {
		return db.toName(table, field1, field2, value1);
	}

	/** 写数据库时某一个字段的存储类型 */
	protected Vector addVector(String field, String value, String type) {
		Vector vect = new Vector();
		vect.add(field);
		vect.add(value);
		vect.add(type);
		return vect;
	}

	/** 分页时取得一页的数据量 */
	public Vector getOnePage(String sql, int page, int records) {
		return db.getOnePage(sql, page, records);
	}

	/** 为某一个字段进行重新排序 */
	public int setSort(String table, String field1, String field2,
			String wherestr, String orderstr, boolean b) {
		return db.setSort(table, field1, field2, wherestr, orderstr, b);
	}

	/** 数据库信息 */
	public Hashtable getDataBaseInfo() {
		return db.getDataBaseInfo();
	}

	/** 创建申明对象 */
	public void prepareStatement(String sql) {
		db.prepareStatement(sql);
	}

	/** 执行查询 */
	public void executeQuery() {
		db.executeQuery();
	}

	/** 转向下一条 */
	public boolean next() {
		return db.next();
	}


	/** 执行更新 */
	public void executeUpdate() {
		db.executeUpdate();
	}

	/** 关闭申明对象 */
	public void closePstm() {
		db.closePstm();
	}

	/** 关闭游标 */
	public void closeRs() {
		db.closeRs();
	}

	public Vector getDataBySql(String sql) {
		return db.getData(sql);
	}

	public boolean getAutoCommit() {
		return db.getAutoCommit();
	}

	public void closeAutoCommit() {
		db.closeAutoCommit();
	}

	public void commit() {
		db.commit();
	}

	public void rollback() {
		db.rollback();
	}

	public void openAutoCommit() {
		db.openAutoCommit();
	}

	public void createStatement() {
		db.createStatement();
	}

	public void closeStm() {
		db.closeStm();
	}

	protected void finalize() {
		this.DBType = "NULL";
		this.closeAllDb();
	}

	public static void main(String[] args) {
		System.out.println("\tbegin\n\n");
		ParentBean p = new ParentBean();
		if (p.db.conn != null) {
			System.out.println("db 测试成功");
			Vector v = p.getDataBySql("select * from news.menu");
			if (v.size() > 5) {
				System.out.println("执行查询成功!");
			}
			try {
				ResultSet r = p.selectRecord("select * from news.menu");
				if (r.next())
					System.out.println("查询结果有:" + r.getString(6));

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		System.out.println("\tend\n");
	}

}

⌨️ 快捷键说明

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