DBUtils 處理Oracle 日期型別
阿新 • • 發佈:2019-01-06
先建表
</pre><pre name="code" class="sql">create table dbuser.msg_table
(
MSG_ID INTEGER,
SYNC_TIME DATE
)
</pre><pre name="code" class="sql">
使用DBUtils 向Oracle插入日期很方便,省去了拼接字串的麻煩</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(); } } }