java - Insert with Select give sql-error (SQLCODE=-803, SQLSTATE=23505)(db2 z/os) -
i try insert values java application , values table1 table2. following error(db2 z/os):
exception in thread "main" com.ibm.db2.jcc.am.sqlintegrityconstraintviolationexception: db2 sql error: sqlcode=-803, sqlstate=23505, sqlerrmc=1;db2t.tsoz360_wv_ausgang, driver=3.66.46 preparedstatement _prep = con.preparestatement("insert db2t.table2 (column1, column2, column3 , column4, column5, column6) select ?, ?, ?, ?, column5, column6 db2t.table1 column1 = ? column2 = ? , column3 = ?"); _prep.setstring(1,"hello"); _prep.setstring(2,"h"); _prep.setstring(3,"1234567890"); _prep.setstring(4,"hsdfdsffdssdfsdfd"); _prep.setlong(5,9876543210l); _prep.setint(6,1); _prep.setint(7,12345678); table1: column1, column2, column3 , column4, column5, column6 table2 column1, column2, column3 , column4, column5, column6
so want insert values generated in java application table2 , 2 values table1. without import 2 values application. i'm doing wrong?
the sql exception got suggests you're trying insert 2 identical values column accepts unique values (did use insert same parameter values more once?).
however, design still bad suggest split query/statement select , insert (2 queries/statements instead of 1). make code more readable easier debug. second, shouldn't use column names parameters (?) in preparedstatement (a preparedstatement meant reusable, however, if column names parameterized, database can't reuse statement).
this means it's wrong like
string sql = "select ?, ?, ?, ?, column5, column6 db2t.table1 column1 = ? column2 = ? _prep.setstring(1, colname1); _prep.setstring(2, colname2); ....
you have specify column names in query (select a,b,c t x=?). if need obtained dynamically can use string concatenation (this potentially prone sql injection if don't sanitize user input etc )
string dynamiccolnames = getcolumnnames();//just method names 1 string string sql = "select "+dynamiccolnames + " ....";// //dynamiccolnames may contain col1, col2, col3 etc
Comments
Post a Comment