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

📄 dbpool.java

📁 广州物价系统开发部分模块
💻 JAVA
字号:
package gzwj.sql;

import java.io.*;
//import com.sjky.pool.*; 
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.net.*;

public class Dbpool {

	private ConnectPool connMgr;

	private Statement stmt;

	private Connection con;

	private ResultSet rst;

	public Connection getPool(String name) throws Exception {
		try {
			connMgr = ConnectPool.getInstance();
			con = connMgr.getConnection(name);
		} catch (Exception e) {
			System.err
					.println("not able to get the connect from server please try to restart the application server");

		}
		return con;
	}
	public void rollBack(){
			try {
				this.con.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public Connection getPool_t(String name, long time) throws Exception {
		try {
			connMgr = ConnectPool.getInstance();
			con = connMgr.getConnection(name, time);
		} catch (Exception e) {
			System.err.println("connect create connect from DB!");

		}
		return con;
	}

	public ResultSet executeQuery(String SqlStr) throws Exception {
		ResultSet result = null;
		try {
			stmt = con.createStatement();
			result = stmt.executeQuery(SqlStr);
			// here add one line by jnma 12.11
			con.commit();
		} catch (java.sql.SQLException e) {
			throw new Exception("can not execute the select the sql: " + SqlStr);
		}
		return result;
	}

	public ResultSet getRst(String SqlStr) throws Exception {
		// ResultSet result = null;
		try {

			stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			// stmt = con.createStatement();
			rst = stmt.executeQuery(SqlStr);
			// here add one line by jnma 12.11
			// con.commit();
		} catch (Exception e) {
			System.out.println("can not execute the select sql: "
					+ e.toString() + " : " + SqlStr);
		}
		return rst;
	}

	public int Update(String SqlStr) throws Exception {
		int result = -1;
		try {
			stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			result = stmt.executeUpdate(SqlStr);
			// here add one line by jnma 12.11
			// con.commit();
		} catch (java.sql.SQLException e) {
			System.err.println("delete,update,insert SQL got error"
					+ e.toString() + " : " + SqlStr);
			return -1;
		}
		return result;
	}

	public boolean handleTransaction(Vector SqlArray) throws Exception {
		boolean result = false;
		int ArraySize = SqlArray.size();
		try {
			stmt = con.createStatement();
			con.setAutoCommit(false);
			System.out.println("ArraySize is" + ArraySize);
			for (int i = 0; i < ArraySize; i++) {
				System.out.println(" start to performance this sentence :"
						+ (String) SqlArray.elementAt(i));
				stmt.executeUpdate((String) SqlArray.elementAt(i));
				System.out.println(" success to performance");
			}
			con.commit();
			con.setAutoCommit(true);
			System.out.println("success to rollback the affair");
			result = true;
		} catch (java.sql.SQLException e) {
			try {
				System.out.println(e.toString());
				System.out.println("default to  operate Database");
				con.rollback();
			} catch (java.sql.SQLException Te) {
				System.err.println("got exception when to rollback one affair");
			}
		}
		try {
			con.setAutoCommit(true);
		} catch (java.sql.SQLException e) {
			System.err.println("a case of crabs to config automatic submit");
		}
		return result;
	}

	public void close(String name) throws Exception {
		try {
			if (stmt != null)
				stmt.close();
			if (con != null) {
				connMgr.freeConnection(name, con);
			}
		} catch (java.sql.SQLException e) {
			System.err.println("throw Exception when to release the connect");
		}
	}

}

⌨️ 快捷键说明

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