我在PostgreSQL 9.1中定义了一个触发器,只要使用以下命令更新记录就会触发:
CREATE OR REPLACE FUNCTION check() RETURNS TRIGGER AS $$
BEGIN
SELECT doubletEngine.checkForDoublet(NEW.id);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS check_upd on people;
CREATE TRIGGER check_upd AFTER UPDATE ON people
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE PROCEDURE check();
方法doubletEngine.checkForDoublet()使用
引入PL / Java 1.4.3CREATE OR REPLACE FUNCTION doubletEngine.checkForDoublet(varchar) RETURNS void AS 'DoubletSearcher.checkForDoublet' LANGUAGE java;
在方法checkForDoublet()中,我尝试使用
连接到数据库Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://127.0.0.1:5432/db";
c = DriverManager.getConnection(url, "postgres", "postgres");
if ( c == null ) {
throw new RuntimeException("Could not connect to server");
}
...但是c仍然是空的。直接从我的IDE调用此代码可以很好地工作,但是当触发器触发时,它只会抛出RuntimeException。我有什么想念...... ???
答案 0 :(得分:1)
根据PL / Java documentation,应该按如下方式获得连接:
Connection conn = DriverManager.getConnection("jdbc:default:connection");