JDBC环境连接openGauss数据库配置(Eclipse)

182 阅读2分钟

JDBC 环境连接配置 (Eclipse)

1.测试环境

客户端系统: Windows 10

客户端软件: eclipse 2020-09

Server操作系统:openEuler 20.03 64bit with ARM

Database版本: openGauss 2.0.0

2. 客户端安装配置 JDK11

DOS窗口输入“java -version”,查看JDK版本,确认为JDK11版本。如果未安装JDK,请

从官方网站下载安装包并安装。

根据如下步骤配置系统环境变量:

a. 右键单击“我的电脑“,选择“属性“。

b. 在“系统“页面左侧导航栏单击“高级系统设置“。

c. 在“系统属性“页面,“高级“页签上单击“环境变量“。

d. 在“环境变量“页面上,“系统变量“区域单击“新建“或“编辑“配置系统变量。变量说明请参

见表。 3. 下载 JDBC 驱动并解压

下载地址:opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.0/x86/o…

4. 启动 eclipse ,新建工程并添加 JDBC 驱动


Create a java project


image.png

Project name: openGauss-JDBC; JRE: JavaSE-11

image.png

不需要创建“Don’t Create”

  image.png

创建一个lib目录在openGauss-JDBC项目下 image.png

把jdbc驱动拷贝到lib下边

image.png  

image.png

加载jdbc驱动

image.png

“Add JARs”

image.png

image.png

在“Libraries”下,选中需要的postgresql.jar文件,然后“Apply and Close”

  image.png

Jdbc jar已经被正确加载,在“Referenced Libraries”下

image.png

创建“Java Class”

image.png

image.png

拷贝准备的代码到java类中

image.png

运行java类“Run as --》java application”

image.png

image.png

Tips: 此次使用eclipse 2020-09 ****创建 JAVA Class1 /测试代码/

package gaussjdbc;

  5. 测试示例代码

//ogtest.java

//演示基于JDBC开发的主要步骤,会涉及创建数据库、创建表、插入数据等。

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.CallableStatement;

 

public class Gaussjdbc {

 

//创建数据库连接。

public static Connection GetConnection(String username, String passwd) {

String driver = "org.postgresql.Driver";

String sourceURL = "jdbc:postgresql://122.9.34.186:26000/db_tpcc";

Connection conn = null;

try {

  //加载数据库驱动。

  Class.forName(driver).newInstance();

} catch (Exception e) {

  e.printStackTrace();

  return null;

}

 

try {

  //创建数据库连接。

  conn = DriverManager.getConnection(sourceURL, username, passwd);

  System.out.println("Connection succeed!");

} catch (Exception e) {

  e.printStackTrace();

  return null;

}

 

return conn;

};

 

//执行普通SQL语句,创建customer_t1表。

public static void CreateTable(Connection conn) {

Statement stmt = null;

try {

  stmt = conn.createStatement();

 

  //执行普通SQL语句。

  int rc = stmt

      .executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");

 

  stmt.close();

} catch (SQLException e) {

  if (stmt != null) {

    try {

      stmt.close();

    } catch (SQLException e1) {

      e1.printStackTrace();

    }

  }

  e.printStackTrace();

}

}

 

//执行预处理语句,批量插入数据。

public static void BatchInsertData(Connection conn) {

PreparedStatement pst = null;

 

try {

  //生成预处理语句。

  pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");

  for (int i = 0; i < 3; i++) {

    //添加参数。

    pst.setInt(1, i);

    pst.setString(2, "data " + i);

    pst.addBatch();

  }

  //执行批处理。

  pst.executeBatch();

  pst.close();

} catch (SQLException e) {

  if (pst != null) {

    try {

      pst.close();

    } catch (SQLException e1) {

    e1.printStackTrace();

    }

  }

  e.printStackTrace();

}

}

 

//执行预编译语句,更新数据。

public static void ExecPreparedSQL(Connection conn) {

PreparedStatement pstmt = null;

try {

  pstmt = conn

      .prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");

  pstmt.setString(1, "new Data");

  int rowcount = pstmt.executeUpdate();

  pstmt.close();

} catch (SQLException e) {

  if (pstmt != null) {

    try {

      pstmt.close();

    } catch (SQLException e1) {

      e1.printStackTrace();

    }

  }

  e.printStackTrace();

 }

}

 

 

 

/**

* 主程序,逐步调用各静态方法。

* @param args

*/

public static void main(String[] args) {

//创建数据库连接。

Connection conn = GetConnection("joe", "Bigdata@123");

 

//创建表。

CreateTable(conn);

 

//批插数据。

BatchInsertData(conn);

 

//执行预编译语句,更新数据。

ExecPreparedSQL(conn);

 

//关闭数据库连接。

try {

  conn.close();

} catch (SQLException e) {

  e.printStackTrace();

}

}

}

 

6. 检查运行结果

-- 检查客户端运行结果

-- 检查数据库数据变化

代码成功运行,且数据库数据变更正常,即连接环境配置完毕。