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

📄 dbhelper.java

📁 对XML和DB的CURD的基本操作.使用的两个比较常用的组合设计模式:MVC模式+DAO模式。
💻 JAVA
字号:
package com.mycompany.myapp.dao.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mycompany.myapp.util.Constants;

/**
 * the database helper with some convenience methods for jdbc operations. 
 */
public class DbHelper {
	public static Connection conn;
	
	/**
	 * get the connection
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception {
		Class.forName(Constants.CLASS_NAME);
		conn = DriverManager.getConnection(Constants.CONNET_STR,
				Constants.USER, Constants.PASSWORD);
		return conn;
	}

	/**
	 * get the resultSet
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static ResultSet getResultSet(String sql) throws Exception {
		boolean bSuccess = true;
		ResultSet rs = null;
		Statement stmt;
		Connection con = getConnection();
		if (con == null)
			bSuccess = false;
		if (bSuccess) {
			try {
				stmt = con.createStatement();
				rs = stmt.executeQuery(sql);
			} catch (SQLException e) {
				e.printStackTrace();
				bSuccess = false;
			}
		}
		if (bSuccess)
			return rs;
		else
			return null;
	}

	/**
	 * execute the sql
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static boolean execute(String sql) throws Exception {
		boolean bSuccess = true;
		Statement stmt = null;
		Connection con = getConnection();
		if (con == null)
			bSuccess = false;
		if (bSuccess) {
			try {
				stmt = con.createStatement();
				bSuccess = stmt.execute(sql);
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
				bSuccess = false;
			}
		}
		return bSuccess;
	}

	/**
	 * close the connection
	 * @param conn
	 */
	public static void closeConnection() {
		try {
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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