【问题标题】:Ballerina: How to connect PostgreSQL database with Ballerina App?Ballerina:如何将 PostgreSQL 数据库与 Ballerina App 连接?
【发布时间】:2018-08-03 00:47:42
【问题描述】:

我是 Ballerina 的新手,正在尝试了解基本用法和功能。有没有什么简单的方法可以将 PostgreSQL 数据库与 Ballerina App 连接起来?

【问题讨论】:

    标签: ballerina


    【解决方案1】:

    您可以使用 ballerina jdbc 包来执行此操作。另请注意,您必须将 postgres jdbc driver 复制到 ${BALLERINA_HOME}/bre/lib。

    以下是连接到 postgres 数据库并执行选择操作的示例。

    import ballerina/jdbc;
    import ballerina/io;
    
    endpoint jdbc:Client testDB {
            url: "jdbc:postgresql://localhost:5432/testdb3",
            username: "postgres",
            password: "123",
            poolOptions: { maximumPoolSize: 1 }
        };
    
    type Customer record {
        int id,
        string name,
    };
    
    function main(string... args) {
    
        table dt = check testDB->select("select id, name from Customers", Customer);
    
        while (dt.hasNext()) {
            Customer rs = check <Customer>dt.getNext();
            io:println(rs.id);
            io:println(rs.name);
        }
        testDB.stop();
    }
    

    请参考this获取完整的ballerina jdbc包功能示例。

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    相关资源
    最近更新 更多