所以我想做的是将自定义日期和当前时间一起设置到 Oracle 数据库中的 DATE 类型中。 我以 mm/dd/yyyy 的形式从 textField 中获取字符串
String tdate = textDate.getText().trim();
为了简化细节,我们只需将 DATE 插入到一个小表格中。
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String current_time = hour+":"+minute+":"+second;
现在我们有 tdate 作为日期字符串和 current_time 作为当前时间。
将表定义为:
create table transaction(
tranaction_num integer,
time_of_transaction DATE);
现在在 jdbc 中
PreparedStatement pStmt = Conn.prepareStatement("insert into transaction values(?,?));
pStmt.setString(1, "1");
pStmt.setString(2, "to_date( '"+tdate+" "+current_time+"','mm/dd/yyyy hh24:mi:ss')");
pStmt.executeUpdate();
这给我一个错误,如下所示
ORA-01858: a non-numeric character was found where a numeric was expected
我知道我做错了什么,但我已经尝试了很多方法来让它工作。我不介意以其他方式获取当前时间,只要它有效
任何帮助将不胜感激
最佳答案
您应该在将日期字符串移交给数据库之前对其进行解析,并且您应该使用 setInt()
作为第一个参数而不是 setString()
SimpleDateFormat parser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = parser.parse(tdate+" "+current_time);
String sqlStmt = "INSERT INTO transaction(tranaction_num, time_of_transaction) VALUES(?,?)";
PreparedStatement pStmt = Conn.prepareStatement(sqlStmt);
pStmt.setInt(1, 1);
pStmt.setDate(2, new java.sql.Date(date.getTime()));
pStmt.executeUpdate();
关于java - 使用字符串变量将自定义日期和当前时间插入 Oracle 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9626135/