当前位置:网站首页>Opengauss database JDBC environment connection configuration (eclipse)
Opengauss database JDBC environment connection configuration (eclipse)
2022-06-11 15:50:00 【Gauss squirrel Club】
1. Test environment
Client system : Windows 10
Client software : eclipse 2020-09
Server operating system :openEuler 20.03 64bit with ARM
database edition : openGauss 2.0.0
2. Get ready
2.1 PC End installation configuration JDK11
DOS Window type “java -version”, see JDK edition , Confirmed as JDK11 edition . If not installed JDK, please
Download the installation package from the official website and install .
Configure the system environment variables as follows :
a. Right click “ My computer “, choice “ attribute “.
b. stay “ System “ Click... On the left navigation bar of the page “ Advanced system setup “.
c. stay “ System attribute “ page ,“ senior “ Click... On the tab “ environment variable “.
d. stay “ environment variable “ On the page ,“ System variables “ Area Click “ newly build “ or “ edit “ Configure system variables .
2.2 download JDBC Drive and decompress
Download address :https://opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.0/x86/openGauss-2.0.0-JDBC.tar.gz
3 Conduct eclipse To configure
start-up eclipse, Create a new project and add JDBC drive

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

No need to create “Don’t Create”

Create a lib Directory in openGauss-JDBC Under the project

hold jdbc Drive copy to lib Underside


load jdbc drive

“Add JARs”


stay “Libraries” Next , Choose what you need postgresql.jar file , then “Apply and Close”

Jdbc jar Has been loaded correctly , stay “Referenced Libraries” Next

establish “Java Class”


Copy the prepared code to java Class

package gaussjdbc;
//ogtest.java
// Presentation based on JDBC The main steps of development , It involves creating a database 、 Create table 、 Insert data, etc .
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 {
// Create database connection .
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 {
// Load database driver .
Class.forName(driver).newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
try {
// Create database connection .
conn = DriverManager.getConnection(sourceURL, username, passwd);
System.out.println("Connection succeed!");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return conn;
};
// Carry out ordinary SQL sentence , establish customer_t1 surface .
public static void CreateTable(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
// Carry out ordinary SQL sentence .
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();
}
}
// Execute preprocessing statements , Bulk insert data .
public static void BatchInsertData(Connection conn) {
PreparedStatement pst = null;
try {
// Generate preprocessing statements .
pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
for (int i = 0; i < 3; i++) {
// Add parameter .
pst.setInt(1, i);
pst.setString(2, "data " + i);
pst.addBatch();
}
// Execute batch .
pst.executeBatch();
pst.close();
} catch (SQLException e) {
if (pst != null) {
try {
pst.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
}
// Executing precompiled statements , Update data .
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();
}
}
/**
* The main program , Call each static method step by step .
* @param args
*/
public static void main(String[] args) {
// Create database connection .
Connection conn = GetConnection("joe", "[email protected]");
// Create table .
CreateTable(conn);
// Batch insert data .
BatchInsertData(conn);
// Executing precompiled statements , Update data .
ExecPreparedSQL(conn);
// Close database connection .
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}function java class “Run as -->java application”

Test sample code & Check the operation results
-- Check the client running results
-- Check for database data changes
The code ran successfully , And the database data changes normally , That is, the connection environment is configured .

边栏推荐
- 微软韦青:狗尾巴的故事—数智时代的第一性原理 | 极客时间
- 同学,你听说过MOT吗?
- 使用Cloud DB构建APP 快速入门-快游戏篇
- Export configuration to FTP or TFTP server
- 一文教会你数据库系统调优
- Go language - value types and reference types
- 【sql语句基础】——删(delete) /改(update)
- 实时特征计算平台架构方法论和实践
- Overview and example analysis of opengauss database performance tuning
- 导入数据:gs_restore or MERGE INTO? 看看哪款更适合你
猜你喜欢

Why are bugs changing more and more?
![How to write elegant secondary classification for enterprise development [small case of meituan]](/img/0b/d78b956b93d562d5e63d3fa398b040.png)
How to write elegant secondary classification for enterprise development [small case of meituan]

How AGC security rules simplify user authorization and authentication requests

Opengauss enterprise installation

拿到20K我用了5年,面了所有大厂,这些高频面试问题都帮你们划出来啦

Introduction to JVM basic concepts

数据库资源负载管理(下篇)

openGauss数据库ODBC环境连接配置(Windows)

The third generation Pentium B70 won the C-NCAP five-star safety performance again

openGauss AI能力升级,打造全新的AI-Native数据库
随机推荐
同学,你听说过MOT吗?
导入数据:gs_restore or MERGE INTO? 看看哪款更适合你
Everything about JS functions
Go Language - value type and Reference Type
Unified record of my code variable names
一文教会你数据库系统调优
轻松上手使用gs_dump和gs_dumpall命令导出数据
前沿科技探究之AI工具:Anomaly-detection
After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
Import data to the database? Try the copy from stdin statement
Kaixia was selected into the 2022 global top 100 innovation institutions list of Kerui Weian
Connect to the database using GSQL
数据库设计建议
前沿科技探究之AI在索引推荐的应用
leetcode 120. Triangle minimum path sum
openGauss 多线程架构启动过程详解
YEF 2022昨日开幕,多网络平台全程免费直播,开启在线技术盛宴!
网站上的 breadcrumb 使用场景浅析
Kaixia takes the lead in launching a new generation of UFS embedded flash memory devices that support Mipi m-phy v5.0
[Yugong series] June 2022 Net architecture class 078 worker cluster of distributed middleware schedulemaster