声明此触发器:
使用Java / JDBC实现触发器:CREATE TRIGGER my_triggerBEFORE UPDATEON my_tableFOR EACH ROWCALL "com.example.MyTrigger"
用jOOQ实现触发器:public class MyTrigger implements Trigger { @Override public void init(Connection conn, String schemaName,String triggerName, String tableName, boolean before, int type) throws SQLException {} @Override public void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException { try (PreparedStatement stmt = conn.prepareStatement( "INSERT INTO log (field1, field2, ...) " + "VALUES (?, ?, ...)") ) { stmt.setObject(1, newRow[0]); stmt.setObject(2, newRow[1]); ... stmt.executeUpdate(); } } @Override public void close() throws SQLException {} @Override public void remove() throws SQLException {}}
由于您在问题中添加了jOOQ标记,因此我怀疑这种替代方法也可能是相关的。您当然可以在H2触发器内使用jOOQ:
@Override public void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException { DSL.using(conn).insertInto(LOG, LOG.FIELD1, LOG.FIELD2, ...).values(LOG.FIELD1.getDataType().convert(newRow[0]), LOG.FIELD2.getDataType().convert(newRow[1]), ...).execute(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)