📄 jdbcutil.java
字号:
/*
* Copyright 2003-2005 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.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
*/
public void setQueryParams(Collection queryParams,
PreparedStatement ps) throws Exception {
if ((queryParams == null) || (queryParams.isEmpty()))
return;
Iterator iter = queryParams.iterator();
int i = 1;
while (iter.hasNext()) {
Object key = iter.next();
if (key == null) return;
if (key instanceof java.lang.String) {
String keyStrs = (String) key;
if (!keyStrs.equals(""))
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());
}
logger.debug(" parameter " + i + " is " + key.toString());
i++;
}
}
/**
* 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -