简体   繁体   English

如何在 Java 代码中从 Postgresql 调用存储过程

[英]How call a stored procedure from Postgresql in the Java code

Searching the web for a solution to create a database for my spring project when it doesn't exist, I found this topic here in stackoverflow:在网上搜索一个解决方案来为我的 spring 项目创建一个数据库,当它不存在时,我在 stackoverflow 中找到了这个主题:

Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL? 为 PostgreSQL 模拟 CREATE DATABASE IF NOT EXISTS?

with this stored procedure to acomplish that:用这个存储过程来完成:

DO
$do$
BEGIN

IF EXISTS (SELECT 1 FROM pg_database WHERE datname = 'mydb') THEN
   RAISE NOTICE 'Database already exists'; 
ELSE
   PERFORM dblink_exec('dbname=' || current_database() -- current db
                      , $$CREATE DATABASE mydb$$);
END IF;

END
$do$

I want run this procedure from my Java code.我想从我的 Java 代码运行这个过程。 My initial idea was include this code as a String atribute in this Service class:我最初的想法是将此代码作为字符串属性包含在此服务类中:

@Service
public class InstallService {

    private String query = "";

    public boolean create_database(String maquina, String usuario, String senha) {
        return false;
    }

    public boolean create_user(String usuario, String senha, String email) {
        return false;
    }
}

But I just find out this can't be done.但我才发现这是做不到的。 Anyone have any sugestion of how to do that inside this class?任何人都对如何在这个班级中做到这一点有任何建议?

I would recommend calling the stored procedure via whatever method you're currently using to connect to Postgres from Spring.我建议通过您当前用于从 Spring 连接到 Postgres 的任何方法调用存储过程。 (ie Spring JDBC: http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html ; or perhaps MyBatis, etc.) (即 Spring JDBC: http : //docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html ;或者 MyBatis 等)

You could define the stored procedure in, say, the template database, and then connect to it via Spring JDBC or whatever method you're employing, with a query of the form:例如,您可以在template数据库中定义存储过程,然后通过 Spring JDBC 或您使用的任何方法连接到它,并使用以下形式的查询:

SELECT stored_proc_name(dbname)

(You would need to have the stored procedure take the DB name as an argument, also.) (您还需要让存储过程将 DB 名称作为参数。)

Edit: In response to the comment below, inquiring if this can be done without adding anything to a database, the answer is yes:编辑:针对下面的评论,询问是否可以在不向数据库添加任何内容的情况下完成此操作,答案是肯定的:

You could connect to the template1 DB, run the SELECT query against pg_catalog to see if the DB exists (you'll get an empty set back if it doesn't), and if it doesn't exist, run another query on the connection to the template1 db to create the DB.您可以连接到template1数据库,针对pg_catalog运行SELECT查询以查看该数据库是否存在(如果不存在,您将获得一个空集),如果不存在,则在连接上运行另一个查询到template1 db 以创建数据库。

Basically, it'd be deconstructing the stored procedure into its constituent parts and calling them directly from Java using JDBC or similar.基本上,它将存储过程解构为其组成部分,并使用JDBC或类似方法直接从 Java 调用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM