java项目连接opengauss数据库大致两种连接方式:

一、jdbc连接

1.下载并加载驱动

从官网下载相应版本的驱动 软件包 | openGauss

注意:架构类型与自己的CPU相同。

2.在项目中将下载并解压后的*.jar添加到项目。

3.在项目配置文件或连接库代码中使用。openGauss jdbc的驱动为“org.opengauss.Driver”,其中url前缀为“jdbc:opengauss”。

 示例:

driver-class-name: org.postgresql.Driver
username: opengauss
password: test#123
url: jdbc:postgresql://192.168.63.131:5432/test?connectTimeout=5&tcpKeepAlive=true

4.代码连接

public static Connection getConnect() {
        String driver = "org.opengauss.Driver";
        String sourceURL = "jdbc:opengauss://127.0.0.1:8080/test?user=myuser&password=myPassWord";
        Properties info = new Properties();
        Connection conn = null;
        try {
            Class.forName(driver);
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
        try {
            conn = DriverManager.getConnection(sourceURL);
            System.out.println("连接成功!");
            return conn;
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

/*DriverManager.getConnection(String url, Properties info);*/

public static Connection getConnect() {
        String driver = "org.opengauss.Driver";
        String sourceURL = "jdbc:opengauss://127.0.0.1:8080/test";
        Properties info = new Properties();
        info.setProperty("user","myuser");
        info.setProperty("password","myPassWord");
        Connection conn = null;
        try {
            Class.forName(driver);
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
        try {
            conn = DriverManager.getConnection(sourceURL, info);
            System.out.println("连接成功!");
            return conn;
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

/*DriverManager.getConnection(String url, String user, String password);*/

public static Connection getConnect() {
        String driver = "org.opengauss.Driver";
        String sourceURL = "jdbc:opengauss://127.0.0.1:8080/test";
        String username="myuser";
        String passwd="myPassWord";
        Connection conn = null;
        try {
            Class.forName(driver);
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
        try {
            conn = DriverManager.getConnection(sourceURL, username, passwd);
            System.out.println("连接成功!");
            return conn;
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

二、通过阿里druid连接

在yml配置文件中(opengauss版本为3.1)

spring:
  datasource:
    tjxt:
      driver-class-name: org.postgresql.Driver
      username: opengauss
      password: test#123
      url: jdbc:postgresql://192.168.63.131:5432/test?connectTimeout=5&tcpKeepAlive=true
      type: com.alibaba.druid.pool.DruidDataSource
    qtxt:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      username: sa
      password: 123
      url: jdbc:sqlserver://192.168.0.10:1433;database=master
      type: com.alibaba.druid.pool.DruidDataSource

注意:官网给出的配置参数是

driver-class-name: org.opengauss.Driver

username: opengauss

password: test#123

url: jdbc:opengauss://192.168.63.131:5432/test

通过阿里druid连接方式并未测试通过。可能与我使用的druid版本有关,我使用的是1.2.16

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐