有Oracle、MySQL、SQLserver三種資料源;
如何從PreparedStatement物件中獲取到sql陳述句!!!
// 執行sql查詢主方法
public boolean submit(String sql, Object... paras) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBConnectionPool.getConnection(dbenum);
// 定義PreparedStatement物件
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
// 結果集一次獲取資料條數
pstmt.setFetchSize(SysConst.FETCHCOUNT);
if (paras != null) {
for (int i = 0; i < paras.length; i++) {
Object para = paras[i];
if (para != null && para.getClass().equals(java.util.Date.class)) {
Date d = (Date) para;
pstmt.setTimestamp(i + 1, new Timestamp(d.getTime()));
} else {
pstmt.setObject(i + 1, para);
}
}
}
// 輸出編輯好的SQL陳述句,便于調查問題
logger.info("SQL陳述句-->" + SchemaUtil.getSQLString(pstmt));
} catch (Exception e) {
logger.error("查詢程序例外", e);
return false;
} finally {
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
logger.error("連接關閉例外", e);
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
logger.error("連接關閉例外", e);
}
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////
public class SchemaUtil {
public static String getSQLString(PreparedStatement pre) {
//列印sql陳述句
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250836.html
標籤:Java相關
上一篇:位元組碼檔案中關于this和super的問題,求教大佬解惑
下一篇:小白一個,求助
