ITPub博客

首页 > 数据库 > 国内数据库 > Gbase JDBC 应用示例

Gbase JDBC 应用示例

国内数据库 作者:123WT321 时间:2020-12-29 14:52:44 0 删除 编辑

1. 使用 JDBC 创建连接

本示例实现了通过 JDBC 建立数据库链接的功能。

package com.gbase.jdbc.simple;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class ConnectionSimple {

public static void main(String[] args) {

ConnectionSimple connectionSimple = new

ConnectionSimple();

connectionSimple.userDriverManagerGetConnection();

}

/**

* 使用 DriverManager 获取连接 .

*/

public void userDriverManagerGetConnection() {

Connection conn = null;

try {

Class.forName("com.gbase.jdbc.Driver");

conn =

DriverManager.getConnection("jdbc:gbase://192.168.5.210:5258/test?us

er=root&password=");

} catch (SQLException ex) {

// 处理错误

System.out.println("SQLException: " +

ex.getMessage());

System.out.println("SQLState: " + ex.getSQLState());

System.out.println("VendorError: " +

ex.getErrorCode());

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (NullPointerException e) {

} catch (Exception e) {

conn = null;

} } } }

 

2. 通过 JDBC 执行 DDL 和 DML 语句

2.1 用例 executeDDLAndDMLSQLByStatement 方法实现以下功能:

使用 Statement 执行 DDL 语句创建一个表;

使用 Statement 执行 DML 语句向表中插入一条数据;

使用 Statement 执行 DML 语句修改 2 中插入的数据。

2.2 用例 executeDDLAndDMLSQLByPreparedStatement 方法实现以下功能:

使用 PreparedStatement 执行 DDL 语句创建一个表;

使用 PreparedStatement 执行 DML 语句向表中插入一条数据;

使用 PreparedStatement 执行 DML 语句修改 2 中插入的数据。

示例如下:

package com.gbase.jdbc.simple;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Statement;

public class ExecuteUpdateSQLByStatement {

private static final String URL =

"jdbc:gbase://192.168.XXX.XX:5258/test?user=sysdba&password=";

/**

* @param args

*/

public static void main(String[] args) {

ExecuteUpdateSQLByStatement executeUpdateSQLByStatement =

new ExecuteUpdateSQLByStatement();

executeUpdateSQLByStatement.executeDDLAndDMLSQLByStateme

nt();

executeUpdateSQLByStatement.executeDDLAndDMLSQLByPrepare

dStatement();

}

/**

* 在 test 数据库中创建一个名称为

* “ user_info ”的表,包含三个字段,

* 并向表中插入三条数据。

*/

public void executeDDLAndDMLSQLByStatement () {

Connection conn = null;

Statement stm = null;

try {

Class.forName("com.gbase.jdbc.Driver");

conn = DriverManager.getConnection(URL);

stm = conn.createStatement();

/*

* create table user_info (

user_id int(11) ,

user_Name varchar(50),

user_info varchar(200)

)ENGINE=GsDB DEFAULT CHARSET=utf8

*/

stm.executeUpdate("drop table if exists `user_info`");

stm.executeUpdate("create table `user_info`

( `user_id` int(11) ,`user_Name` varchar(50),`user_info`

varchar(200))ENGINE=GsDB DEFAULT CHARSET=utf8");

stm.executeUpdate("insert into `user_info`

(`user_id`,`user_name`, `user_info`) values (3,' 张五 ',' 南大通用

-gbase8d')");

stm.executeUpdate("update `user_info` set `user_name`

= ' 张五修改 ' where user_id='3' ");

System.out.println("executeDDLAndDMLSQLByStatement ok");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

stm.close();

} catch (NullPointerException e) {

} catch (Exception e) {

stm = null;

}

try {

conn.close();

} catch (NullPointerException e) {

} catch (Exception e) {

conn = null;

} } }

/**

* 在 test 数据库中创建一个名称为

* “ user_info ”的表,包含三个字段,

* 并向表中插入三条数据。

*/

public void executeDDLAndDMLSQLByPreparedStatement () {

Connection conn = null;

PreparedStatement stm = null;

try {

Class.forName("com.gbase.jdbc.Driver");

conn = DriverManager.getConnection(URL);

stm = conn.prepareStatement("drop table if exists

`user_info`");

/*

* create table user_info (

user_id int(11) ,

user_Name varchar(50),

user_info varchar(200)

)ENGINE=GsDB DEFAULT CHARSET=utf8

*/

stm.addBatch("create table `user_info-2` ( `user_id`

int(11) ,`user_Name` varchar(50),`user_info` varchar(200))ENGINE=GsDB

DEFAULT CHARSET=utf8");

stm.executeBatch();

stm = conn.prepareStatement("insert into `user_info-2`

(`user_id`,`user_name`, `user_info`) values (?,?,?)");

stm.setInt(1, 3);

stm.setString(2, " 张五 ");

stm.setString(3, " 南大通用 -gbase8d");

stm.executeUpdate();

stm = conn.prepareStatement("update `user_info-2` set

`user_name` = ? where user_id=? ");

stm.setString(1, " 张五修改 Prepared");

stm.setInt(2, 3);

stm.executeUpdate();

System.out.println("executeDDLAndDMLSQLByPreparedSta

tement ok");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

stm.close();

} catch (NullPointerException e) {

} catch (Exception e) {

stm = null;

}

try {

conn.close();

} catch (NullPointerException e) {

} catch (Exception e) {

stm = null;

} } } } 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69991979/viewspace-2746265/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2020-12-29

  • 博文量
    4
  • 访问量
    2979