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

📄 dbconnect.java

📁 前台:文章浏览、发表留言、Game、Music 后台:文章相关:发表文章、修改文章、删除文章、批量移动文章 栏目相关:增加栏目、修改栏目、删除栏目、栏目链接、栏目排序系统栏目分为系统内部栏目和外部栏目
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.yhcms.db;

import java.io.UnsupportedEncodingException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

/**
 * <p>
 * Title:链接数据库
 * </p>
 * <li>链接数据库的各种操作</li>
 * <b>CopyRight: yyhweb[由由华网]</b>
 * 
 * @author stephen
 * @version YH-2.0
 */
public class DBConnect {

	private ConnCache connCache;

	private Connection conn;

	private Statement stmt;

	private PreparedStatement preStmt;

	private ResultSet rs;

	private String email;

	/**
	 * @param s
	 * @throws DBConnException
	 */
	public void init(String s) throws DBConnException {
		try {
			connCache = ConnCache.getInstance();
			conn = connCache.getConnection();
		} catch (Exception exception) {
			try {
				email = ConnCache.getInstance().getEmail();
				ConnCache.getInstance().close();
			} catch (Exception exception1) {
			}
			throw new DBConnException(
					"<h5>\u3010Database Connection Failed! Please contact to "
							+ email + "\u3011</h5>ErrorInfo\uFF1A" + exception);
		}
	}

	public DBConnect() throws DBConnException {
		email = "yyhweb@126.com";
		try {
			init("");
			stmt = conn.createStatement();
		} catch (Exception exception) {
			throw new DBConnException(exception.getMessage());
		}
	}

	public DBConnect(int i, int j) throws DBConnException {
		email = "yyhweb@126.com";
		try {
			init("");
			stmt = conn.createStatement(i, j);
		} catch (Exception exception) {
			throw new DBConnException(exception.getMessage());
		}
	}

	public DBConnect(String s) throws DBConnException, SQLException {
		email = "yyhweb@126.com";
		try {
			init("");
			prepareStatement(s);
		} catch (Exception exception) {
			throw new DBConnException(exception.getMessage());
		}
	}

	public DBConnect(boolean flag) throws DBConnException, SQLException {
		email = "yyhweb@126.com";
		try {
			if (flag)
				init("");
		} catch (Exception exception) {
			throw new DBConnException(exception.getMessage());
		}
	}

	public DBConnect(String s, String s1) throws Exception {
		email = "yyhweb@126.com";
		try {
			conn = null;
			stmt = null;
			preStmt = null;
			init(s1);
			prepareStatement(s);
		} catch (Exception exception) {
			throw new DBConnException(exception.getMessage());
		}
	}

	public DBConnect(String s, int i, int j) throws DBConnException,
			SQLException {
		email = "yyhweb@126.com";
		try {
			init("");
			prepareStatement(s, i, j);
		} catch (Exception exception) {
			throw new DBConnException(exception.getMessage());
		}
	}

	public void prepareStatement(String s) throws DBConnException, SQLException {
		preStmt = conn.prepareStatement(s);
	}

	public void prepareStatement(String s, int i, int j)
			throws DBConnException, SQLException {
		preStmt = conn.prepareStatement(s, i, j);
	}

	public void executeQuery(String s) throws DBConnException, SQLException {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
		} catch (Exception exception) {
			System.out
					.println("Error close rs when do executeQuery in DBConnect: "
							+ exception.toString());
		}
		try {
			if (stmt != null)
				rs = stmt.executeQuery(s);
		} catch (Exception exception1) {
			if (!getDefaultCommit()) {
				rollBack();
				setAutoCommit(true);
			}
			close();
			System.err.println("ErrorQuerySQL: " + s);
			System.err
					.println("ExecuteQuery error: " + exception1.getMessage());
			throw new DBConnException(
					"<h5>\u3010Database Query Failed! Please contact to "
							+ getEmail() + "\u3011</h5>ErrorInfo\uFF1A"
							+ exception1);
		}
	}

	public void executeQuery() throws DBConnException, SQLException {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
		} catch (Exception exception) {
			System.out
					.println("Error close rs when do executeQuery in DBConnect: "
							+ exception.toString());
		}
		try {
			if (preStmt != null)
				rs = preStmt.executeQuery();
		} catch (Exception exception1) {
			if (!getDefaultCommit()) {
				rollBack();
				setAutoCommit(true);
			}
			close();
			System.err
					.println("ExecuteQuery error: " + exception1.getMessage());
			throw new DBConnException(
					"<h5>\u3010Database Query Failed! Please contact to "
							+ getEmail() + "\u3011</h5>ErrorInfo\uFF1A"
							+ exception1);
		}
	}

	public int executeUpdate(String s) throws DBConnException, SQLException {
		int i = 0;
		try {
			i = stmt.executeUpdate(s);
		} catch (Exception exception) {
			// exception;
			if (!getDefaultCommit()) {
				rollBack();
				setAutoCommit(true);
			}
			close();
			System.err.println("ErrorUpdateSQL: " + s);
			System.err
					.println("ExecuteUpdate error: " + exception.getMessage());
			throw new DBConnException(
					"<h5>\u3010Database Update Failed! Please contact to "
							+ getEmail() + "\u3011</h5>ErrorInfo\uFF1A"
							+ exception);
		}
		return i;
	}

	public int executeUpdate() throws DBConnException, SQLException {
		int i = 0;
		try {
			i = preStmt.executeUpdate();
		} catch (Exception exception) {
			if (!getDefaultCommit()) {
				rollBack();
				setAutoCommit(true);
			}
			close();
			System.err
					.println("ExecuteUpdate error: " + exception.getMessage());
			throw new DBConnException(
					"<h5>\u3010Database Update Failed! Please contact to "
							+ getEmail() + "\u3011</h5>ErrorInfo\uFF1A"
							+ exception);
		}
		return i;
	}

	public String getEmail() throws DBConnException, SQLException {
		return email;
	}

	public void insert(String s) throws DBConnException, SQLException {
		stmt.addBatch(s);
	}

	public void addBatch() throws DBConnException, SQLException {
		preStmt.addBatch();
	}

	public int executeBatch(boolean flag) throws DBConnException, SQLException {
		int i = 0;
		try {
			if (flag)
				i = preStmt.executeBatch().length;
			else
				i = stmt.executeBatch().length;
		} catch (Exception exception) {
			// exception;
			close();
			System.err.println("ExecuteBatch error: " + exception.getMessage());
			throw new DBConnException(
					"<h5>\u3010Database ExecuteBatch Failed! Please contact to "
							+ getEmail() + "\u3011</h5>ErrorInfo\uFF1A"
							+ exception);
		}
		return (i);
	}

	public void close() {
		try {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
			} catch (Exception exception) {
				System.out
						.println("\u5173\u95EDResultSet\u9519\u8BEF\uFF0C\u4E0B\u9762\u4F1A\u5C1D\u8BD5\u518D\u5173\u95ED\u4E00\u6B21\u3002");
				rs.close();
				rs = null;
			} finally {
				try {
					if (stmt != null) {
						stmt.clearBatch();
						stmt.close();
						stmt = null;
					}
				} catch (Exception exception3) {
					System.out
							.println("\u5173\u95EDStatement\u9519\u8BEF\uFF0C\u4E0B\u9762\u4F1A\u5C1D\u8BD5\u518D\u5173\u95ED\u4E00\u6B21\u3002");
					stmt.close();
					stmt = null;
				} finally {
					try {
						if (preStmt != null) {
							preStmt.clearParameters();
							preStmt.close();
							preStmt = null;
						}
					} catch (Exception exception5) {
						System.out
								.println("\u5173\u95EDPrepareStatement\u9519\u8BEF\uFF0C\u4E0B\u9762\u4F1A\u5C1D\u8BD5\u518D\u5173\u95ED\u4E00\u6B21\u3002");
						preStmt.close();
						preStmt = null;
					} finally {
						try {
							if (conn != null) {
								conn.close();
								conn = null;
							}
						} catch (Exception exception7) {
							System.out
									.println("\u5173\u95EDConnection\u9519\u8BEF\uFF0C\u4E0B\u9762\u4F1A\u5C1D\u8BD5\u518D\u5173\u95ED\u4E00\u6B21\u3002");
							conn.close();
							conn = null;
						}
					}
				}
			}
		} catch (Exception exception1) {
			System.out
					.println("\u5173\u95ED\u8FDE\u63A5\u6C60\u53E5\u67C4\u65F6\u51FA\u73B0\u9519\u8BEF\uFF01");
			exception1.printStackTrace();
		}
	}

	public boolean next() throws DBConnException, SQLException {
		boolean b = false;
		try {
			b = rs.next();
		} catch (Exception exception) {
			// exception;
			close();
			System.err.println("next error: " + exception.getMessage());
			throw new DBConnException(
					"<h5>\u3010Database Query Failed! Are you sure you have done executeQuery?\u3011</h5>ErrorInfo\uFF1A"
							+ exception);
		}
		return (b);
	}

	public boolean previous() throws DBConnException, SQLException {
		boolean b;
		try {
			b = rs.previous();
		} catch (Exception exception) {
			// exception;
			close();
			System.err.println("previous error: " + exception.getMessage());
			throw new DBConnException(
					"<h5>\u3010Database Query Failed! Are you sure you have done executeQuery?\u3011</h5>ErrorInfo\uFF1A"
							+ exception);
		}
		return (b);

⌨️ 快捷键说明

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