赞
踩
本文分享自华为云社区《JDBC连接openGauss6.0和PostgreSQL16.2性能对比》,作者: Gauss松鼠会小助手。
详细安装步骤请问度娘,输入能正常返回即已安装
- [root@db06 ~]# java -version
- openjdk version "1.8.0_262"
- OpenJDK Runtime Environment (build 1.8.0_262-b10)
- OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
- [root@db06 ~]# javac -version
- javac 1.8.0_262
下载PostgreSQL驱动jar包:https://jdbc.postgresql.org/
下载openGuass驱动jar包:https://opengauss.org/zh/download/
纯java代码,不涉及数据库层,编辑java文件代码,注意文件名需与类名同名
vi HelloWorld.java
- public class HelloWorld{
- public static void main(String[] args){
- System.out.println("Hello World . . . ");
- }
- }
编译运行:
- javac HelloWorld.java
- java HelloWorld
需要用到驱动jar包,测试PG版本是16.2
创建Demo文件,文件名与类名同名
vi DBConnectDemo.java
- import java.sql.*;
-
- public class DBConnectDemo {
-
- public static void main(String[] args){
- // 相关ip,port,database,user,password需进行替换
- String url = "jdbc:postgresql://172.16.107.156:54321/testdb";
- String username = "muser";
- String password = "Test@123";
- try {
- // 加载驱动
- Class.forName("org.postgresql.Driver");
- // 打印数据库版本
- Connection conn = DriverManager.getConnection(url, username, password);
- Statement stmt = conn.createStatement();
- ResultSet result = stmt.executeQuery("select version();");
- while (result.next()){
- System.out.println("DB version : " + result.getString("version"));
- }
-
- long avgtime = 0;
- int n = 10; // 循环次数
-
- // 循环打印连接建立时间
- for(int i=0;i<n;i++){
- long start = System.nanoTime();
- Connection connection = DriverManager.getConnection(url, username, password);
- long end = System.nanoTime() - start;
- System.out.println("connect success: " + end + " 纳秒");
- connection.close();
- avgtime += end;
- }
- System.out.println("avg time: " + avgtime/n + " 纳秒");
- } catch (Exception e){
- e.printStackTrace();
- }
- }
-
- }
编译运行:需要在路径前面加上 .: 来指定路径
javac编译时在同目录下可以不指定驱动jar包
java运行时则需要指定
- javac -cp "/tmp/postgresql-42.5.2.jar" DBConnectDemo.java
- java -cp "/tmp/postgresql-42.5.2.jar" DBConnectDemo (有问题)
- java -cp ".:/tmp/postgresql-42.5.2.jar" DBConnectDemo
- java -cp ".:./*" DBConnectDemo
openGauss出6.0了,下载个极简版来试试
opengauss连接需要调整驱动和数据库信息
- String url = "jdbc:opengauss://172.16.107.156:15400/testdb";
-
- Class.forName("org.opengauss.Driver");
完整代码:
vi OGConnectDemo.java
- import java.sql.*;
-
- public class OGConnectDemo {
-
- public static void main(String[] args){
- // 相关ip,port,database,user,password需进行替换
- String url = "jdbc:opengauss://172.16.107.156:6543/testdb";
- String username = "muser";
- String password = "Test@1234";
- try {
- // 加载驱动
- Class.forName("org.opengauss.Driver");
- // 打印数据库版本
- Connection conn = DriverManager.getConnection(url, username, password);
- Statement stmt = conn.createStatement();
- ResultSet result = stmt.executeQuery("select version();");
- while (result.next()){
- System.out.println("DB version : " + result.getString("version"));
- }
-
- long avgtime = 0;
- int n = 10; // 循环次数
-
- // 循环打印连接建立时间
- for(int i=0;i<n;i++){
- long start = System.nanoTime();
- Connection connection = DriverManager.getConnection(url, username, password);
- long end = System.nanoTime() - start;
- System.out.println("connect success: " + end + " 纳秒");
- connection.close();
- avgtime += end;
- }
- System.out.println("avg time: " + avgtime/n + " 纳秒");
- } catch (Exception e){
- e.printStackTrace();
- }
- }
-
- }
编译运行:OG会打印其他日志信息,这里我们把输出结果写到文件里
- javac -cp ".:/tmp/opengauss-jdbc-6.0.0-RC1.jar" OGConnectDemo.java
- java -cp .:/tmp/opengauss-jdbc-6.0.0-RC1.jar OGConnectDemo > OGConnectDemo.log
- cat OGConnectDemo.log
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。