java连接数据库基类

package com.csair.cbs.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据库操作(增、删、查、改)基类
 * @class DataBaseUtils
 * @description 
 * @author 李智慧
 * @copyRight copyright(c) 2011 广东南航易网通电子商务有限公司,Rights Reserved
 * @time Nov 17, 2011 3:23:00 PM
 */
public class DataBaseUtils {
	private final String authorName = "李智慧";
	private final String functionName = "操作数据库基类";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DataBaseUtils dataBaseUtils = new DataBaseUtils();
		/** 新增 **/
//		String sql = "select BANKCODE,PAYSTYLECODE,BANKNAME,ABLED,CURRENTSTYLE from BANKMANAGE where ABLED = ? and PAYSTYLECODE = ?";
	String sql = "select PAYCODE,PAYNAME from PAYMANAGE";
//		List list = new ArrayList<String>();
//		list.add("1");
//		list.add("angel");
//		int getColumnLength = 5;
//		List list2 = dataBaseUtils.getRecord(sql, list, null, getColumnLength);
		/** 新增 **/
		
		/** 修改 **/
//		String sql = "update BANKMANAGE set DISCRIBE = ? ,ABLED = ? where BANKCODE = ? and PAYSTYLECODE = ?";
//		List list = new ArrayList<String>();
//		list.add("银联电话支付-招商银行2");
//		list.add("0");
//		list.add("cmb");
//		list.add("tel");
		/** 修改 **/
		
		/** 新增 **/
//		String sql = " insert into PAYMANAGE(PAYCODE,PAYNAME,ABLED,LOGO,DISCRIBE) values(?,?,?,?,?) ";
//		List list = new ArrayList<String>();
//		list.add("aaa");
//		list.add("bbb");
//		list.add("1");
//		list.add("11");
//		list.add("23434");
		/** 新增 **/
		
		/** 删除 **/
		String sql = " delete from PAYMANAGE where PAYNAME = ?";
		List list = new ArrayList<String>();
		list.add("bbb");
		/** 删除 **/
		dataBaseUtils.addUpdateDeleteRecord(sql, list, null);
//		for(int i=0;i<list2.size();i++){
//			String[] strings = (String[]) list2.get(i);
//			for(int j=0;j<strings.length;j++){
//				System.out.print(strings[j]+"\t");
//			}
//			System.out.println();
//		}
		
	}
	
	/**
	 * 打印到log日志,好排查错误,更快时间解决问题
	 * 
	 * @param className
	 *            类名称
	 * @param methodName
	 *            方法名称
	 * @param msg
	 *            错误原因
	 * @param author
	 *            开发人
	 * @param functionName
	 *            功能模块名称
	 */
	public void print(String className, String methodName, String msg,
			String author, String functionName) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String dateTime = simpleDateFormat.format(date);
		System.out.println("###程序执行出现错误【"+dateTime+"】,错误在【" + className + "】类,【" + methodName
				+ "】方法,开发者【" + author + "】,对应开发模块【" + functionName
				+ "】:具体错误原因:" + msg+"###");
	}
	
	/**
	 * 新增记录
	 * @param sql
	 * @param list
	 * @param connection
	 * @return
	 */
	public boolean addUpdateDeleteRecord(String sql,List list,Connection connection){
		boolean result = false;
		int flag = 1;//记录有没有带connection对象,默认有带connection
		PreparedStatement preparedStatement = null;
		try{
			if(connection == null){
				connection = JDBC_Connection.getConnection();
				flag = 0;//没有带connection对象
			}
			preparedStatement = connection.prepareStatement(sql);
			if(list!=null){
				for(int i=0;i<list.size();i++){
					preparedStatement.setString(i+1, list.get(i).toString());
				}
			}
			preparedStatement.executeUpdate();
			result = true;
		}catch (Exception e) {
			print("DataBaseUtils","addUpdateDeleteRecord",e.getMessage().toString(),authorName,functionName);
		}finally{
			if(flag == 0){
				//如果没有带connection 要断开connection 的连接
				JDBC_Connection.release(null, preparedStatement, connection);
			}
			else {
				JDBC_Connection.release(null, preparedStatement, null);
			}
		}
		return result;
	}
	
	/**
	 * 获取表中的值
	 * @param sql
	 * @param list
	 * @param connection
	 * @param getColumnLength
	 * @return
	 */
	public List getRecord(String sql,List list,Connection connection,int getColumnLength){
		List<String[]> list2 = new ArrayList<String[]>();
		int flag = 1;//记录有没有带connection对象,默认有带connection
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			if(connection == null){
				connection = JDBC_Connection.getConnection();
				flag = 0;//没有带connection对象
			}
			preparedStatement = connection.prepareStatement(sql);
			if(list!=null){
				for(int i=0;i<list.size();i++){
					preparedStatement.setString(i+1, list.get(i).toString());
				}
			}
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()){
				String[] strings = new String[getColumnLength];
				for(int j=0;j<getColumnLength;j++){
					strings[j] = resultSet.getString(j+1);
				}
				list2.add(strings);
			}
		}catch (Exception e) {
			print("DataBaseUtils","addUpdateDeleteRecord",e.getMessage().toString(),authorName,functionName);
		}finally{
			if(flag == 0){
				//如果没有带connection 要断开connection 的连接
				JDBC_Connection.release(resultSet, preparedStatement, connection);
			}
			else {
				JDBC_Connection.release(resultSet, preparedStatement, null);
			}
		}
		return list2;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值