【问题标题】:Sequencing primary key in oracle / JDBCoracle / JDBC中的主键排序
【发布时间】:2013-06-19 13:29:57
【问题描述】:

所以我有代码:

public void addUser( 
            String username, String password,
            String f_name, String l_name, String email) 
    {
        try 
        {
            //conn.setAutoCommit(false); 
            pstmnt = conn.prepareStatement("INSERT INTO users VALUES (?,?,?,?)");
            pstmnt.setString(1, "user_id_increment.nextval");
            pstmnt.setString(2, username);
            pstmnt.setString(3, password);
            pstmnt.setInt(4, 0);

            pstmnt.execute();

在Oracleb中我有这个序列:

--Auto incrementing the user_id
CREATE SEQUENCE user_id_increment
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;

但是,我在 eclipse 中遇到了异常:

Error: ORA-01722: invalid number

我认为调用序列 name.nextval 会给我序列中的下一个值,当我插入任何其他整数时 setString 工作。

有什么想法吗?

【问题讨论】:

    标签: java oracle jdbc sequence


    【解决方案1】:

    试试

    pstmnt = conn.prepareStatement("INSERT INTO users VALUES (user_id_increment.nextval,?,?,?)");
            pstmnt.setString(1, username);
            pstmnt.setString(2, password);
            pstmnt.setInt(3, 0);
    

    您必须在 SQL 语句中包含您的序列,而不是将其作为参数传递。

    setString 适用于其他整数的原因大概是因为 jdbc 驱动程序可以将它们强制转换为整数(并且它不能强制“user_id_increment.nextval”)

    【讨论】:

    • 天才!非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多