openGauss數(shù)據(jù)庫JDBC環(huán)境連接配置的詳細(xì)過程(Eclipse)
1.測試環(huán)境
客戶端系統(tǒng):Windows 10
客戶端軟件:eclipse 2020-09
Server操作系統(tǒng):openEuler 20.03 64bit with ARM
數(shù)據(jù)庫版本:openGauss 2.0.0
2.準(zhǔn)備
2.1 PC端安裝配置JDK11
DOS窗口輸入“java -version”,查看JDK版本,確認(rèn)為JDK11版本。如果未安裝JDK,請
從官方網(wǎng)站下載安裝包并安裝。
根據(jù)如下步驟配置系統(tǒng)環(huán)境變量:
a. 右鍵單擊“我的電腦“,選擇“屬性“。
b. 在“系統(tǒng)“頁面左側(cè)導(dǎo)航欄單擊“高級系統(tǒng)設(shè)置“。
c. 在“系統(tǒng)屬性“頁面,“高級“頁簽上單擊“環(huán)境變量“。
d. 在“環(huán)境變量“頁面上,“系統(tǒng)變量“區(qū)域單擊“新建“或“編輯“配置系統(tǒng)變量。
2.2下載JDBC驅(qū)動并解壓
下載地址:http://xiazai.jb51.net/202206/yuanma/openGaussjdbc_jb51.rar
3 進行eclipse配置
啟動eclipse,新建工程并添加JDBC驅(qū)動

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

不需要創(chuàng)建“Don’t Create”

創(chuàng)建一個lib目錄在openGauss-JDBC項目下

把jdbc驅(qū)動拷貝到lib下邊


加載jdbc驅(qū)動

“Add JARs”


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

Jdbc jar已經(jīng)被正確加載,在“Referenced Libraries”下

創(chuàng)建“Java Class”


拷貝準(zhǔn)備的代碼到j(luò)ava類中

package gaussjdbc;
//ogtest.java
//演示基于JDBC開發(fā)的主要步驟,會涉及創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表、插入數(shù)據(jù)等。
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 {
//創(chuàng)建數(shù)據(jù)庫連接。
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 {
//加載數(shù)據(jù)庫驅(qū)動。
Class.forName(driver).newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
try {
//創(chuàng)建數(shù)據(jù)庫連接。
conn = DriverManager.getConnection(sourceURL, username, passwd);
System.out.println("Connection succeed!");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return conn;
};
//執(zhí)行普通SQL語句,創(chuàng)建customer_t1表。
public static void CreateTable(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
//執(zhí)行普通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();
}
}
//執(zhí)行預(yù)處理語句,批量插入數(shù)據(jù)。
public static void BatchInsertData(Connection conn) {
PreparedStatement pst = null;
try {
//生成預(yù)處理語句。
pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
for (int i = 0; i < 3; i++) {
//添加參數(shù)。
pst.setInt(1, i);
pst.setString(2, "data " + i);
pst.addBatch();
}
//執(zhí)行批處理。
pst.executeBatch();
pst.close();
} catch (SQLException e) {
if (pst != null) {
try {
pst.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
}
//執(zhí)行預(yù)編譯語句,更新數(shù)據(jù)。
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();
}
}
/**
* 主程序,逐步調(diào)用各靜態(tài)方法。
* @param args
*/
public static void main(String[] args) {
//創(chuàng)建數(shù)據(jù)庫連接。
Connection conn = GetConnection("joe", "Bigdata@123");
//創(chuàng)建表。
CreateTable(conn);
//批插數(shù)據(jù)。
BatchInsertData(conn);
//執(zhí)行預(yù)編譯語句,更新數(shù)據(jù)。
ExecPreparedSQL(conn);
//關(guān)閉數(shù)據(jù)庫連接。
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}運行java類“Run as -->java application”

測試示例代碼&檢查運行結(jié)果
-- 檢查客戶端運行結(jié)果
--檢查數(shù)據(jù)庫數(shù)據(jù)變化
代碼成功運行,且數(shù)據(jù)庫數(shù)據(jù)變更正常,即連接環(huán)境配置完畢。

到此這篇關(guān)于openGauss數(shù)據(jù)庫JDBC環(huán)境連接配置(Eclipse)的文章就介紹到這了,更多相關(guān)openGauss數(shù)據(jù)庫JDBC內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成百度AI實現(xiàn)人臉識別的項目實踐
本文主要介紹了SpringBoot集成百度AI實現(xiàn)人臉識別的項目實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
spring boot devtools在Idea中實現(xiàn)熱部署方法
這篇文章主要介紹了spring boot devtools在Idea中實現(xiàn)熱部署方法及注意要點,需要的朋友可以參考下2018-02-02
maven依賴關(guān)系中的<scope>provided</scope>使用詳解
這篇文章主要介紹了maven依賴關(guān)系中的<scope>provided</scope>使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

