openGauss數(shù)據(jù)庫JDBC環(huán)境連接配置的詳細過程(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.準備
2.1 PC端安裝配置JDK11
DOS窗口輸入“java -version”,查看JDK版本,確認為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”


拷貝準備的代碼到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)文章
Java中增強for循環(huán)在一維數(shù)組和二維數(shù)組中的使用方法
下面小編就為大家?guī)硪黄狫ava中增強for循環(huán)在一維數(shù)組和二維數(shù)組中的使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
SpringBoot項目啟動數(shù)據(jù)加載內(nèi)存的三種方法
一般來說,SpringBoot工程環(huán)境配置放在properties文件中,啟動的時候?qū)⒐こ讨械膒roperties/yaml文件的配置項加載到內(nèi)存中,本文給大家介紹了SpringBoot項目啟動數(shù)據(jù)加載內(nèi)存中的三種方法,需要的朋友可以參考下2024-04-04
SpringCloud實現(xiàn)全鏈路灰度發(fā)布的示例詳解
灰度發(fā)布是指在軟件或服務(wù)發(fā)布過程中,將新版本的功能或服務(wù)以較小的比例引入到生產(chǎn)環(huán)境中,僅向部分用戶或節(jié)點提供新功能的一種發(fā)布策略,下面我們就來學(xué)習(xí)一下SpringCloud如何實現(xiàn)全鏈路灰度發(fā)布2023-11-11
SpringBoot中實現(xiàn)Redis緩存預(yù)熱
緩存預(yù)熱是一種在系統(tǒng)啟動后,但在實際使用前將數(shù)據(jù)加載到緩存中的技術(shù),本文主要來和大家一起探討如何在Spring Boot應(yīng)用程序中實現(xiàn)Redis緩存預(yù)熱,以確保系統(tǒng)在處理請求前就已經(jīng)處于最佳狀態(tài),感興趣的可以了解下2023-11-11
編寫Spring MVC控制器的14個技巧(小結(jié))
這篇文章主要介紹了編寫Spring MVC控制器的14個技巧,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

