116 lines
2.6 KiB
Java
116 lines
2.6 KiB
Java
package com.tenwa.reckon.util;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.ResultSetMetaData;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import com.amarsoft.app.als.sys.SystemConfig;
|
|
import com.amarsoft.are.ARE;
|
|
import com.amarsoft.are.jbo.JBOException;
|
|
import com.amarsoft.are.jbo.JBOTransaction;
|
|
|
|
|
|
|
|
|
|
/**
|
|
*
|
|
* @author zhangc
|
|
*/
|
|
public class Conn {
|
|
|
|
|
|
private Connection conn ;
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
public Conn(JBOTransaction tx){
|
|
try {
|
|
this.conn=com.amarsoft.awe.util.Transaction.createTransaction(tx).getConnection();
|
|
} catch (JBOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (SQLException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 执行查询
|
|
*
|
|
* @param sql
|
|
* 查询的SQL语句
|
|
* @param params
|
|
* 参数
|
|
* @return
|
|
* @throws SQLException
|
|
* @throws Exception
|
|
*/
|
|
public List<Map<String, String>> executeQuery(String sql,String ... params) throws SQLException {
|
|
|
|
List<Map<String, String>> result;
|
|
|
|
result = new ArrayList<Map<String, String>>();
|
|
PreparedStatement ps = this.conn.prepareStatement(sql);
|
|
if(params != null && params.length > 0){
|
|
for(int i = 0 ; i < params.length ; i++){
|
|
ps.setString(i+1, params[i]);
|
|
}
|
|
}
|
|
ResultSet rs = ps.executeQuery();
|
|
ResultSetMetaData rsmd = rs.getMetaData();
|
|
int colunCount = rsmd.getColumnCount();
|
|
while(rs.next()){
|
|
Map<String, String> value = new HashMap<String,String>();
|
|
for(int i = 1 ; i <= colunCount ; i++){
|
|
value.put(rsmd.getColumnName(i).toLowerCase(), rs.getString(rsmd.getColumnName(i).toLowerCase()));
|
|
}
|
|
result.add(value);
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 执行的修改语句
|
|
*
|
|
* @param sql
|
|
* update或delete的SQL语句
|
|
* @param mesage
|
|
* 输出SQL语句的前置消息字符
|
|
* @return
|
|
* @throws SQLException
|
|
* @throws Exception
|
|
*/
|
|
public Boolean executeUpdate(String sql, String... params) throws SQLException {
|
|
|
|
PreparedStatement ps = this.conn.prepareStatement(sql);
|
|
if(params != null && params.length > 0){
|
|
for(int i = 0 ; i < params.length ; i++){
|
|
ps.setString(i+1, params[i]);
|
|
}
|
|
}
|
|
return ps.execute();
|
|
|
|
}
|
|
|
|
public Boolean executeUpdate(String sql, List<String> params) throws SQLException {
|
|
|
|
PreparedStatement ps = this.conn.prepareStatement(sql);
|
|
if(params != null && params.size() > 0){
|
|
for(int i = 0 ; i < params.size() ; i++){
|
|
ps.setString(i+1, params.get(i));
|
|
}
|
|
}
|
|
return ps.execute();
|
|
|
|
}
|
|
}
|