brenda写的sql增删改查模板代码

package com.cpcnet.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.cpcnet.utils.ExceptionHelper;
import com.cpcnet.utils.PropertiesHelper;


public class MysqlDBConn {
	
	public static String DB_CONFIG = "com/cpcnet/config/db.properties";
	
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	
	private String MY_DRIVER = ""; 
	private String MY_URL = ""; 
	private String MY_USERNAME = ""; 
	private String MY_PASSWORD = "";
	
	public MysqlDBConn() {
		init("mysql");
	}
	
	public MysqlDBConn(String prefixName) {
		init(prefixName);
	}
	
	public int execUpdate(String sql) {
		int flag = 0;
		try {
			stmt = this.getConn().createStatement();
			flag = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			ExceptionHelper.getInstance().addExceptionList(e);
		}
		return flag;
	}

	
	public int execUpdate(String sql, Object[] parameters) {
		int flag = 0;
		try {
			PreparedStatement pstmt = this.getConn().prepareStatement(sql);
			for(int i=0, l=parameters.length; i<l; i++) {
				pstmt.setObject(i+1, parameters[i]);
			}
			flag = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			ExceptionHelper.getInstance().addExceptionList(e);
		}
		return flag;
	}
	
	public ResultSet execQuery(String sql, Object[] parameters) {
		try {
			PreparedStatement pstmt = this.getConn().prepareStatement(sql);
			for(int i=0, l=parameters.length; i<l; i++) {
				pstmt.setObject(i+1, parameters[i]);
			}
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			ExceptionHelper.getInstance().addExceptionList(e);
		}
		return rs;
	}
	
	public ResultSet execQuery(String sql) {
		try {
			stmt = this.getConn().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			ExceptionHelper.getInstance().addExceptionList(e);
		}
		return rs;
	}
	
	private void init(String prefixName) {
		Properties prop = new PropertiesHelper().getProperty(DB_CONFIG);
		MY_DRIVER = prop.getProperty(prefixName + "_driver");
		MY_URL = prop.getProperty(prefixName + "_url");
		MY_USERNAME = prop.getProperty(prefixName + "_username");
		MY_PASSWORD = prop.getProperty(prefixName + "_password");
	}

	public void closeDB(){
		try {
			if(rs != null) {
				rs.close();
			}
			if(stmt != null) {
				stmt.close();
			}
			if(conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			ExceptionHelper.getInstance().addExceptionList(e);
		}
	}

	public Connection getConn(){
		try {
			if(conn == null) {
				Class.forName(MY_DRIVER).newInstance();
				conn = DriverManager.getConnection(MY_URL, MY_USERNAME, MY_PASSWORD);
			}
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			ExceptionHelper.getInstance().addExceptionList(e);
		}
		return conn;
	}
	
}

你可能感兴趣的:(brenda写的sql增删改查模板代码)