测试环境
客户端系统: windows 10
客户端软件: eclipse 2020-09
server操作系统:openeuler 20.03 64bit with arm
database版本: opengauss 2.0.0
作者:酷哥
1.客户端安装配置jdk11
dos窗口输入“java -version”,查看jdk版本,确认为jdk11版本。如果未安装jdk,请 从官方网站下载安装包并安装。
根据如下步骤配置系统环境变量:
a. 右键单击“我的电脑“,选择“属性“。
b. 在“系统“页面左侧导航栏单击“高级系统设置“。
c. 在“系统属性“页面,“高级“页签上单击“环境变量“。
d. 在“环境变量“页面上,“系统变量“区域单击“新建“或“编辑“配置系统变量。变量说明请参 见表。
2.下载jdbc驱动并解压
下载地址:https://opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.0/x86/opengauss-2.0.0-jdbc.tar.gz
3.启动eclipse,新建工程并添加jdbc驱动
create a java project
project name: opengauss-jdbc; jre: javase-11
不需要创建“don’t create”
创建一个lib目录在opengauss-jdbc项目下
把jdbc驱动拷贝到lib下边
加载jdbc驱动
“add jars”
在“libraries”下,选中需要的postgresql.jar文件,然后“apply and close”
jdbc jar已经被正确加载,在“referenced libraries”下
创建“java class”
拷贝准备的代码到java类中
运行java类“run as --》java application”
tips: 此次使用eclipse 2020-09创建java class
/*测试代码*/
package gaussjdbc;
//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", "[email protected]");
//创建表。
createtable(conn);
//批插数据。
batchinsertdata(conn);
//执行预编译语句,更新数据。
execpreparedsql(conn);
//关闭数据库连接。
try {
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
}
4.测试示例代码
5.检查运行结果
-- 检查客户端运行结果
--检查数据库数据变化
代码成功运行,且数据库数据变更正常,即连接环境配置完毕。
感谢分享
感谢分享