JDBC URL批量處理參數配置


1,useServerPrepStmts參數影響數據精度

  • useServerPrepStmts:如果服務器支持,是否使用服務器端預處理語句? 默認值為“真”
com.mysql.jdbc.ResultSetImpl
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
        if (!this.isBinaryEncoded) {        // 1
            String stringVal = this.getString(columnIndex);
            if (stringVal != null) {
                BigDecimal val;
                if (stringVal.length() == 0) {
                    val = new BigDecimal(this.convertToZeroLiteralStringWithEmptyCheck());
                    return val;
                } else {
                    try {
                        val = new BigDecimal(stringVal);
                        return val;
                    } catch (NumberFormatException var5) {
                        throw SQLError.createSQLException(Messages.getString("ResultSet.Bad_format_for_BigDecimal", new Object[]{stringVal, Constants.integerValueOf(columnIndex)}), "S1009");
                    }
                }
            } else {
                return null;
            }
        } else {
            return this.getNativeBigDecimal(columnIndex);  // 2
        }
    }
  • Java在用BigDecimal接受數據庫varchar數據時,會丟失精度
  • mysql的url后面useServerPrepStmts參數 決定了截圖中isBinaryEncoded的值
  • 如果isBinaryEncoded為true,則代碼會走2的邏輯,2的邏輯是讀取數據庫中decimal的保留位數,因為數據庫價格那個字段是varchar,所以讀取到的保留位數為0,最終導致獲取的數據是不保留小數位
  • 如果isBinaryEncoded為false,則走1的邏輯,1的邏輯是拿數據庫里查詢的數據直接構造bigdecimal並返回,所以不會出問題
  • useServerPrepStmts = false& rewriteBatchedStatements = true

作者:JavaHub
鏈接:https://www.jianshu.com/p/86a01ee64080
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM