1 package edu.must.uh.util; 2 3 import java.beans.BeanInfo; 4 import java.beans.Introspector; 5 import java.beans.PropertyDescriptor; 6 import java.lang.reflect.Method; 7 import java.sql.Connection; 8 import java.sql.SQLException; 9 import java.util.Date; 10 import java.util.List; 11 import java.util.Map; 12 import org.apache.commons.dbutils.DbUtils; 13 import org.apache.commons.dbutils.QueryRunner; 14 import org.apache.commons.dbutils.handlers.BeanListHandler; 15 import org.apache.commons.dbutils.handlers.MapListHandler; 16 import org.apache.log4j.Logger; 17 18 public class MySqlUtils { 19 20 private static Logger logger = Logger.getLogger(MySqlUtils.class); 21 22 public static <T> int save(String table,T bean) throws Exception { 23 24 String sql= getSaveSQL(table,bean); 25 System.out.println(sql); 26 logger.info(sql); 27 QueryRunner qr = new QueryRunner(); 28 //调用QueryRunner方法update 29 Connection conn = MysqlManager.getConnection(); 30 int row = qr.update(conn, sql); 31 System.out.printf("已经有[%d]发生了改变",row); 32 DbUtils.closeQuietly(conn); 33 return row; 34 } 35 36 37 public static int disOrEnAble(String table,String enabledMark,String keyFields,String keyValue)throws SQLException{ 38 String sql= "UPDATE "+ table+ "SET EnabledMark = "+enabledMark+" WHERE "+ keyFields + "= '"+keyValue+"'"; 39 System.out.println(sql); 40 logger.info(sql); 41 QueryRunner qr = new QueryRunner(); 42 Connection conn = MysqlManager.getConnection(); 43 int row = qr.update(conn, sql); 44 System.out.printf("已经有[%d]发生了改变",row); 45 DbUtils.closeQuietly(conn); 46 return row; 47 } 48 49 50 51 public static int delete(String table,String keyFields,String keyValue)throws SQLException{ 52 String sql= "UPDATE "+ table+ "SET DeleteMark = 1 WHERE "+ keyFields + "= '"+keyValue+"'"; 53 System.out.println(sql); 54 logger.info(sql); 55 QueryRunner qr = new QueryRunner(); 56 Connection conn = MysqlManager.getConnection(); 57 int row = qr.update(conn, sql); 58 System.out.printf("已经有[%d]发生了改变",row); 59 DbUtils.closeQuietly(conn); 60 return row; 61 } 62 63 public static int deleteTrue(String table,String keyFields,String keyValue)throws SQLException{ 64 String sql= "DELETE FROM "+ table+ " WHERE "+ keyFields + "= '"+keyValue+"'"; 65 System.out.println(sql); 66 logger.info(sql); 67 QueryRunner qr = new QueryRunner(); 68 Connection conn = MysqlManager.getConnection(); 69 int row = qr.update(conn, sql); 70 System.out.printf("已经有[%d]发生了改变",row); 71 DbUtils.closeQuietly(conn); 72 return row; 73 } 74 75 private static String getUpdateSQL(String table,Object obj) throws Exception { 76 StringBuilder sqlString = new StringBuilder(); 77 BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); 78 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 79 for (PropertyDescriptor property : propertyDescriptors) { 80 String fieldName = property.getName(); 81 String key = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); 82 Method getter = property.getReadMethod(); 83 Object valueObject = getter.invoke(obj); 84 if(key.toUpperCase().equals("CLASS")) { 85 continue; 86 } 87 if(valueObject !=null) { 88 if(property.getPropertyType() == java.util.Date.class) { 89 valueObject = ToolsOfDate.toYYYYMMDDHHMISS((Date)valueObject); 90 } 91 sqlString.append( key+" = '"+ valueObject +"',"); 92 } 93 } 94 return sqlString.deleteCharAt(sqlString.length()-1).toString() ; 95 } 96 /* 97 * 定义方法,使用QueryRunner类的方法update将数据表的数据修改 98 */ 99 public static <T> int update(String table,T bean,String keyFields,String keyValue)throws Exception{ 100 101 String updateFieldString = getUpdateSQL(table,bean); 102 String sql= "UPDATE "+ table+ " SET "+updateFieldString+" WHERE "+ keyFields + "= '"+keyValue+"'"; 103 System.out.println(sql); 104 logger.info(sql); 105 //创建QueryRunner类对象 106 QueryRunner qr = new QueryRunner(); 107 //调用QueryRunner方法update 108 Connection conn = MysqlManager.getConnection(); 109 int row = qr.update(conn, sql); 110 System.out.printf("已经有[%d]发生了改变",row); 111 DbUtils.closeQuietly(conn); 112 return row; 113 } 114 115 public static Integer update(String sql) throws Exception { 116 QueryRunner qr = new QueryRunner(); 117 //调用QueryRunner方法update 118 Connection conn = MysqlManager.getConnection(); 119 int row = qr.update(conn, sql); 120 System.out.printf("已经有[%d]发生了改变",row); 121 DbUtils.closeQuietly(conn); 122 return row; 123 124 } 125 126 127 //INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) 128 private static String getSaveSQL(String table,Object obj) throws Exception { 129 StringBuilder sqlString = new StringBuilder(); 130 StringBuilder keyString = new StringBuilder(); 131 keyString.append(" ( "); 132 StringBuilder valString = new StringBuilder(); 133 valString.append(" ( "); 134 sqlString.append("INSERT INTO " + table); 135 BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); 136 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 137 for (PropertyDescriptor property : propertyDescriptors) { 138 String fieldName = property.getName(); 139 String key = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); 140 Method getter = property.getReadMethod(); 141 Object valueObject = getter.invoke(obj); 142 if(key.toUpperCase().equals("CLASS")) { 143 continue; 144 } 145 if(valueObject !=null) { 146 keyString.append( key+","); 147 if(property.getPropertyType() == java.util.Date.class) { 148 valueObject = ToolsOfDate.toYYYYMMDDHHMISS((Date)valueObject); 149 } 150 valString.append("'"+ valueObject +"',"); 151 } 152 } 153 return sqlString.toString() + keyString.deleteCharAt(keyString.length()-1).append(")").toString()+"VALUES"+ valString.deleteCharAt(valString.length()-1).append(")").toString(); 154 } 155 156 157 /* 158 * 结果集第八种处理方法,MapListHandler 159 * 将结果集每一行存储到Map集合,键:列名,值:数据 160 * Map集合过多,存储到List集合 161 */ 162 public static List<Map<String,Object>> getlist(String sql)throws SQLException{ 163 QueryRunner qr = new QueryRunner(); 164 System.out.printf(sql); 165 logger.info(sql); 166 //调用方法query,传递结果集实现类MapListHandler 167 //返回值List集合, 存储的是Map集合 168 Connection conn = MysqlManager.getConnection(); 169 List<Map<String,Object>> list = qr.query(conn, sql, new MapListHandler()); 170 DbUtils.closeQuietly(conn); 171 return list; 172 173 } 174 175 public static <T> List<T> getlist(String sql, Class<T> oclass) throws Exception { 176 177 QueryRunner qr = new QueryRunner(); 178 logger.info(sql); 179 T beanT = oclass.newInstance(); 180 Connection conn = MysqlManager.getConnection(); 181 List<T> list = qr.query(sql, new BeanListHandler<T>((Class<? extends T>) beanT.getClass())); 182 DbUtils.closeQuietly(conn); 183 return list; 184 } 185 186 187 }