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

Pat*_*ogt 8 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()));
Run Code Online (Sandbox Code Playgroud)

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

java.sql.SQLException: ORA-00913: Zu viele Werte
Run Code Online (Sandbox Code Playgroud)

谷歌翻译例外是:

java.sql.SQLException: ORA-00913: Too many values
Run Code Online (Sandbox Code Playgroud)

mwa*_*ngi 5

您可以使用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){}
 }
Run Code Online (Sandbox Code Playgroud)