package xz.xsr.pub.uip.util;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import xz.xsr.pub.uip.modal.parser.Field;
import xz.xsr.pub.uip.modal.parser.UIConfiguration;
import xz.xsr.pub.util.Parameter;
public class DBUtil {
private static String DB_ADAPTEE_IMPL_CLASS = null;
public static synchronized Connection getConn(Parameter parameter)
throws Exception {
String debugMode = parameter.getSessionParam(UIConstant.UI_DEBUG_MODE);
if (DB_ADAPTEE_IMPL_CLASS == null||"1".equals(debugMode)) {
String uiConfPath = parameter
.getSessionParam(UIConstant.UI_CONF_PATH);
String xml = UIUtil.getFileContent(uiConfPath
+ "UIConfiguration.xml");
UIConfiguration obj = UIConfiguration.fromXml(xml);
DB_ADAPTEE_IMPL_CLASS = obj.getDbAdapter().getImplclass();
}
Class cl = Class.forName(DB_ADAPTEE_IMPL_CLASS);
DBAdaptee dbAdaptee = (DBAdaptee) cl.newInstance();
Connection conn = dbAdaptee.getDBConnection();
return conn;
}
public HashMap getPagingDataBySQL(ResultSet rs, Parameter parameter)
throws Exception {
int pageIndex = parameter.getRequestParam("pageIndex") != null ? Integer
.parseInt(parameter.getRequestParam("pageIndex"))
: 0;
int pageSize = parameter.getRequestParam("pageSize") != null ? Integer
.parseInt(parameter.getRequestParam("pageSize")) : 0;
rs.last();
int rowCount = rs.getRow();
rs.first();
rs.previous();
for (int i = 0; pageSize != -1 && i < pageIndex * pageSize; i++) {
rs.next();
}
ArrayList rList = resultSetToList(rs, pageSize);
HashMap result = new HashMap();
result.put("data", rList);
result.put("total", rowCount);
return result;
}
public ArrayList getDataListBySQL(ResultSet rs) throws Exception {
int pageSize = -1;//标识查所有数据
ArrayList rList = resultSetToList(rs, pageSize);
return rList;
}
public void executeSQL(Connection conn, String sql) throws Exception {
Statement stmt = conn.createStatement();
System.out.println("executeSQL sql="+sql);
try {
stmt.execute(sql);
} catch (SQLException e) {
throw new Exception("SQL无效:"+e.getMessage()+"<br/>SQL:"+sql);
}
stmt.close();
}
public ResultSet executeQuery(Connection conn, String sql) throws Exception
{
System.out.println("executeQuery sql="+sql);
ResultSet rs = null;
try {
rs = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
} catch (SQLException e) {
throw new Exception("SQL无效:"+e.getMessage()+"<br/>SQL:"+sql);
}
return rs;
}
public FuncResult executeProcedure(Connection conn, String procName, ArrayList args) throws Exception {
FuncResult rs = new FuncResult();
String sql = "call " + procName + "(";
int argsLength = args.size();
for (int i = 0; i < argsLength; i++) {
sql += "?,";
}
sql += "?,?)";
System.out.println("executeProcedure sql="+sql);
CallableStatement cs = conn.prepareCall(sql);
String param = "";
for (int i = 0; i < argsLength; i++) {
cs.setString(i + 1, ((String[])args.get(i))[1]);
param += ","+ (((String[])args.get(i))[0]) + "="+ (((String[])args.get(i))[1]);
}
if(param.length()>0){
param = param.substring(1);
}
cs.registerOutParameter(argsLength + 1, oracle.jdbc.OracleTypes.INTEGER);
cs.registerOutParameter(argsLength + 2, oracle.jdbc.OracleTypes.VARCHAR);
System.out.println("参数:"+JSONUtil.Encode(param));
try {
cs.execute();
} catch (SQLException e) {
throw new Exception("调用存储过程失败:"+e.getMessage()+"<br/>存储过程:"+ sql+",参数:"+param);
}
rs.setFlag(cs.getInt(argsLength + 1)+"");
rs.setMsg(cs.getString(argsLength + 2) == null ? "" : cs.getString(argsLength + 2));
cs.close();
return rs;
}
public void insert(Connection conn, String tableName,HashMap valueMap2) throws SQLException{
StringBuffer sql1 = new StringBuffer("insert into "+tableName+"(");
StringBuffer sql2 = new StringBuffer(" values(");
Iterator it = valueMap2.keySet().iterator();
Vector vm = new Vector();
while(it.hasNext()){
String key = (String)it.next();
sql1.append(key).append(",");
sql2.append("?,");
vm.add(valueMap2.get(key));
}
String sql = sql1.substring(0,sql1.length()-1)+")"+sql2.subSequence(0, sql2.length()-1)+")";
System.out.println("insert sql="+sql);
System.out.println("参数:"+JSONUtil.Encode(valueMap2));
PreparedStatement psd = conn.prepareStatement(sql);
fillStatement(psd, vm);
try{
psd.execute();
}catch(SQLException e){
throw new SQLException("数据插入失败:"+e.getMessage()+"<br/>sql="+sql+",参数:"+JSONUtil.Encode(valueMap2));
}
psd.close();
}
public void update(Connection conn, String tableName,HashMap valueMap,HashMap whereMap) throws SQLException{
StringBuffer sql1 = new StringBuffer("update "+tableName+" set ");
Iterator it = valueMap.keySet().iterator();
Vector v = new Vector();
while(it.hasNext()){
String key = (String)it.next();
sql1.append(key).append("=?,");
v.add(valueMap.get(key));
}
sql1 = new StringBuffer(sql1.substring(0, sql1.length()-1)).append(" where 1=1 ");
Iterator it2 = whereMap.keySet().iterator();
while(it2.hasNext()){
String key = (String)it2.next();
sql1.append(" and ").append(key).append("=?");
v.add(whereMap.get(key));
}
String sql = sql1.toString();
System.out.println("update sql="+sql);
System.out.println("参数:"+JSONUtil.Encode(valueMap));
PreparedStatement psd = conn.prepareStatement(sql);
fillStatement(psd, v);
try{
psd.execute();
}catch(SQLException e){
throw new SQLException("数据更新失败:"+e.getMessage()+"<br/>sql="+sql+",参数:"+JSONUtil.Encode(valueMap));
}
psd.close();
}
public ArrayList<Field> getAllFieldByTabOrViewName(Connection conn, String objName) throws SQLException{
ResultSet rs = conn.createStatement().executeQuery( "SELECT COLUMN_NAME, DATA_TYPE\n" +
" FROM DBA_TAB_COLUMNS\n" +
" WHERE TABLE_NAME = '"+objName+"'");
ArrayList fields = new ArrayList();
while(rs.next()){
Field f = new Field();
f.setFieldName(rs.getString("COLUMN_NAME"));
f.setFieldType(rs.getString("DATA_TYPE"));
fields.add(f);
}
rs.close();
return fields;
}
public void delete(Connection conn,String tableName,HashMap whereMap) throws SQLException{
StringBuffer sql1 = new StringBuffer("delete from "+tableName+" where 1=1 ");
Vector v = new Vector();
Iterator it2 = whereMap.keySet().iterator();
while(it2.hasNext()){
String key = (String)it2.next();
sql1.append(" and ").append(key).append("=?");
v.add(whereMap.get(key));
}
String sql = sql1.toString();
System.out.println("delete sql="+sql);
System.out.println("参数:"+JSONUtil.Encode(whereMap));
PreparedStatement psd = conn.prepareStatement(sql);
fillStatement(psd, v);
psd.execute();
psd.close();
}
public void fillStatement(PreparedStatement psd, Vector params) throws SQLException {
for(int index=0;index<params.size();index++){
Object objValue = params.get(index);
if (objValue instanceof String) {
String paraValue = (String) objValue;
psd.setString(index + 1, paraValue);
} else if (objVa
评论1
最新资源