java - 尝试将 double 值插入 Oracle 数据库时出现 SQLException

标签 java sql string oracle11g ora-00913

我必须开发一个小程序,将一些数据插入到 Oracle 数据库中。不幸的是,我在 SQL Statement 及其执行方面遇到了一些麻烦。这是我正在使用的代码:

db.execute(
    String.format("INSERT INTO tops VALUES (%d, '%s', %d, %f.00, '%s', TO_TIMESTAMP('%s', 'YYYY-MM-DD HH24:MI:SS.FF'))", 
        item.getID(),
        item.getTitle(),
        this.elements,
        item.getSize(),
        item.getEntity(),
        timestamp.toString()));

这是执行应该工作的部分,但我收到以下错误:

java.sql.SQLException: ORA-00913: Zu viele Werte

Google Translate异常(exception)是:

java.sql.SQLException: ORA-00913: Too many values

最佳答案

您可以按照 Guallaume 在评论中的建议使用这样的准备好的语句;

PreparedStatement pstmt = null;
Connection conn = null;

try{
     //if you have a method that creates a connection for you.
     conn = getConnection();
     pstmt = conn.prepareStatement("INSERT INTO tops(id, title, elements, size, entity, timeStamp) VALUES(?,?,?,?,?,?)");
     pstmt.setInt(1,item.getID());

     //Assuming that title is a String data type
     pstmt.setString(2,item.getTitle());
     pstmt.setString(3,this.elements);
     pstmt.setDouble(4,item.getSize()); // <--- JDBC will make sure this works

     //assuming Entity data type is String
     pstmt.setString(5,item.getEntity());

     //if your timestamp's string format is 
     //well formed, you may insert as a string.
     pstmt.setString(6,timestamp.toString());
     pstmt.executeUpdate();
}catch(Exception e){
     e.printStackTrace();
}finally{  
     try{
         pstmt.close();
     }catch(Exception e){}

     try{
         conn.close();
     }catch(Exception e){}
 }

关于java - 尝试将 double 值插入 Oracle 数据库时出现 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13361692/

相关文章:

java - 我的代码生成器无法正常工作

java - 使用迭代器删除条目的程序

java - 如何以当前毫秒为单位检查 1 天摘要的数据库记录

android - "How to retrieve data from multiple tables using cursors in android studio"

java - 为什么要设置字符串 DRIVER?

mysql - 使用 Mysql 选择语句

java - 是否可以将字符串中的文本颜色更改为 Java 中的多种颜色?

Java - 编码风格 : What are the cons and pros of "ABC". 等于 ("SOMESTRING") 风格字符串比较?

c++ - 通过知道变量的名称来更改变量的值

java - MySQL JDBC 连接器转义问题