Web Dev Notes - 15 - 对于数据库连接的封装

自己写的数据库封装,在使用时只需要使用query和update操作即可



package api.com.servlet;

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


public class DBBean {

	private static Connection con = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;
	private static boolean is_ready = false;
	private static boolean is_driver_loaded = false;

	private DBBean() {

	}

	private static void load() {
		try {
			Class.forName(StaticConfig.db_driver);
		} catch (Exception e) {
			System.err.println("DB Driver Load Error!");
			is_driver_loaded = false;
		}
		is_driver_loaded = true;
	}

	private static void getReady() {
		if (is_driver_loaded == false)
			load();
		if (is_driver_loaded == false) {
			is_ready = false;
			return;
		}

		try {
			con = DriverManager.getConnection(StaticConfig.db_url,
					StaticConfig.db_user, StaticConfig.db_password);
		} catch (Exception e) {
			System.err.println("DB Connection Fetch Error!");
			con = null;
			is_ready = false;
			return;
		}

		is_ready = true;

	}

	public static ResultSet query(String sql) {

		if (is_ready == false) {
			getReady();
		}

		if (is_ready == false)
			return null;
		
		try {
			stmt = con.createStatement();
		} catch (Exception e) {
			System.err.println("DB Statement Fetch Error!");
			stmt = null;
			return null;
		}
		
		try {
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			System.err.println("DB Query Error!");
			rs = null;
		}
		return rs;
	}

	public static int update(String sql) {
		
		if (is_ready == false) {
			getReady();
		}

		if (is_ready == false)
			return -1;
		
		try {
			stmt = con.createStatement();
		} catch (Exception e) {
			System.err.println("DB Statement Fetch Error!");
			stmt = null;
			return -1;
		}
		int inflenced_row = -1;
		if (stmt == null) {
			System.err.println("DB Statement Fetch Error!");
			return -1;
		}
		try {
			inflenced_row = stmt.executeUpdate(sql);
		} catch (Exception e) {
			System.err.println("DB Update Error!");
			inflenced_row = -1;
		}
		return inflenced_row;
	}

	public void disconnect() {
		try {
			if (null == rs) {
				rs.close();
			}
			if (null == stmt) {
				stmt.close();
			}
			if (null == con) {
				con.close();
			}
		} catch (Exception e) {
			System.err.println("DB Resource Release Error!");
		}
	}

}

为了防止乱码可以指定读取的输入库的编码,修改如下


private static void getReady() {
		if (is_driver_loaded == false)
			load();
		if (is_driver_loaded == false) {
			is_ready = false;
			return;
		}

		try {
			Properties prop = new Properties();
			prop.put("user", StaticConfig.db_user);
			prop.put("password",StaticConfig.db_password);
			prop.put("characterEncoding","GBK"); 
			con = DriverManager.getConnection(StaticConfig.db_url,
					prop);
		} catch (Exception e) {
			System.err.println("DB Connection Fetch Error!");
			con = null;
			is_ready = false;
			return;
		}

		is_ready = true;

	}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值