java解析oracle日志,获取指定日期后的更新和插入数据

1.数据库连接很简单就不写了

如何实现解析oracle日志?

在oracle中有logmnr解析过程包,通过该过程包对oracle日志进行解析,以下是示例

/**
 * 
 *  执行过程语句
*/
private void executeCallable(String _sql, CallableStatement _call,
Connection _con) throws SQLException {
_call = _con.prepareCall(_sql);
_call.execute();
}


/**

*该方法是获取oracle日志地址,封装到一个list中

*/

private List<String> doLogAddress() throws SQLException {
List<String> listLog = new ArrayList<String>();

//数据库连接
Connection con = this.createConnection();
Statement stat = con.createStatement();
// 获取日志地址
String logSql = "select group#,member from v$logfile ORDER BY group#";
ResultSet res = null;
try {
res = stat.executeQuery(logSql);
while (res.next()) {
listLog.add(res.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeConnection(con, res);
}
return listLog;
}


/**

*添加日志文件返回一个String

*/

private String doAddfileSQL(List<String> _listLog) {
StringBuffer addLogSql = new StringBuffer();
addLogSql.append("BEGIN ");
addLogSql.append("dbms_logmnr.add_logfile(logfilename=>'"
+ _listLog.get(0).replaceAll("\\\\", "\\\\\\\\")
+ "',options=>dbms_logmnr.NEW);");
for (int i = 1; i < _listLog.size() - 1; i++) {
addLogSql.append("dbms_logmnr.add_logfile(logfilename=>'"
+ _listLog.get(i).replaceAll("\\\\", "\\\\\\\\")
+ "',options=>dbms_logmnr.addfile);");
}
addLogSql.append("END;");
return addLogSql.toString();
}


/**

*获取指定日期后的更新和插入数据的rowid

*_date是指定日期,格式:yyyy-MM-dd HH24:mi:ss

*/

public List<String> doUpdateAndInsertToData(String _date) {
Connection con = this.createConnection();
Statement stat = null;
CallableStatement callableStatement = null;
List<String> listLog = new ArrayList<String>();
try {
listLog = this.doLogAddress();
stat = con.createStatement();
// 添加所有日志
String addLogSQL = this.doAddfileSQL(listLog);
this.executeCallable(addLogSQL, callableStatement, con);
System.out.println("添加日志完成");
// 开始分析日志,联网方式
String startLog = "BEGIN dbms_logmnr.start_logmnr(OPTIONS=>DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);END;";
this.executeCallable(startLog, callableStatement, con);
System.out.println("日志分析完成");
// 查询日志内容,将更新和插入操作的数据全部查出来
String analysisLog = "SELECT t.ROW_ID FROM v$logmnr_contents t where t.TABLE_NAME='"+(需要查找的表名)+"' "
+ "and (t.OPERATION like 'UPDATE' or t.OPERATION like 'INSERT') "
+ "and to_date(TO_CHAR(SCN_TO_TIMESTAMP(t.scn), 'yyyy-MM-dd HH24:mi:ss'),'yyyy-MM-dd HH24:mi:ss')"
+ ">to_date('" + _date + "','yyyy-MM-dd HH24:mi:ss')";
ResultSet result = stat.executeQuery(analysisLog);
listLog.clear();
while (result.next()) {
System.out.println(result.getString(1));
listLog.add(result.getString(1));
}
// 分析完成后,释放内存
String endLogSQL = "BEGIN dbms_logmnr.end_logmnr;END;";
this.executeCallable(endLogSQL, callableStatement, con);
} catch (SQLException e) {
e.printStackTrace();
}
return listLog;
}


将rowid取出来后,在源数据库中到相应表中用rowid进行查询,将结果集同步到目标数据中


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的 Java JDBC 读取 Oracle 数据库插入数据的示例代码: 1. 首先需要安装 Oracle 的 JDBC 驱动程序,可以从官方网站下载并安装。 2. 在 Java 代码中导入 JDBC 驱动程序: ``` import java.sql.*; ``` 3. 连接到 Oracle 数据库: ``` String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String user = "username"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); ``` 其中 url 中的 localhost:1521 是 Oracle 数据库的主机地址和端口号,orcl 是要连接的数据库实例名。user 和 password 分别是数据库的用户名和密码。 4. 从 Oracle 数据库中读取数据: ``` Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); // do something with the data } rs.close(); stmt.close(); ``` 这里使用了 Statement 对象执行了一条 SELECT 查询语句,并使用 ResultSet 对象获取查询结果。查询结果可以使用 rs.next() 方法逐行遍历,然后通过列名获取每一行的数据。 5. 插入数据Oracle 数据库: ``` PreparedStatement pstmt = conn.prepareStatement("INSERT INTO mytable (id, name, age) VALUES (?, ?, ?)"); pstmt.setInt(1, 1); pstmt.setString(2, "John"); pstmt.setInt(3, 30); pstmt.executeUpdate(); pstmt.close(); ``` 这里使用了 PreparedStatement 对象执行了一条 INSERT 插入语句,通过占位符 ? 传递参数。pstmt.executeUpdate() 方法用于执行插入操作。 6. 最后记得关闭数据库连接: ``` conn.close(); ``` 完整的代码示例: ```java import java.sql.*; public class OracleJDBCExample { public static void main(String[] args) throws SQLException { String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String user = "username"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); // read data from Oracle database Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); // do something with the data } rs.close(); stmt.close(); // insert data into Oracle database PreparedStatement pstmt = conn.prepareStatement("INSERT INTO mytable (id, name, age) VALUES (?, ?, ?)"); pstmt.setInt(1, 1); pstmt.setString(2, "John"); pstmt.setInt(3, 30); pstmt.executeUpdate(); pstmt.close(); conn.close(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值