Gbase JDBC 应用示例
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;
} } } }
本示例实现了通过 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;
} } } }
...全文
相关推荐
jdbc链接gbase数据库的驱动包
在上述示例中,我们没有显示关闭连接的代码,实际应用中应当添加适当的异常处理和关闭逻辑。
GBase8a数据库安装包,文档,驱动包.rar
**GBase JDBC驱动**:“gbase-connector-java-8.3.81.53-build52.8-bin”是GBase 8a的Java数据库连接器,它允许Java应用程序通过JDBC
gbase-jdbc驱动gbase-connector-8.3.81.51和infomix驱动ifxjdbc.jar
`gbase-connector` 是一个Java实现的JDBC驱动程序,它使得Java应用程序能够通过标准的JDBC API连接到GBase数据库,执行SQL查询,进行数据插入、更新和删除等操作。
gbase jdbc 驱动下载 gbase-connector-java
GBase JDBC驱动是连接GBase数据库的重要工具,它允许Java应用程序通过JDBC接口与GBase数据库进行交互。
南大通用Gbase-jdbc-drvier驱动
**南大通用Gbase-jdbc-driver驱动详解**Gbase-jdbc-driver是由南大通用数据技术股份有限公司开发的一款数据库连接驱动,主要用于Java应用程序与Gbase 8系列数据库之间的通信


其他
