Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

set identity_insert tablename on - Invalid call Statement method: {0}

843854Sep 4 2003 — edited Sep 5 2003
Hi,

I'm getting an error upon executing my code. I have a table which has an identity column in it. However the table is currently empty and I want to transfer the data from an Oracle database into the SQL Server database. This is all very easy. However...

I need to run the following command before I can add the data.

"set identity_insert tablename on"

However, upon execution I get the following error - how can I use the prepared statement AND set the tables identity_insert value to on?

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid call Statement method: {0}

The code that does this process is below... in this case you can assume setIdentOn = true.

String insertSQL = "insert into " + currentTable + "(" + columns + ") values(" + values + ")";

PreparedStatement insert = msconn.prepareStatement(insertSQL);
try {
if(setIdentOn) {
mslog("Setting IDENTITY_INSERT \"" + currentTable + "\" ON");
insert.execute("SET IDENTITY_INSERT " + currentTable + " ON");
}
insert.clearParameters();

for(int i=0;i<cOrder.size();i++) {
Object obj = data.get((String)cOrder.elementAt(i));

if(obj instanceof String) insert.setString(i+1, (String) obj);
else if(obj instanceof Integer) insert.setInt(i+1, ( (Integer) obj).intValue());
else if(obj instanceof Float) insert.setFloat(i+1, ((Float)obj).floatValue());
else if(obj instanceof Double) insert.setDouble(i+1, ((Double)obj).doubleValue());
else if(obj instanceof Character) insert.setString(i+1, String.valueOf(((Character)obj).charValue()));
else if(obj instanceof Timestamp) insert.setTimestamp(i+1, (Timestamp)obj);
else if(obj instanceof java.sql.Date) insert.setDate(i+1, (java.sql.Date)obj);
else if(obj instanceof NULL) {
int DATA_TYPE = getSQLType(currentTable, (String)cOrder.elementAt(i));
switch(DATA_TYPE) {
case Types.BLOB: {
insert.setBinaryStream(i+1, new ByteArrayInputStream(new byte[0], 0, 0), 0);
break;
}
case Types.CLOB: {
insert.setCharacterStream(i+1, new CharArrayReader(new char[0], 0, 0), 0);
break;
}
default: {
insert.setNull(i+1, DATA_TYPE);
}
}
}
else if(obj instanceof Blob) {
Blob blob = (Blob)obj;
insert.setBinaryStream(i+1, blob.getBinaryStream(), (int)blob.length());
}
else if(obj instanceof Clob) {
Clob clob = (Clob)obj;
insert.setCharacterStream(i+1, clob.getCharacterStream(), (int)clob.length());
}
else mslog("Object not bound at location " + (i+1));
}
return insert.executeUpdate();
}
catch(Exception e) {
System.err.println("FAILED ON: " + insertSQL);
throw e;
}
finally {
// try { statement.close(); } catch(Exception e) { }
try { insert.close(); } catch(Exception e) { }
}


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 3 2003
Added on Sep 4 2003
3 comments
939 views