java jdbc gbase_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;
} } } }
總結
以上是生活随笔為你收集整理的java jdbc gbase_Gbase JDBC 应用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: libcurl使用样例
- 下一篇: 微信自动抢红包的实现(Demo已增加查看