在使用jdbc操作数据库中,最常用的操作便是对数据库实现增,删,改,查四种基本的操作,在一般的java操作模式下,常用的是使用一个数据对象(就是和数据库中表列对应的数据结构,只用set和get方法),但是,在写的过程中,使用PreparedStatement时,就会出现,对象的参数之间的对应耦合太强还要写太多的set参数方法,很繁琐。,所以我就为此写了一个封装类,其实很简单,就是使用一个对象数组保存不确定的?参数。具体的看代码:
/** *
/** *
Title:
*
Description:对数据库实际操作的封装类
*
Copyright: Copyright (c) 2004
*
Company:
* @author 陈少坤 qq:29189725
* @version 1.0 DBOperator */
import java.sql.*;
public class DBOperator { private DBConnectionManager db = null; private java.sql.PreparedStatement ps = null; private java.sql.ResultSet rs=null; private Connection con = null; public DBOperator() { initialize(); } private void initialize() { db = DBConnectionManager.getInstance(); con = db.getConnection("idb"); } /** *所有对数据库操作中需要返回RestsultSet的字符串 for example select * @param sql sql字符串 * @param params * @return */ public ResultSet select(String sql, Object[] params) { rs = null; try { ps = con.prepareStatement(sql); int index = 1; if (params != null) { int n = params.length; if (n < 2 || n % 2 != 0 || (n/2)!=this.getStrNum(sql,"?")) { throw new IllegalArgumentException( "参数为奇数或者是小于2,或者是参数的个数不一致"); } for (int i = 0; i < params.length; i += 2) { params[i+1]=this.pareObjToStr(params[i+1]);//把第二个参数对象转换为字符串类型 if ( ( (String) params[i]).toLowerCase().equals("string")) { ps.setString(index++, (String) params[i + 1]); } if ( ( (String) params[i]).toLowerCase().equals("long")) { ps.setLong(index++, Long.parseLong( (String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("int")) { ps.setInt(index++, Integer.parseInt( (String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("date")) { ps.setString(index++, (String) params[i + 1]); } if ( ( (String) params[i]).toLowerCase().equals("float")) { ps.setFloat(index++, Float.parseFloat((String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("double")) { ps.setDouble(index++, Double.parseDouble((String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("image")) { ps.setBytes(index++, (byte[]) params[i + 1]); } } } rs = ps.executeQuery(); } catch (NumberFormatException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return rs; } /** *所有对数据库的更新操作 contains(insert update delete) * @param sql 带参数的sql语句 * @param params 参数数组 * @return */ public int update(String sql, Object[] params) { int num = 0; try { ps = con.prepareStatement(sql); int index = 1; if (params != null) { int n=params.length; if (n < 2 || n % 2 != 0 || (n/2)!=this.getStrNum(sql,"?")) { throw new IllegalArgumentException( "参数为奇数或者是小于2,或者是参数的个数不一致"); } for (int i = 0; i < params.length; i += 2) {params[i+1]=this.pareObjToStr(params[i+1]);//吧第二个参数对象转换为字符串类型
if ( ( (String) params[i]).toLowerCase().equals("string")) { ps.setString(index++, (String) params[i + 1]); } if ( ( (String) params[i]).toLowerCase().equals("long")) { ps.setLong(index++, Long.parseLong( (String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("int")) { ps.setInt(index++, Integer.parseInt( (String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("date")) { ps.setString(index++, (String) params[i + 1]); } if ( ( (String) params[i]).toLowerCase().equals("float")) { ps.setFloat(index++, Float.parseFloat((String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("double")) { ps.setDouble(index++, Double.parseDouble((String) params[i + 1])); } if ( ( (String) params[i]).toLowerCase().equals("image")) { ps.setBytes(index++, (byte[]) params[i + 1]); } } } num = ps.executeUpdate(); } catch (NumberFormatException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); }return num;
}
/** * 把Object类型对象按照相应的类型进行转换,返回String类型 * @param obj * @return 返回String类型 */ private String pareObjToStr(Object obj){ if(obj==null) return null; if(obj instanceof String) return obj.toString(); if(obj instanceof Integer) return ((Integer)obj).toString(); if(obj instanceof Long) return ((Long)obj).toString(); if(obj instanceof Float) return ((Float)obj).toString(); if(obj instanceof Double) return ((Double)obj).toString(); if(obj instanceof java.util.Date) return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.util.Date)obj);return obj.toString(); }
/** *统计原来的字符串中包含的实际字符串的个数 * @param sql 原来的字符串 * @param str 其中包含的字符串 * @return 原字符从串中包含的字符串的个数 */ private int getStrNum(String sql,String str) { int num = 0; int index = sql.indexOf(str); while (index != -1) { num++; index=sql.indexOf(str,index+str.length()); } return num; } /** * 释放数据库连接资源 */ public void freeCon(){ try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (con != null) { db.freeConnection("idb", con); } } catch (SQLException ex) { ex.printStackTrace(); } }public static void main(String args[]){ DBOperator dbe=new DBOperator();String sql="select *from year_sub_com where id=4"; dbe.select(sql,null); dbe.freeCon(); }}
使用很简单:所有的查询可以使用select方法,其他的增,删,改,可以使用update方法。其中的两个参数,
一个是带着?的sql语句,另一个是对应?的对象数组,数组的个数是?的2倍,应为它有类型和值。例如1:
String sql="delete from year_performance_basic_target where id=?"; DBOperator dbo=new DBOperator(); Object[] para={"long",""+id}; dbo.update(sql,para);例如2:
String sql="select *from year_stat_person where com_code=? and sum_year=?"; DBOperator dbo=new DBOperator(); Object[] para={"string",com_code,"int",""+year}; ResultSet rs=dbo.select(sql,para);在后来的开发中,我发现如果要对多个表都要实现增,删,改,操作,并且表的字段很多的话比如近百个字段,
那么在写插入sql语句和update sql语句的时候太麻烦了,很容易出错,所以我油写了一个简单的“代码生成器”
给开发中减少了大量繁琐的工作,尽管比较简单,但确实很耗用。代码我将在下一篇文章中给出。
