Demonstration of Basic JDBC Operations with GBase 8s

Cong Li - Jul 12 - - Dev Community

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

Writing the Java File

Create a Java file for the sample: JdbcSample.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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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

javac JdbcSample.java
Enter fullscreen mode Exit fullscreen mode

This will generate the JdbcSample.class file.

JDBC Test

[gbasedbt@node2 ~]$ java -cp .:gbasedbtjdbc_3.5.1.jar JdbcSample
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player