建议使用以下浏览器,以获得最佳体验。 ie 9.0 以上版本 chrome 31 谷歌浏览器 firefox 30 火狐浏览器
温馨提示

抱歉,您需设置社区昵称后才能参与社区互动!

前往修改
我再想想
选择版块
主题:324帖子:963

【技术干货】

opengauss数据库jdbc环境连接配置(eclipse)

2022/5/25 338

测试环境

客户端系统: 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.检查运行结果

-- 检查客户端运行结果

--检查数据库数据变化

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




回复2

0 0
2022/5/25 11:51

感谢分享

0 0
2022/5/25 13:48

感谢分享

上划加载中
直达楼层
全部回复
正序浏览
标签
您还可以添加5个标签
  • 没有搜索到和“关键字”相关的标签
  • 云产品
  • 星辰平台的解决方案
  • 技术领域
  • 通用技术
  • 平台功能
取消

opengauss数据库jdbc环境连接配置(eclipse)-星辰平台

您已采纳当前回复为最佳回复

发帖: 162粉丝: 5

级别 : 版主,版块专家

[技术干货] opengauss数据库jdbc环境连接配置(eclipse)

测试环境

客户端系统: 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.检查运行结果

-- 检查客户端运行结果

--检查数据库数据变化

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




分享文章到朋友圈

分享文章到微博
您已采纳当前回复为最佳回复

发帖: 0粉丝: 0

发表于2022年05月25日 11:51:15

感谢分享

您已采纳当前回复为最佳回复

jack20

发帖: 520粉丝: 227

发表于2022年05月25日 13:48:49

感谢分享

您需要登录后才可以回帖 | 立即注册

您对问题的回复是否满意?
满意度
非常满意 满意 一般 不满意
我要反馈
0/200