Home > other >  Gbase JDBC application example
Gbase JDBC application example

Time:12-30

1. Using the JDBC create connections
This example realize the function of establishing database link through JDBC,
Package com. Gbase. JDBC. Simple;
Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import the Java. SQL. SQLException;
Public class ConnectionSimple {
Public static void main (String [] args) {
ConnectionSimple ConnectionSimple=new
ConnectionSimple ();
ConnectionSimple. UserDriverManagerGetConnection ();
}
/* *
* use the DriverManager get connection.
*/
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=");
{} the catch (SQLException ex)
//processing error
System. Out.println (" SQLException: + "
Ex. GetMessage ());
System. The out. Println (" SQLState: "+ ex. GetSQLState ());
System. Out.println (" VendorError: "+
Ex. GetErrorCode ());
} the catch (a ClassNotFoundException e) {
e.printStackTrace();
} the finally {
Try {
conn.close();
{} catch NullPointerException (e)
} the catch (Exception e) {
Conn=null;
}}}}

2. Through JDBC DDL and DML statements
2.1 cases executeDDLAndDMLSQLByStatement method to realize the following functions:
Using the Statement DDL Statement to create a table;
Use the Statement executing DML statements to insert a table in the data;
Use the Statement executing DML statements modify 2 insert data,
2.2 cases executeDDLAndDMLSQLByPreparedStatement method to realize the following functions:
Using a PreparedStatement DDL statement to create a table;
PreparedStatement executing DML statement to insert a table in the data;
Use PreparedStatement executing DML statements modify 2 insert data,
The sample is as follows:
Package com. Gbase. JDBC. Simple;
Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the 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 ();
}
/* *
* in the test database to create a name for the
* "user_info" table, contains three fields,
* and insert the three data into the table,
*/
Public void executeDDLAndDMLSQLByStatement () {
Connection conn=null;
The 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 the 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, 'zhang five', 'ntu general
- gbase8d ') ");
STM. ExecuteUpdate (" update ` user_info ` set ` user_name `
Where='fives modify' user_id='3' ");
System. The out. Println (" executeDDLAndDMLSQLByStatement ok ");
} the catch (a ClassNotFoundException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
} the catch (SQLException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
} the finally {
Try {
STM. The close ();
{} catch NullPointerException (e)
} the catch (Exception e) {
STM=null;
}
Try {
conn.close();
{} catch NullPointerException (e)
} the catch (Exception e) {
Conn=null;
}}}
/* *
* in the test database to create a name for the
* "user_info" table, contains three fields,
* and insert the three data into the table,
*/
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 the 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
The 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, "fives");
STM. SetString (3, "ntu general - gbase8d");
STM. ExecuteUpdate ();
STM=conn. PrepareStatement (" update ` user_info - 2 ` set
` user_name `=? Where user_id=? ");
STM. SetString (1, "zhang five modified Prepared");
STM. SetInt (2, 3); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related