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.
192 lines
6.8 KiB
192 lines
6.8 KiB
package com.fr.plugin.utils; |
|
|
|
import com.fanruan.api.data.ConnectionKit; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.log.FineLoggerProvider; |
|
|
|
import java.math.BigDecimal; |
|
import java.sql.*; |
|
import java.util.*; |
|
|
|
public class DBUtils { |
|
String db_name = ""; |
|
|
|
private static FineLoggerProvider logger = FineLoggerFactory.getLogger(); |
|
|
|
public DBUtils() { |
|
this.db_name = "DJDB"; |
|
} |
|
|
|
public com.fr.data.impl.Connection getDbConnect() { |
|
return ConnectionKit.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 int exec(String sql, String date) throws Exception { |
|
logger.info("query data by sql:{} 时间:{}", sql, date); |
|
Connection con = null; |
|
CallableStatement call = null; |
|
try { |
|
com.fr.data.impl.Connection dbConnect = getDbConnect(); |
|
con = dbConnect.createConnection(); |
|
call = con.prepareCall(sql); |
|
call.registerOutParameter(1, Types.INTEGER); |
|
call.execute(); |
|
Integer ret = call.getInt(1); |
|
return ret; |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
throw e; |
|
} finally { |
|
if (call != null) { |
|
call.close(); |
|
} |
|
if (con != null) { |
|
con.close(); |
|
} |
|
} |
|
} |
|
|
|
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 { |
|
Connection connection = getDbConnect().createConnection(); |
|
PreparedStatement pstmt = connection.prepareStatement(sql); |
|
setParams(pstmt, params); |
|
try { |
|
ResultSet resultSet = pstmt.executeQuery(); |
|
if (resultSet.next()) { |
|
return resultSet.getInt(1) > 0; |
|
} |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} finally { |
|
connection.close(); |
|
} |
|
|
|
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)); |
|
PreparedStatement pstmt = null; |
|
Connection connection = null; |
|
try { |
|
com.fr.data.impl.Connection dbConnect = getDbConnect(); |
|
connection = dbConnect.createConnection(); |
|
pstmt = connection.prepareStatement(sql); |
|
setParams(pstmt, params); |
|
int i = pstmt.executeUpdate(); |
|
return i; |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error("执行更新SQL报错: sql:{}", e, sql); |
|
} finally { |
|
if (pstmt != null) { |
|
pstmt.close(); |
|
} |
|
if (connection != null) { |
|
connection.close(); |
|
} |
|
} |
|
return 0; |
|
} |
|
|
|
/** |
|
* 取查询结果集字段 |
|
* |
|
* @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(); |
|
return arrs; |
|
} |
|
}
|
|
|