You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
5.6 KiB
151 lines
5.6 KiB
package com.fr.plugin.utils; |
|
|
|
import com.fr.base.FRContext; |
|
import com.fr.file.ConnectionConfig; |
|
import com.fr.file.DatasourceManager; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.log.FineLoggerProvider; |
|
|
|
import java.sql.*; |
|
import java.util.*; |
|
|
|
public class DbUtils { |
|
String db_name = ""; |
|
|
|
FineLoggerProvider logger = FineLoggerFactory.getLogger(); |
|
|
|
|
|
public DbUtils(String db_name) { |
|
this.db_name = db_name; |
|
} |
|
|
|
public com.fr.data.impl.Connection getDbConnect() { |
|
return ConnectionConfig.getInstance().getConnection(db_name); |
|
} |
|
|
|
public List<Map<String, Object>> select(String sql, Object... params) { |
|
logger.info("query data by sql:" + sql + Arrays.toString(params)); |
|
try { |
|
com.fr.data.impl.Connection dbConnect = getDbConnect(); |
|
|
|
Connection con = dbConnect.createConnection(); |
|
PreparedStatement preparedStatement = con.prepareStatement(sql); |
|
setParams(preparedStatement, params); |
|
ResultSet rs = preparedStatement.executeQuery(sql); |
|
// 获得记录的详细信息,然后获得总列数 |
|
ResultSetMetaData resMetaData = rs.getMetaData(); |
|
int colNum = resMetaData.getColumnCount(); |
|
// 用对象保存数据 |
|
String name = ""; |
|
String value = ""; |
|
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); |
|
while (rs.next()) { |
|
Map<String, Object> cells = new HashMap<String, Object>(); |
|
for (int i = 0; i < colNum; i++) { |
|
name = resMetaData.getColumnLabel(i); |
|
if (cells.get(name) != null) { |
|
name = resMetaData.getColumnLabel(i); |
|
} |
|
if (rs.getObject(i) != null && resMetaData.getColumnTypeName(i).equals("DATETIME") || resMetaData.getColumnTypeName(i).equals("TIMESTAMP")) { |
|
value = rs.getObject(i).toString(); |
|
cells.put(name, value.substring(0, value.length() - 2)); |
|
} else { |
|
cells.put(name, rs.getString(i)); |
|
} |
|
} |
|
list.add(cells); |
|
} |
|
// 释放数据库资源 |
|
rs.close(); |
|
preparedStatement.close(); |
|
con.close(); |
|
return list; |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
return null; |
|
} |
|
|
|
public Map<String, Object> findOneRow(String sql, Object... params) { |
|
List<Map<String, Object>> select = select(sql, params); |
|
if (select != null) { |
|
if (!select.isEmpty()) { |
|
return select.get(0); |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
public boolean checkExist(String sql, Object... params) throws Exception { |
|
PreparedStatement pstmt = getDbConnect().createConnection().prepareStatement(sql); |
|
setParams(pstmt, params); |
|
ResultSet resultSet = pstmt.executeQuery(); |
|
if (resultSet.next()) { |
|
return resultSet.getInt(1) > 0; |
|
} |
|
return false; |
|
} |
|
|
|
private void setParams(PreparedStatement pstmt, Object... params) throws SQLException { |
|
if (params.length > 0) { |
|
int length = params.length; |
|
for (int i = 1; i <= length; i++) { |
|
pstmt.setObject(i, params[i - 1]); |
|
} |
|
} |
|
} |
|
|
|
public int exSqlUpdate(String sql, Object... params) throws Exception { |
|
logger.info("update data by sql:" + sql + " params " + Arrays.toString(params)); |
|
com.fr.data.impl.Connection dbConnect = getDbConnect(); |
|
Connection connection = dbConnect.createConnection(); |
|
PreparedStatement pstmt = connection.prepareStatement(sql); |
|
setParams(pstmt, params); |
|
int i = pstmt.executeUpdate(); |
|
pstmt.close(); |
|
connection.close(); |
|
return i; |
|
} |
|
|
|
/** |
|
* 取查询结果集字段 |
|
* @param sql |
|
* @param params |
|
* @return |
|
* @throws Exception |
|
*/ |
|
public List<Map<String,String>> exQuery(String sql, Object... params) throws Exception { |
|
logger.info("query data by sql:" + sql + " params " + Arrays.toString(params)); |
|
com.fr.data.impl.Connection dbConnect = getDbConnect(); |
|
Connection connection = dbConnect.createConnection(); |
|
PreparedStatement pstmt = connection.prepareStatement(sql); |
|
setParams(pstmt, params); |
|
ResultSet resultSet = pstmt.executeQuery(); |
|
ResultSetMetaData resMetaData = resultSet.getMetaData(); |
|
int columnCount = resMetaData.getColumnCount(); |
|
List<Map<String,String>> arrs=new ArrayList<Map<String,String>>(); |
|
while (resultSet.next()) { |
|
String name; |
|
String value; |
|
Map<String, String> one = new HashMap<String, String>(); |
|
for (int i = 1; i <= columnCount; i++) { |
|
name = resMetaData.getColumnLabel(i); |
|
if (one.get(name) != null) { |
|
name = resMetaData.getColumnLabel(i); |
|
} |
|
if (resultSet.getObject(i) != null && resMetaData.getColumnTypeName(i).equals("DATETIME") || resMetaData.getColumnTypeName(i).equals("TIMESTAMP")) { |
|
value = resultSet.getObject(i).toString(); |
|
one.put(name, value.substring(0, value.length() - 2)); |
|
} else { |
|
one.put(name, resultSet.getString(i)); |
|
} |
|
} |
|
arrs.add(one); |
|
} |
|
|
|
pstmt.close(); |
|
connection.close(); |
|
logger.info("查询结果:"+arrs); |
|
return arrs; |
|
} |
|
}
|
|
|