我必须开发一个小程序,将一些数据插入到 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/