📄 dbexecute.java
字号:
else {
try {
outstr = new String(str.getBytes("GB2312"), "ISO8859_1");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
if (str == null || str == "") {
outstr = "";
}
return outstr;
}
public String convetStrToDb(String instr) {
String outstr = "";
if (instr == null || instr == "") {
outstr = "";
}
else {
try {
outstr = new String(instr.getBytes("ISO8859_1"), "GB2312");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
if (instr == null || instr == "") {
outstr = "";
}
return outstr;
}
public ArrayList replaceQueryParam() {
if (this.paramList.isEmpty())
return null;
ArrayList paramOrderArray = new ArrayList();
int ivar = 0;
String[] sqlString = this.strQuery.trim().split(" ");
for (int i = 0; i < sqlString.length; i++) {
if (sqlString[i].length() > 0) {
String[] sqlStringT = sqlString[i].split(",");
for (int g = 0; g < sqlStringT.length; g++) {
if (sqlStringT[g].length() > 0) {
String[] varString = sqlStringT[g].split(":");
for (int j = 0; j < varString.length; j++) {
if (varString[j].length() > 0) {
if (varString[j].substring(0, 1).equals("V")) {
ivar = ivar + 1;
for (Iterator it = this.paramList.iterator(); it.hasNext();) {
HashMap varList = (HashMap) it.next();
HashMap paramOrderMap = new HashMap();
if (varList.get("paramName") != null) {
String paraName = varString[j].replaceAll("\\)", "");
if (varList.get("paramName").toString().equalsIgnoreCase(":" + paraName)) {
int sin = this.strQuery.indexOf(varList.get("paramName").toString()) + varList.get("paramName").toString().length();
if (this.strQuery.length() > sin) {
if (this.strQuery.substring(sin, sin + 1).equalsIgnoreCase(",") || this.strQuery.substring(sin, sin + 1).equalsIgnoreCase(" ") || this.strQuery.substring(sin, sin + 1).equalsIgnoreCase(")") || this.strQuery.substring(sin, sin + 1).equalsIgnoreCase("")) {
this.strQuery = this.strQuery.replaceFirst(varList.get("paramName").toString(), "?");
paramOrderMap.put("paramValue", varList.get("paramValue").toString());
paramOrderMap.put("order", new Integer(ivar));
paramOrderArray.add(paramOrderMap);
}
}
else {
this.strQuery = this.strQuery.replaceAll(varList.get("paramName").toString(), "?");
paramOrderMap.put("paramValue", varList.get("paramValue").toString());
paramOrderMap.put("order", new Integer(ivar));
paramOrderArray.add(paramOrderMap);
}
}
}
}
}
}
}
}
}
}
}
return paramOrderArray;
}
public PreparedStatement setQueryParam(PreparedStatement stm, ArrayList inparamOrderArray) {
if (inparamOrderArray == null || inparamOrderArray.isEmpty())
return stm;
for (Iterator it = inparamOrderArray.iterator(); it.hasNext();) {
HashMap varList = (HashMap) it.next();
int iOrder = new Integer(varList.get("order").toString()).intValue();
try {
if (varList.get("paramValue").toString().length() > 1000) {
stm.setCharacterStream(iOrder, new StringReader(varList.get("paramValue").toString()), varList.get("paramValue").toString().length());
}
else {
stm.setObject(iOrder, varList.get("paramValue"));
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
return stm;
}
public String getQuery() {
QueryXML xml = new QueryXML();
String basepath = Thread.currentThread().getContextClassLoader().getResource("").toString();
String fieldspath = basepath.substring(0, basepath.length() - 8) + "SaasQuery.xml";
xml.setXmlFilePath(fieldspath);
xml.setTableName(this.strTable);
xml.setQueryName(this.strQuery);
String sql = xml.getQueryTableXML();
return sql;
}
// 分页查询
public ArrayList selBizQuery(int strStart, int count) {
Dbcommit tempConnect = new Dbcommit();
tempConnect.getConnect();
Connection tempConnectHandle = tempConnect.getConnectionHandle();
PreparedStatement stm = null;
ResultSet rst = null;
//int iCount = count + strStart - 1;//liuy
ResultSetMetaData rstMetaDate = null;
ArrayList dataArray = new ArrayList();
int columnCount = 0;
try {
this.strQuery = this.getQuery();
ArrayList paraOrder = this.replaceQueryParam();
if (stm != null)
stm.clearParameters();
// this.strQuery ="select * from (select a.*, rownum rn from ("+this.strQuery+" ) a where rownum <= "+iCount+")where rn >= "+strStart;
this.strQuery = this.strQuery + " limit " + strStart + "," +count;// iCount;//liuy
stm = tempConnectHandle.prepareStatement(this.strQuery);
this.setQueryParam(stm, paraOrder);
rst = stm.executeQuery();
rstMetaDate = rst.getMetaData();
columnCount = rstMetaDate.getColumnCount();
}
catch (SQLException e) {
throw new RuntimeException(e);
}
try {
while (rst.next()) {
HashMap row = new HashMap();
for (int i = 1; i <= columnCount; i++) {
String strFieldName = rstMetaDate.getColumnName(i).toLowerCase();
if (rstMetaDate.getColumnTypeName(i) == "VARCHAR2" || rstMetaDate.getColumnTypeName(i) == "CHAR" || rstMetaDate.getColumnTypeName(i) == "VARCHAR" || rstMetaDate.getColumnTypeName(i) == "DATE") {
if (rst.getObject(i) != null) {
row.put(strFieldName, rst.getObject(i).toString());
}
else {
row.put(strFieldName, rst.getObject(i));
}
}
else if (rstMetaDate.getColumnTypeName(i) == "LONG") {
if (rst.getCharacterStream(i) != null) {
java.io.Reader reader = (java.io.Reader) rst.getCharacterStream(i);
String strtmp = "";
try {
strtmp = (String) getLargerString(reader);
row.put(strFieldName, strtmp);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
else {
row.put(strFieldName, "");
}
}
else {
row.put(strFieldName, rst.getObject(i));
}
}
dataArray.add(row);
}
}
catch (SQLException e) {
tempConnect.closeConn();
throw new RuntimeException(e);
}
tempConnect.closeConn();
return dataArray;
}
public static String getLargerString(java.io.Reader reader) throws Exception {
char[] content = new char[1024000];
char[] buffer = new char[1024];
int len = 0;
int off = 0;
int contentLen = 1024000;
while (true) {
len = reader.read(buffer);
if (len == -1)
break;
if (off + len > contentLen) {
char[] tmp = new char[contentLen + 1024000];
System.arraycopy(content, 0, tmp, 0, off);
content = tmp;
contentLen = contentLen + 1024000;
}
System.arraycopy(buffer, 0, content, off, len);
off = off + len;
}
return new String(content, 0, off);
}
public Object getFromBuffer(String strObj, Buffers inbuffers) {
Object objDao = null;
/**
* if (inbuffers == null) return null; if (strObj == "") return null; Field[] fields; ArrayList inbuffer = new ArrayList(); inbuffer = inbuffers.BuffersToArrayList(inbuffers); try { Class daoClass = Class.forName(strObj.trim()); objDao = (Object)daoClass.newInstance(); fields = objDao.getClass().getDeclaredFields(); } catch (ClassNotFoundException e) { throw new RuntimeException("调用类未找到:"+e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch
* (IllegalAccessException e) { throw new RuntimeException(e); } for (int i=0;i<=fields.length-1;i++) { log.LOG_INFO("fieldsfieldsfields"+fields[i].getName()); for (Iterator it = inbuffer.iterator(); it.hasNext();) { HashMap map =(HashMap)it.next(); try { if (map.get("name").toString().toUpperCase().equalsIgnoreCase(fields[i].getName().toUpperCase())) { if (map.get("value") != null) { PropertyUtils.setProperty(objDao, fields[i].getName(), map.get("value")); } else { if
* (map.get("type").toString().equalsIgnoreCase("string")) { PropertyUtils.setProperty(objDao, fields[i].getName(), ""); } else if (map.get("type").toString().equalsIgnoreCase("int")) { PropertyUtils.setProperty(objDao, fields[i].getName(), -1); } } break; } } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } }
*/
return objDao;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -