Skip to content

2. Basic Functions#

Database objects can be used with the Altibase JDBC driver in the same manner as using the standard JDBC interface.

This chapter explains how to connect to the database server with an IPv6 address and comparatively describes three statements that can be used in JDBC application programs.

IPv6 Connectivity#

The Altibase JDBC driver supports the use of IPv6 addresses and host names conversible to IPv6 addresses in the JDBC URL.

Overview#

To specify an IPv6 address in the URL, the address must be enclosed in square brackets ("[ ]"). For example, when specifying the localhost to an IP address, brackets are not used for writing the IPv4 address, 127.0.0.1. Meanwhile, brackets are necessary for using the IPv6 address, [::1]. For more detailed information on IPv6 address notation, please refer to Administrator's Manual.

Prerequisites#

java.net.preferIPv4Stack#

To connect with an IPv6 address, the java.net.preferIPv4Stack property must be set to FALSE when the client runs.

If this property is set to TRUE, the client application cannot connect to the database server with an IPv6 address.

$ java -Djava.net.preferIPv4Stack=false sample [::1]

java.net.preferIPv6Addresses#

Regardless of whether the java.net.preferIPv6Addresses property is set to TRUE or FALSE, the Altibase JDBC driver is not affected.

PREFER_IPV6#

If the host name is input to the server_ip property of the URL, the JDBC driver converts the host name to an IPv4 address or an IPv6 address, depending on the value specified for the PREFER_IPV6 property.

If this property is TRUE, and the host name is input to the server_ip property, the client application first converts the host name to an IPv6 address.

If this property is omitted or set to FALSE, however, the client application program first converts the host name to an IPv4 address.

If the client application fails on its first attempt to connect, it will attempt to re-connect with an IP address of a different version from the first.

How to Use IPv6#

For IP addresses of the IPv6 format, the address value is available for specification as it is.

Whether the JDBC driver is to convert the host name to an IPv4 address or an IPv6 address can be specified in the PREFER_IPV6 property.

Properties sProps = new Properties();
...
sProps.put( "PREFER_IPV6", "FALSE");

If the PREFER_IPV6 property is set to FALSE as above, the JDBC driver converts the host name to an IPv4 address. If the PREFER_IPV6 property is TRUE and the host name is given, the application first converts the host name to an IPv6 address.

If this property is omitted or set to FALSE, the client application first converts the host name to an IPv4 address. The basic operation of a JDBC driver is the conversion of the host name to an IPv4 address.

If the client application fails on its first attempt to connect with an IP address of a preferred version, it will attempt to re-connect with an IP address of another version.

Examples#

Connection sCon = null;
Properties sProps = new Properties();

sProps.put( "user", "SYS");
sProps.put( "password", "MANAGER");
sProps.put( "PREFER_IPV6", "FALSE");

String sURL = "jdbc:Altibase://localhost:20300/mydb";
Connection sCon = DriverManager.getConnection( sURL, sProps );


Statement, PreparedStatement and CallableStatement#

Depending on whether or not an in/out parameter is used in a SQL statement or whether or not a SQL statement is directly executed, different Statement objects are available for use in JDBC. The following table shows whether or not the PREPARE function and in/output parameters are available for use for each Statement.

PREPARE IN Parameter OUT Parameter
Statement X X X
PreparedStatement O O X
CallableStatement O O O

Statement#

Statement is mainly used for directly executing static SQL statements.

PreparedStatement#

PreparedStatement is mainly used for preparing the SQL statement before executing it. When the same statement is repeatedly executed, improved performance can be anticipated with the use of PreparedStatement, instead of Statement.

When the PreparedStatement object is created, the Altibase JDBC driver commands the server to PREPARE the statement. If the server fails to PREPARE, an error is returned and the JDBC driver throws an exception.

Unlike Statement, input parameters can be used for PreparedStatement. A parameter is indicated as the "?" character within a SQL statement and can be set to a value with the setXXX() method.

Example#

The following is a code example using IN parameters with PreparedStatement.

PreparedStatement sPrepStmt = sConn.prepareCall("INSERT INTO t1 VALUES (?, ?)");
sPrepStmt.setInt(1, 1);
sPrepStmt.setString(2, "string-value");
sPrepStmt.execute();
sPrepStmt.close();

CallableStatement#

CallableStatement can be used with an input or output parameter. CallableStatement is mainly used for calling a stored procedure or a stored function.

Example#

The following is a code example using an IN parameter and an OUT parameter with CallableStatement.

CallableStatement sCallStmt = connection().prepareCall("{call p1(?, ?)");
sCallStmt.setInt(1, 1);
sCallStmt.registerOutParameter(2, Types.VARCHAR);
sCallStmt.execute();

String sOutVal = sCallStmt.getString(2);
// todo something ...

sCallStmt.close();

Using the National Character Set#

This section offers instructions on how to use national character strings on UNICODE types, such as NCHAR or NVARCHAR types, in JDBC.

Retrieving and Altering Data#

Data of NCHAR/NVARCHAR data types can be retrieved and altered with JDBC in the same manner as retrieving and altering data of CHAR/VARCHAR types. The same methods used for the CHAR data type, such as getString and setString etc., can be used.

Using Constant Strings#

How to use a constant string with a national character in a SQL statement is shown below:

  • Set the NcharLiteralReplace property to TRUE when connecting to the server.
  • To use a national character string in a SQL statement as a constant string, prefix 'N' to the string.

Example#

// create table t1 (c1 nvarchar(1000)); 
Properties sProps; 
sProps.put( "user", "SYS"); 
sProps.put( "password", "MANAGER"); 
sProps.put( "NcharLiteralReplace", "true"); 
Connection sCon = DriverManager.getConnection( sURL, sProps ); 

Statement sStmt = sCon.createStatement(); 
sStmt.execute("insert into t1 values (N'AB가나')"); 
ResultSet sRS = sStmt.executeQuery( "select * from t1 where c1 like N'%가나%'");