Demonstration of Basic JDBC Operations with GBase 8s
Tags: database
Published: 2024-07-12 07:56:46

### Sample Environment

| Software       | Version                |
|----------------|------------------------|
| JDBC Driver    | gbasedbtjdbc_3.5.1.jar |
| JDK            | 1.8                    |

### JDBC Driver Download

The JDBC driver package is typically named: `gbasedbtjdbc_3.5.1.jar`.  
Official download link: [GBase 8s JDBC Driver](https://www.gbase.cn/download/gbase-8s-1?category=DRIVER_PACKAGE)

### Writing the Java File

Create a Java file for the sample: `JdbcSample.java`

```java
import java.sql.*;

public class JdbcSample {
    public static String DRIVER_CLASSNAME = "com.gbasedbt.jdbc.Driver";
    public static String DRIVER_URL = "jdbc:gbasedbt-sqli://192.168.80.70:9088/testdb:GBASEDBTSERVER=gbase01;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=10";
    public static String DRIVER_USER = "gbasedbt";
    public static String DRIVER_PASS = "GBase123$%";

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sqlstr = "";

        Class.forName(DRIVER_CLASSNAME);
        connection = DriverManager.getConnection(DRIVER_URL, DRIVER_USER, DRIVER_PASS);
        System.out.println("Connection succeed!");

        sqlstr = "drop table if exists company";
        preparedStatement = connection.prepareStatement(sqlstr);
        preparedStatement.executeUpdate();
        System.out.println("drop table company succeed!");

        sqlstr = "create table company(coid serial, coname varchar(255), coaddr varchar(255), primary key(coid))";
        preparedStatement = connection.prepareStatement(sqlstr);
        preparedStatement.executeUpdate();
        System.out.println("create table company succeed!");

        sqlstr = "insert into company values(0,?,?),(0,?,?)";
        preparedStatement = connection.prepareStatement(sqlstr);
        preparedStatement.setString(1, "GBase");
        preparedStatement.setString(2, "TJ");
        preparedStatement.setString(3, "GBase BeiJing");
        preparedStatement.setString(4, "BJ");
        preparedStatement.executeUpdate();
        System.out.println("insert table company succeed!");

        sqlstr = "select * from company";
        preparedStatement = connection.prepareStatement(sqlstr);
        resultSet = preparedStatement.executeQuery();
        while(resultSet.next()) {
            System.out.println(resultSet.getObject(1) + "\t" + resultSet.getObject(2) + "\t" + resultSet.getObject(3));
        }
        System.out.println("select table company succeed!");

        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
```

### Property Descriptions

| Property Name     | Description                      | Example Value                                                                                                                                                        |
|-------------------|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| DRIVER_CLASSNAME  | Database driver class name       | `com.gbasedbt.jdbc.Driver`                                                                                                                                           |
| DRIVER_URL        | Database connection string       | `jdbc:gbasedbt-sqli://192.168.80.70:9088/testdb:GBASEDBTSERVER=gbase01;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=10`                          |
| DRIVER_USER       | Database connection user         | `gbasedbt`                                                                                                                                                           |
| DRIVER_PASS       | Password for the user            | `GBase123$%`                                                                                                                                                         |

> Note: The `DRIVER_URL` uses information provided after installation and deployment.

### Compile `JdbcSample.java`

```sh
javac JdbcSample.java
```

This will generate the `JdbcSample.class` file.

### JDBC Test

```sh
[gbasedbt@node2 ~]$ java -cp .:gbasedbtjdbc_3.5.1.jar JdbcSample
```

Expected output:

```
Connection succeed!
drop table company succeed!
create table company succeed!
insert table company succeed!
1       GBase   TJ
2       GBase BeiJing   BJ
select table company succeed!
```

> Note: `-cp` specifies the current directory `.` and `gbasedbtjdbc_3.5.1.jar` as the CLASSPATH.