2012-04-25 105 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