锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.sql.*;
public class GeneralDao {
private Connection connection = null;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
/**
* 鎵ц鎻掑叆錛屾洿鏂幫紝鍒犻櫎鎿嶄綔錛岃繑鍥炲間負(fù)褰卞搷璁板綍鏁般?BR> * @param sql
* @param parameter
*/
public int executeUpdate(String sql, Object parameter) {
List result = new ArrayList();
//鎸夋ā寮忓尮閰嶈漿鎹ql璇彞
Pattern pattern = Pattern.compile("#[a-zA-Z]+#"); //妯″紡鍖歸厤
Matcher matcher = pattern.matcher(sql);
String formatSql = matcher.replaceAll("?");
//榪炴帴鏁版嵁婧?BR> PreparedStatement st = null;
try {
st = connection.prepareStatement(formatSql);
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//娣誨姞棰勫鐞嗗弬鏁?BR> int i = 1;
while (matcher.find()) {
String field = matcher.group();
String formatField = field.substring(1, field.length() - 1);
try {
st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
try {
return st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return 0;
}
/**
* 鏌ヨ絎琾ageCount欏甸潰錛堟瘡涓〉闈㈢殑璁板綍鏁頒負(fù)pageSize錛夌殑緇撴灉
* @param sql sql鏌ヨ
* @param parameter 鍙傛暟瀵硅薄
* @param pageSize 欏甸潰緇撴灉闆嗗ぇ灝?BR> * @param pageCount 欏甸潰鏁?BR> * @param resultClass 緇撴灉綾?BR> * @return List
*/
public List executeQuerys(String sql, Object parameter, int pageSize, int pageCount, Class resultClass){
List result = new ArrayList();
//鎸夋ā寮忓尮閰嶈漿鎹ql璇彞
Pattern pattern = Pattern.compile("#[a-zA-Z]+#"); //妯″紡鍖歸厤
Matcher matcher = pattern.matcher(sql);
String formatSql = matcher.replaceAll("?");
//榪炴帴鏁版嵁婧?BR> PreparedStatement st = null;
try {
//璁劇疆棰勫鐞嗙姸鎬佸弬鏁幫紝婊氬姩錛屽彧璇匯?BR> st = connection.prepareStatement(formatSql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//娣誨姞棰勫鐞嗗弬鏁?BR> int i = 1;
while (matcher.find()) {
String field = matcher.group();
String formatField = field.substring(1, field.length() - 1);
try {
st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
ResultSet rs = null;
try {
rs = st.executeQuery();
//鑾峰彇鎬昏褰曟暟
rs.last();
int totalCount = rs.getRow();
//褰撳墠欏甸潰絎竴鏉¤褰曚綅緗?BR> int curPagePosition = (pageCount -1)*pageSize + 1;
if (totalCount < curPagePosition) {
return null;
}
rs.beforeFirst();
rs.absolute(curPagePosition);
//灝佽鍊煎璞?BR> int k = 0;
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next() && k < pageSize) {
Object o = resultClass.newInstance();
for (int j = 1; j <= cols; j++) {
String name = rsmd.getColumnName(j);
Object value = rs.getObject(j);//浣滈氱敤綾誨瀷澶勭悊,榪欐牱row涓殑綾誨瀷閮芥槸Object鍨嬬殑銆?BR> String voName = DataBaseUtil.toInValueName(name);
DataBaseUtil.executeSetMethod(voName, value, o);
}
result.add(o);
k++; //鑾峰彇鏁板姞1
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InstantiationException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return result;
}
/**
* 鏌ヨ鎵鏈夌邯褰?BR> * @param sql
* @param parameter
* @param resultClass
* @return List
*/
public List executeQuerys(String sql, Object parameter, Class resultClass){
//榛樿涓烘墍鏈夎褰?BR> return this.executeQuerys(sql, parameter, Integer.MAX_VALUE, 1, resultClass);
}
/**
* 鏌ヨ鍗曚釜璁板綍
* @param sql
* @param parameter
* @param resultClass
* @return Object
*/
public Object executeQuery(String sql, Object parameter, Class resultClass){
List list = executeQuerys(sql, parameter,resultClass);
if (list == null || list.size() == 0) {
return null;
}
return list.get(0);
}
}