jdbcutil.java
来自「用jbuilder写的源程序」· Java 代码 · 共 116 行
JAVA
116 行
/*
* Copyright 2003-2006 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jdon.model.query;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* @author <a href="mailto:banqiao@jdon.com">banq </a>
*
*/
public class JdbcUtil {
private final static Logger logger = Logger.getLogger(JdbcUtil.class);
/**
* queryParam type only support String Integer Float or Long Double Bye Short
* if you need operate other types, you must use JDBC directly!
*/
public void setQueryParams(Collection queryParams, PreparedStatement ps) throws Exception {
if ((queryParams == null) || (queryParams.size() == 0))
return;
int i = 1;
Object key = null;
Iterator iter = queryParams.iterator();
while (iter.hasNext()) {
key = iter.next();
if (key != null) {
convertType(i, key, ps);
logger.debug("[JdonFramework] parameter " + i + " = " + key.toString());
} else {
logger.warn("[JdonFramework] parameter " + i + " is null");
ps.setString(i, "");
}
i++;
}
}
private void convertType(int i, Object key, PreparedStatement ps) {
try {
if (key instanceof java.lang.String) {
String keyStrs = (String) key;
ps.setString(i, keyStrs);
} else if (key instanceof Integer) {
ps.setInt(i, ((Integer) key).intValue());
} else if (key instanceof Float) {
ps.setFloat(i, ((Float) key).floatValue());
} else if (key instanceof Long) {
ps.setLong(i, ((Long) key).longValue());
} else if (key instanceof Double) {
ps.setDouble(i, ((Double) key).doubleValue());
} else if (key instanceof Byte) {
ps.setByte(i, ((Byte) key).byteValue());
} else if (key instanceof Short) {
ps.setShort(i, ((Short) key).shortValue());
} else {
ps.setObject(i, key);
logger.debug("[JdonFramework]warniing: Type =" + key.getClass().getName() + " isn't be converted!");
}
} catch (SQLException e) {
logger.error("[JdonFramework]setQueryParams error " + e + "in parameter order=" + i + " its value=" + key);
} catch (Exception e) {
logger.error(e);
}
}
/**
* return a List
* in the List, every object is a map,
* by database column name, we can get the its result from map
*
*
* @param rs ResultSet
* @return List
* @throws Exception
*/
public List extract(ResultSet rs) throws Exception {
ResultSetMetaData meta = rs.getMetaData();
int count = meta.getColumnCount();
List ret = new ArrayList();
while (rs.next()) {
Map map = new LinkedHashMap(count);
for (int i = 1; i <= count; i++) {
map.put(meta.getColumnName(i), rs.getObject(i));
}
ret.add(map);
}
return ret;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?