前端之家收集整理的这篇文章主要介绍了
DBUtils 处理Oracle 日期类型,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
先建表
</pre><pre name="code" class="sql">create table dbuser.msg_table
(
MSG_ID INTEGER,SYNC_TIME DATE
)
</pre><pre name="code" class="sql">
</pre><p>使用DBUtils插入数据</p><p><pre name="code" class="java">import java.sql.*;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
public class JDBCStatementInsertExample {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:demo";
private static final String DB_USER = "user";
private static final String DB_PASSWORD = "password";
public static void main(String[] argv) {
Connection conn;
QueryRunner queryRunner = new QueryRunner();
try {
Class.forName(DB_DRIVER);
conn = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
String sql = "INSERT INTO dbuser.msg_table(MSG_ID,SYNC_TIME) VALUES(?,?)";
Integer msgId = 8;
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
Object[] record = new Object[]{msgId,timestamp};
int n = queryRunner.update(targetConn,insertsql,record);
if (n != 1) {
System.out.println("got error whern insert data");
}
DbUtils.close(conn);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
使用DBUtils 向Oracle插入日期很方便,省去了拼接字符串的麻烦
原文链接:https://www.f2er.com/oracle/213095.html