一般在sqlplus或者其他第三方oracle工具中,按照语法及需求写好代码,直接执行创建过程即可。
网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了章贡免费建站欢迎大家使用!
一般语法如下:
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER }
{INSERT | DELETE | UPDATE [OF column [, column …]]}
[OR {INSERT | DELETE | UPDATE [OF column [, column …]]}...]
ON [schema.]table_name | [schema.]view_name
[REFERENCING {OLD [AS] old | NEW [AS] new| PARENT as parent}]
[FOR EACH ROW ]
[WHEN condition]
PL/SQL_BLOCK | CALL procedure_name;
1、oracle数据库中的触发器,insert into users values('05','trigger','普通','trigger','拥有一般权限');。
2、oracle数据库中的触发器(触发器dml删除三条记录后)delete from users where u_type='其他';。
3、oracle数据库中的触发器(替代触发器修改记录信息)。
4、oracle数据库中的触发器(触发器dml插入一条记录,日志之前)。
5、oracle数据库中的触发器delete orders where o_id='200708021850';。
6、oracle数据库中的触发器(触发器dml更新一条记录之前)。
创建两个表:
create table a
(stdid int,
stdname varchar2(10));
create table b
(stdid int,
stdname varchar2(10));
创建触发器:
CREATE OR REPLACE TRIGGER tr_insert
after insert
ON a
FOR EACH ROW
BEGIN
INSERT INTO b(stdid,stdname)
VALUES(:new.stdid,:new.stdname);
END;
验证,在a表中插入数据:
insert into a values (1,'a');
commit;
验证b表结果:
1、Java代码创建Oracle触发器
Java代码如下:
create or replace and compile java source named jym as
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class JYM {
public static String sendSynMsg(String ipAddr, byte[] datas) throws Exception{
InetSocketAddress endpoint = new InetSocketAddress(ipAddr ,18002);
Socket socket = null;
OutputStream out = null;
InputStream in = null;
try {
socket = new Socket();
socket.connect(endpoint);
out = socket.getOutputStream();
in = socket.getInputStream();
out.write(datas);
out.flush();
return null;
} finally {
if (out != null) {
try {
out.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
public static void say(String ip,byte[] context) throws Exception {
String str=JYM.sendSynMsg(ip,context);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.9.109:1521:orcl","zhym","jhhz621");
PreparedStatement pr=conn.prepareStatement("insert into backdata(context) values(?)");
pr.setBytes(1, context);
pr.executeUpdate();
pr.close();
conn.close();
}
}
create or replace and compilejava source named jym as
import java.io.BufferedReader;
import java.io.InputStream;
importjava.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
importjava.NET.InetSocketAddress;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
importjava.sql.PreparedStatement;
import java.sql.Statement;
public class JYM {
public static StringsendSynMsg(String ipAddr, byte[] datas) throws Exception{
InetSocketAddress endpoint = new InetSocketAddress(ipAddr ,18002);
Socket socket = null;
OutputStream out = null;
InputStream in = null;
try {
socket = new Socket();
socket.connect(endpoint);
out = socket.getOutputStream();
in = socket.getInputStream();
out.write(datas);
out.flush();
return null;
} finally {
if (out != null) {
try {
out.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
public static void say(String ip,byte[] context) throws Exception {
String str=JYM.sendSynMsg(ip,context);
Class.forName("Oracle.jdbc.driver.OracleDriver");
Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@192.168.9.109:1521:orcl","zhym","jhhz621");
PreparedStatement pr=conn.prepareStatement("insert intobackdata(context) values(?)");
pr.setBytes(1, context);
pr.executeUpdate();
pr.close();
conn.close();
}
}
2、存储过程调用Java source
Sql代码
代码写好后可以先用存储过程测试:
Sql代码
总结:假如运行存储过程时爆出了 请给用户复制权限的提示则按照提示在sysdba 下赋权限,如:
Sql代码
exec dbms_java.grant_permission('zhym','SYS:java.net.SocketPermission','192.168.9.12:18002','connect,resolve')
exec dbms_java.grant_permission('ZHYM','SYS:java.net.SocketPermission','127.0.0.1:1521','connect,resolve')
execdbms_java.grant_permission('zhym','SYS:java.Net.SocketPermission','192.168.9.12:18002','connect,resolve')
execdbms_java.grant_permission('ZHYM','SYS:java.net.SocketPermission','127.0.0.1:1521','connect,resolve')
exec 后面的语句会在你的报错提示中打印出来,只要前面加exec就可以了。
create or replace trigger qwx_tri after insert
on QWXMX for each row
begin
insert into FEIYONG (qxdh) values(:new.qxdh)
end;
你写的不伦不类的,和sqlserver混淆了吧