2012-04-25 111 views
0

我嘗試使用下面的JDBC語句從Java中插入一些值到Oracle DB:錯誤而JDBC預處理語句

String SQL_PREP_INSERT = "INSERT INTO ABC.TEST (LOG_ID, SESSION_ID,USER_ID) VALUES" 
      + " (ABC.logid_seq.nextval, ?, ?)"; 

stmt = con.prepareStatement(SQL_PREP_INSERT); 
stmt.setString(1, sessionId); 
stmt.setString(2, userid); 
stmt.execute(); 
stmt.close(); 

的順序如下創建:

create sequence ABC.logid_seq 
minvalue 1 maxvalue 9999999999999999999999 
increment by 10 start with 10 cache 20 noorder nocycle ; 

我我得到以下錯誤,

java.sql.SQLException: ORA-00942: table or view does not exist 

但是,當我嘗試手動插入到表中,它的成功FUL。

insert into ABC.test(LOG_ID,SESSION_ID,USER_ID) values 
    (VZPPTL.logid_seq.nextval,'test_session', '001'); 

什麼問題?

+1

http://stackoverflow.com/questions/6561650/getting-an-exception-ora-00942-table-or-view-does-not-exist-when-inserting-in – 2012-04-25 09:39:17

+0

抱歉..如何接受答案? – 2012-04-25 09:58:14

+0

謝謝..我接受以前的問題.. – 2012-04-25 10:13:29

回答

2

可能看錯了表或數據庫。你確定你從代碼看正確的數據庫嗎?

+0

是的,它的右側數據庫和右側表格,因爲在將序列添加到插入語句之前,我可以先插入。我剛剛刪除了表並通過添加新列log_id重新創建。現在得到這個錯誤。 – 2012-04-25 10:51:34

+0

您是否嘗試過在數據庫上運行'preparedstatement'生成的查詢? – KodeSeeker 2012-04-25 11:07:54

+0

號我現在不能生成Prepared Statement查詢,因爲代碼位於不同的框中,我需要刪除代碼更改。但在本地機器上,我不能嘗試,因爲有了pointbase而不是Oracle DB。我正在嘗試使用pointbase,java.sql.SQLException獲取以下內容:在位置211的表中未找到列「NEXTVAL」。 – 2012-04-25 12:20:20

1

在準備語句中不需要提供模式名稱(在這種情況下爲ABC)。

試試這個,它可能工作。

String SQL_PREP_INSERT =「INSERT INTO TEST(LOG_ID,SESSION_ID,USER_ID)VALUES」 +「(logid_seq.nextval,?,?)」;

+0

嗯好吧,我會嘗試沒有模式名稱。但以前我能夠使用schema.table_name插入。但在那種情況下,我沒有序列。字符串SQL_PREP_INSERT =「INSERT INTO ABC.TEST(SESSION_ID,USER_ID)VALUES」 \t \t \t +「(?,?)」; – 2012-04-25 10:37:25

+0

我剛剛添加了一個新的列log_id。並使用序列插入。添加後,它拋出該錯誤。 – 2012-04-25 11:00:43