我在PostgreSQL数据库中创建了一个包含42列的表。其中五个是bigint
类型,而其他类型是character varying
类型。然后我尝试使用一些java代码和spring framework中的jdbc库插入数据。
我遇到了一些我需要找到的错误,这很难,因为该表包含很多列 jdbc中要插入该表的语法:
String sql = "insert into t_reconcile_stage values("
+ " ?::bigint," // 1 id
+ "?::bigint," // 2 m_participant_id
+ "?::bigint," // 3 m_status_data_id
+ "?," // 4 reconcile_file
+ "?::date," // 5 reconcile_date
+ "?," // 6 transaction_date
+ "?," // 7 transaction_time
+ "?::bigint," // 8 stan
+ "?," // 9 rrn
+ "?," // 10 merchant_type
+ "?," // 11 terminal_id
+ "?," // 12 pan
+ "?," // 13 debit_account
+ "?," // 14 credit_account
+ "?::bigint," // 15 amount
+ "?," // 16 customer_id
+ "?," // 17 participant_reference
+ "?," // 18 data_key
+ "?," // 19 dealer_code
+ "?," // 20 biller_code
+ "?," // 21 product_code
+ "?," // 22 feature_code
+ "?::bigint," // 23 acquire_fee
+ "?::bigint," // 24 issuer_fee
+ "?::bigint," // 25 biller_fee
+ "?::bigint," // 26 switching_fee
+ "?::bigint," // 27 merchant_fee
+ "?," // 28 transaction_type
+ "?," // 29 trfree
+ "?," // 30 free_data1
+ "?," // 31 free_data2
+ "?," // 32 free_data3
+ "?," // 33 free_data4
+ "?," // 34 free_data5
+ "?," // 35 free_data6
+ "?," // 36 free_data7
+ "?," // 37 free_data8
+ "?," // 38 free_data9
+ "?," // 39 free_data10
+ "?::bigint," // 40 settlement_amount
+ "?::bigint," // 41 charge_amount
+ "?" // 42 status
+ ")"; //
simpleJdbcTemplate.update( sql,
d[0],d[1],d[2],d[3],d4,
d[5],d[6],d[7],d[8],d[9],
d[10],d[11],d[12],d[13],d[14],
d[15],d[16],d[17],d[18],d[19],
d[20],d[21],d[22],d[23],d[24],
d[25],d[26],d[27],
d[28],d[29],d[30],d[31],d[32],d[33],d[34],d[35],d[36],d[37],d[38],
d[39],d[40],d[41]
);
我得到的错误:
[2014-08-06 10:48:01,753] [ INFO] - org.springframework.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
[2014-08-06 10:48:01,769] [ERROR] - dwidasa.reconcile.service.etl.PreReconcileImpl - error in -> etl(6000007777-20140608-gabung.txt) error in inserting data = 101932,0608,00006003110,5136,534110565309,201406,05280698950B312D9FF000,000000078281,00000078281,0000000000,000000000,0020 into tabel t_reconcile_stage
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [insert into t_reconcile_stage values( ?::bigint,?::bigint,?::bigint,?,?::date,?,?,?::bigint,?,?,?,?,?,?,?::bigint,?,?,?,?,?,?,?,?::bigint,?::bigint,?::bigint,?::bigint,?::bigint,?,?,?,?,?,?,?,?,?,?,?,?,?::bigint,?::bigint,?)]; ERROR: invalid input syntax for integer: ""; nested exception is org.postgresql.util.PSQLException: ERROR: invalid input syntax for integer: ""
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTe
我不知道错误发生在哪一列。所以我需要使用java中的try-catch
语句逐列将数据插入到该表中。是否可以在Postgres中进行INSERT
这样的查询?
或者找到该错误的任何其他方法?或者我错过了什么?
(我是处理数据库的初学者。)
答案 0 :(得分:2)
INSERT
是全有或全无操作。插入一个新行,并且需要使用值或NULL(使用显式输入或默认列)分配每一列。所以不,你所要求的是不可能的。
来自Postgres的错误是:
错误:整数的输入语法无效:“”
问题的根源在于您尝试将空字符串作为bigint
号出售,这是不可能的。将字符数据转换为整数时,必须将空字符串转换为某种有效形式。 0
或NULL
(不含引号)。
答案 1 :(得分:0)
此错误的一种解决方法是为列指定默认值(但这并非总是很好)。这样,如果你在insert语句中缺少数据,它应该使用默认值。
虽然我建议你设计一个更新方法的修订版,正如上面提到的海报,你的整个问题来自你的数组中的一个值是空的,并且可以通过适当的数据验证来修复。