HDB 6.5.1 JDBC Eng
HDB 6.5.1 JDBC Eng
HDB 6.5.1 JDBC Eng
This manual contains proprietary information of Altibase® Corporation; it is provided under a license agreement containing restric-
tions on use and disclosure and is also protected by copyright patent and other intellectual property law. Reverse engineering of the
software is prohibited.
All trademarks, registered or otherwise, are the property of their respective owners
Altibase Corporation
10F, Daerung PostTower II, 182-13,
Guro-dong Guro-gu Seoul, 152-847, South Korea
Telephone: +82-2-2082-1000 Fax: 82-2-2082-1099
Homepage: http://www.altibase.com
Contents
Preface ............................................................................................................................................................................ v
About This Manual ...................................................................................................................................................................................vi
Audience.......................................................................................................................................................................................vi
Software Environment.............................................................................................................................................................vi
Organization................................................................................................................................................................................vi
Documentation Conventions ............................................................................................................................................. vii
Related Reading.........................................................................................................................................................................ix
Online Manuals ..........................................................................................................................................................................ix
Altibase Welcomes Your Comments..................................................................................................................................ix
1. Starting JDBC..............................................................................................................................................................1
1.1 Installing the JDBC Driver ................................................................................................................................................................2
1.1.1 Version Compatibility.....................................................................................................................................................2
1.1.2 Checking the JDBC Driver Version.............................................................................................................................2
1.1.3 Setting the CLASSPATH .................................................................................................................................................2
1.2 Connecting to the Database...........................................................................................................................................................3
1.2.1 Loading the Driver...........................................................................................................................................................3
1.2.2 Configuring Connection Information.......................................................................................................................3
1.2.3 Connecting to the Database .......................................................................................................................................3
1.2.4 Examples .............................................................................................................................................................................3
1.2.5 Compiling and Running ................................................................................................................................................4
1.3 Connection Information...................................................................................................................................................................5
1.3.1 Setting Connection Attributes ....................................................................................................................................5
1.3.2 About Connection Attributes......................................................................................................................................5
1.4 Using Statement and ResultSet.................................................................................................................................................. 19
1.4.1 Examples .......................................................................................................................................................................... 19
1.5 JDBC Connection Failover ............................................................................................................................................................ 21
2. Basic Functions.........................................................................................................................................................23
2.1 IPv6 Connectivity............................................................................................................................................................................. 24
2.1.1 Overview .......................................................................................................................................................................... 24
2.1.2 Prerequisites ................................................................................................................................................................... 24
2.1.3 How to Use IPv6............................................................................................................................................................. 24
2.1.4 Examples .......................................................................................................................................................................... 25
2.2 Statement, PreparedStatement and CallableStatement................................................................................................... 26
2.2.1 Statement ........................................................................................................................................................................ 26
2.2.2 PreparedStatement...................................................................................................................................................... 26
2.2.3 CallableStatement ........................................................................................................................................................ 26
2.3 Using the National Character Set............................................................................................................................................... 28
2.3.1 Retrieving and Altering Data .................................................................................................................................... 28
2.3.2 Using Constant Strings ............................................................................................................................................... 28
3. Advanced Functions ................................................................................................................................................29
3.1 Auto-generated Keys ..................................................................................................................................................................... 30
3.1.1 How to Use Auto-generated Keys........................................................................................................................... 30
3.1.2 Restrictions...................................................................................................................................................................... 30
3.1.3 Examples .......................................................................................................................................................................... 31
3.2 Timeout ............................................................................................................................................................................................... 32
3.2.1 Login Timeout................................................................................................................................................................ 32
3.2.2 Response Timeout ........................................................................................................................................................ 32
3.3 DataSource......................................................................................................................................................................................... 34
3.3.1 How to set DataSource ............................................................................................................................................... 34
3.3.2 Connecting with DataSource ................................................................................................................................... 34
3.4 Connection Pool .............................................................................................................................................................................. 35
3.4.1 Configuring WAS (Web Application Server)........................................................................................................ 35
3.5 Multiple ResultSet ........................................................................................................................................................................... 38
3.6 JDBC and Failover............................................................................................................................................................................ 39
3.6.1 What is a Failover? ........................................................................................................................................................ 39
iii
3.6.2 How to Use Failover ..................................................................................................................................................... 39
3.6.3 Code Examples .............................................................................................................................................................. 41
3.7 JDBC Escapes..................................................................................................................................................................................... 44
3.8 How to Use ResultSet ..................................................................................................................................................................... 45
3.8.1 Creating ResultSet ........................................................................................................................................................ 45
3.8.2 ResultSet Types.............................................................................................................................................................. 45
3.8.3 Concurrency.................................................................................................................................................................... 46
3.8.4 Holdability ....................................................................................................................................................................... 46
3.8.5 Restrictions...................................................................................................................................................................... 47
3.8.6 Detecting Holes ............................................................................................................................................................. 48
3.8.7 Fetch Size ......................................................................................................................................................................... 49
3.8.8 Refreshing Rows............................................................................................................................................................ 49
3.9 Atomic Batch ..................................................................................................................................................................................... 50
3.9.1 How to Use Atomic Batch .......................................................................................................................................... 50
3.9.2 Restrictions...................................................................................................................................................................... 50
3.9.3 Examples .......................................................................................................................................................................... 50
3.10 Date, Time, Timestamp ............................................................................................................................................................... 52
3.10.1 Meanings ....................................................................................................................................................................... 52
3.10.2 Conversion Table........................................................................................................................................................ 52
3.11 GEOMETRY....................................................................................................................................................................................... 54
3.11.1 How To Use GEOMETRY ........................................................................................................................................... 54
3.11.2 Examples........................................................................................................................................................................ 54
3.12 LOB ..................................................................................................................................................................................................... 55
3.12.1 Prerequisites................................................................................................................................................................. 55
3.12.2 Using BLOB ................................................................................................................................................................... 55
3.12.3 Using CLOB Data......................................................................................................................................................... 60
3.12.4 Freeing Resources ...................................................................................................................................................... 65
3.12.5 Restrictions ................................................................................................................................................................... 67
3.13 Controlling Autocommit ............................................................................................................................................................ 68
4. Tips & Recommendations .......................................................................................................................................69
4.1 Tips for Better Performance......................................................................................................................................................... 70
5. Error Messages .........................................................................................................................................................71
5.1 SQL States.......................................................................................................................................................................................... 72
AppendixA. Data Type Mapping ................................................................................................................................77
Data Type Mapping ............................................................................................................................................................................... 77
Converting Java Data Types to Database Data Types............................................................................................................... 78
Converting Database Data Types to Java Data Types............................................................................................................... 79
iv
Preface
v Preface
About This Manual
This manual explains how to use the ALTIBASE HDB JDBC Driver.
Audience
This manual has been prepared for the following users of ALTIBASE HDB:
• Database administrators
• Application developers
• Programmers
It is recommended that those reading this manual possess the following background knowledge:
• Basic knowledge in the use of computers, operating systems, and operating system utilities
Software Environment
This manual has been prepared assuming that ALTIBASE HDB 6 will be used as the database server.
Organization
This manual is organized as follows:
This chapter offers basic instructions on how to use the ALTIBASE HDB JDBC driver.
This chapter explains how to connect to the database server with an IPv6 address and compar-
atively describes three statements that can be used in JDBC application programs.
This chapter introduces advanced functions provided by the ALTIBASE HDB JDBC driver and
explains how to use them.
This chapter provides instructions for the efficient use of the ALTIBASE HDB JDBC driver.
This chapter lists the SQL States of errors which can occur while using the ALTIBASE HDB JDBC
driver.
This appendix lists the compatibility between ALTIBASE HDB data types and standard JDBC
data types/Java data types.
Documentation Conventions
This section describes the conventions used in this manual. Understanding these conventions will
make it easier to find information in this manual and in the other manuals in the series.
Element Description
SELECT
NOT
vii Preface
Element Description
DROP
ASC
DESC
ASC
DESC
The following table describes the printing conventions used in the code examples.
Lower Case Let- Indicate program elements set by SELECT e_lastname FROM employ-
ters the user, such as table names, col- ees;
umn names, file names, etc.
Upper Case Let- Keywords and all elements pro- DESC SYSTEM_.SYS_INDICES_;
ters vided by the system appear in
upper case.
Related Reading
For additional technical information, please refer to the following manuals:
• Administrator’s Manual
• Replication Manual
Online Manuals
Online versions of our manuals (PDF or HTML) are available from Altibase's Customer Support site
(http://support.altibase.com/).
When you send your feedback, please make sure to include the following information:
• The name and version of the manual that you are using
ix Preface
• Any comments that you have about the manual
For immediate assistance with technical issues, please contact Altibase’s Customer Support site
(http://support.altibase.com/).
1 Starting JDBC
1.1 Installing the JDBC Driver
Once the package is installed, the ALTIBASE HDB JDBC driver can be found in the
$ALTIBASE_HOME/lib directory.
Class.forName("Altibase.jdbc.driver.AltibaseDriver");
2. Acquire the driver from the DriverManager with the connection URL.
The format of the connection URL used for the argument of the connect method above is shown
below:
jdbc:Altibase://server_ip:server_port/dbname
1.2.4 Examples
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionExample
{
public static void main(String[] args) throws Exception
{
String sURL = "jdbc:Altibase://localhost:20300/mydb";
Properties sProps = new Properties();
sProps.put("user", "SYS");
3 Starting JDBC
1.2 Connecting to the Database
sProps.put("password", "MANAGER");
Class.forName("Altibase.jdbc.driver.AltibaseDriver");
Driver sDriver = DriverManager.getDriver(sURL);
Connection sCon = sDriver.connect(sURL, sProps);
}
}
$ javac ConnectionExample.java
$ java ConnectionExample
"jdbc:Altibase://localhost:20300/mydb?fetch_enough=0&time_zone=DB_TZ"
...
• Setting range: Whether the attribute which is set affects the whole system or only affects the
given session.
• Description
5 Starting JDBC
1.3 Connection Information
1.3.2.1 alternateservers
Default Value
Mandatory No
Setting Range
Description The list of servers available for connection in the event of a Connec-
tion Failover. For instructions on its use, please refer to “JDBC and
Failover” in Chapter 3.
1.3.2.2 app_info
Default Value
Mandatory No
1.3.2.3 auto_commit
Mandatory No
1.3.2.4 ciphersuite_list
Default Value Please refer to the list of supported cipher suites for JRE.
Mandatory No
1.3.2.5 clientside_auto_commit
Mandatory No
1.3.2.6 connectionretrycount
Default Value 0
Mandatory No
Setting Range
1.3.2.7 connectionretrydelay
Default Value 0
Mandatory No
Setting Range
7 Starting JDBC
1.3 Connection Information
1.3.2.8 database
Mandatory No
Description The name of the database created in the HDB server to be connected
1.3.2.9 datasource
Default Value
Range [0 - 65535]
1.3.2.10 date_format
Mandatory No
Description Sets the in/output format of the DATE type. If the format is set and a
data of another format is input by the client, instead of treating it as a
DATE type, an error is returned.
1.3.2.11 description
Default Value
Mandatory No
1.3.2.12 ddl_timeout
Default Value 0
Mandatory No
Description Sets the time limit for the execution of DDL statements. Queries
which exceed the execution time limit are automatically terminated.
The unit is seconds.
The value 0 indicates infinity.
1.3.2.13 failover_source
Default Value
Mandatory No
Setting Range
1.3.2.14 fetch_enough
Default Value 0
Range [0 - 2147483647]
Mandatory No
9 Starting JDBC
1.3 Connection Information
Description Sets the FetchSize of the current session. This indicates the number of
rows to be retrieved per FETCH from the server.
If this value is 0, the JDBC driver fetches the maximum size of data
that can be contained in one network packet from the server.
1.3.2.15 fetch_timeout
Default Value 60
Mandatory No
Description Sets the time limit for the execution of SELECT statements. Queries
which exceed the execution time limit are automatically terminated.
The unit is seconds.
The value 0 indicates infinity.
1.3.2.16 healthcheckduration
Default Value 30
Mandatory No
Setting Range
Description Specifies the interval after which a server on which a Failover has
occurred is re-listed on the AlternativeServer list.
If a Connection Failover occurs on a server, the server is removed from
the AlternativeServer list. After a certain length of time, the
removed server is re-added to the AlternativeServer list and
this interval is determined by the HealthCheckDuration prop-
erty.
The unit is seconds.
For instructions on its use, please refer to “JDBC and Failover” in Chap-
ter 3.
1.3.2.17 idle_timeout
Default Value 0
Mandatory No
Description Sets the time limit during which the Connection stays connected
without performing any operations. The connection is automatically
released when the time is up.
The unit is seconds.
The value 0 indicates infinity.
1.3.2.18 isolation_level
Default Value
Range [2 | 4 | 8]
Mandatory No
Description 2: TRANSACTION_READ_COMMITTED
4: TRANSACTION_REPEATABLE_READ
8: TRANSACTION_SERIALIZABLE
1.3.2.19 keystore_password
Mandatory No
1.3.2.20 keystore_type
Mandatory No
11 Starting JDBC
1.3 Connection Information
1.3.2.21 keystore_url
Mandatory No
Description Specifies the path to the keystore (a container for its own private key
and the certificates with their corresponding public keys).
1.3.2.22 loadbalance
Mandatory No
Setting Range
1.3.2.23 lob_cache_threshold
Range [0 - 8192]
Mandatory No
Description Sets the maximum size of the LOB data to be cached on the client.
1.3.2.24 login_timeout
Default Value
Mandatory No
Description Sets the maximum waiting time for logging in. For further informa-
tion, please refer to “Timeout” in Chapter 3.
1.3.2.25 max_statements_per_session
Default Value
Mandatory No
1.3.2.26 ncharliteralreplace
Mandatory No
1.3.2.27 password
Default Value
Range
Mandatory Yes
1.3.2.28 port
13 Starting JDBC
1.3 Connection Information
Range [0 - 65535]
Mandatory No
1.3.2.29 prefer_ipv6
Mandatory No
Setting Range
1.3.2.30 privilege
Default Value
Mandatory No
1.3.2.31 query_timeout
Mandatory No
Description Sets the time limit for query execution. Queries which exceed the exe-
cution time limit are automatically terminated.
The unit is seconds.
The value 0 indicates infinity.
1.3.2.32 remove_redundant_transmission
Default Value 0
Range [0 | 1]
Mandatory No
Description Sets whether or not to use the duplicate data compression method
for CHAR, VARCHAR, NCHAR, NVARCHAR type strings.
1.3.2.33 response_timeout
Default Value
Mandatory No
Description Sets the maximum waiting time for a response. For further informa-
tion, please refer to “Timeout” in Chapter 3.
1.3.2.34 sessionfailover
Mandatory No
Setting Range
Description Sets whether or not to use STF (Session Time Failover). For instruc-
tions on its use, please refer to “JDBC and Failover” in Chapter 3.
1.3.2.35 server
Mandatory Yes
15 Starting JDBC
1.3 Connection Information
1.3.2.36 ssl_enable
Mandatory No
Description Specifies whether or not to connect to the database over SSL connec-
tion. For further information, please refer to the Altibase SSL/TLS User’s
Guide.
1.3.2.37 time_zone
Range
Mandatory No
Description Sets the time zone. For further information, please refer to the
TIME_ZONE property in General Reference.
1.3.2.38 truststore_password
Mandatory No
1.3.2.39 truststore_type
Mandatory No
1.3.2.40 truststore_url
Mandatory No
1.3.2.41 user
Default Value
Range
Mandatory Yes
1.3.2.42 utrans_timeout
Mandatory No
Description Sets the time limit for the execution of UPDATE statements. Queries
which exceed the execution time limit are automatically terminated.
The unit is seconds.
The value 0 indicates infinity.
1.3.2.43 verify_server_certificate
Mandatory No
17 Starting JDBC
1.3 Connection Information
1.4.1 Examples
import java.util.Properties;
import java.sql.*;
//...
// Retrieve ResultSet
while(sRs.next())
{
for(int i=1; i<=sRsMd.getColumnCount(); i++)
{
// Get Actual Data and Printout
System.out.println(sRs.getObject(i));
}
}
19 Starting JDBC
1.4 Using Statement and ResultSet
// Retrieve ResultSet
while(sRs.next())
{
for(int i=1; i<=sRsMd.getColumnCount(); i++)
{
// Get Actual Data and Printout
System.out.println(sRs.getObject(i));
}
}
// Retrieve ResultSet
while(sRs.next())
{
for(int i=1; i<=sRsMd.getColumnCount(); i++)
{
// Get Actual Data and Printout
System.out.println(sRs.getObject(i));
}
}
// Eliminate Resources
sRs.close();
sStmt.close();
In the event of such a failure, the client which connected to the server on which the failure occurred
detects the situation and automatically connects to another server and processes the statements
that were being executed; this process is called a Fail-Over.
For instructions on how to use the Fail-Over feature in JDBC applications, please refer to Chapter 4.
of Replication Manual.
21 Starting JDBC
1.5 JDBC Connection Failover
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.
23 Basic Functions
2.1 IPv6 Connectivity
2.1.1 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 fur-
ther information on IPv6 address notation, please refer to Administrator’s Manual.
2.1.2 Prerequisites
2.1.2.1 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.
2.1.2.2 java.net.preferIPv6Addresses
Regardless of whether the java.net.preferIPv6Addresses property is set to TRUE or FALSE, the ALTI-
BASE HDB JDBC driver is not affected.
2.1.2.3 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.
Whether the JDBC driver is to convert the host name to an IPv4 address or an IPv6 address can be
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.
2.1.4 Examples
Connection sCon = null;
Properties sProps = new Properties();
25 Basic Functions
2.2 Statement, PreparedStatement and CallableStatement
Statement X X X
PreparedStatement O O X
CallableStatement O O O
2.2.1 Statement
Statement is mainly used for directly executing static SQL statements.
2.2.2 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 HDB 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.
2.2.2.1 Examples
The following is a code example using IN parameters with PreparedStatement.
2.2.3 CallableStatement
CallableStatement can be used with an input or output parameter. CallableStatement is mainly used
for calling a stored procedure or a stored function.
2.2.3.1 Examples
The following is a code example using an IN parameter and an OUT parameter with CallableState-
ment.
sCallStmt.close();
27 Basic Functions
2.3 Using the National Character Set
• To use a national character string in a SQL statement as a constant string, prefix ‘N’ to the
string.
2.3.2.1 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 );
29 Advanced Functions
3.1 Auto-generated Keys
In ALTIBASE HDB, a sequence can act as auto-generated keys. This section explains how to obtain
the values of auto-generated keys in JDBC.
Or, after having created the PreparedStatement object with a method specifying the column for
which auto-generated keys are to be obtained and executing it, the ResultSet of the auto-generated
keys can be retrieved with the getGeneratedKeys() method.
The following are Statement methods that execute SQL statements which retrieve auto-generated
keys.
The following are Connection methods that create the PreparedStatement object which retrieve
auto-generated keys.
After having executed a SQL statement in one of the above two ways, auto-generated keys can be
obtained by a ResultSet object with the following Statement method.
3.1.2 Restrictions
When obtaining auto-generated keys in ALTIBASE HDB, the following restrictions apply:
• Since ALTIBASE HDB does not support columns with the AUTO INCREMENT property, auto-
generated keys can only be obtained from a sequence.
The following is an example of a SQL statement from which auto-generated keys can be obtained.
The following is an example of a SQL statement from which auto-generated keys cannot be
obtained.
If a SQL statement which does not produce auto-generated keys is executed with the generator flag
(Statement.RETURN_GENERATED_KEYS), the flag is ignored and the getGeneratedKeys()
method returns an empty result set.
3.1.3 Examples
sStmt.executeUpdate(sQstr, Statement.RETURN_GENERATED_KEYS);
ResultSet sKeys = sStmt.getGeneratedKeys();
while (sKeys.next())
{
int sKey = sKeys.getInt(1);
// do somethings...
}
sKeys.close();
sStmt.close();
31 Advanced Functions
3.2 Timeout
3.2 Timeout
This section gives an explanation of timeouts which can occur in a client session connected to the
ALTIBASE HDB server and provides code examples to show how to set properties related to time-
outs.
1. Create a Connection object with the Properties object to which the timeout property has been
added.
2. Create a Connection object with a connection URL which specifies the timeout property.
This value is applied to all methods which communicate with the server.
1. Create a Connection object with a Properties object to which the timeout property has been
added.
2. Create a Connection object with a connection URL which specifies the timeout property.
// Linux
export ALTIBASE_RESPONSE_TIMEOUT=100
// Windows
set ALTIBASE_RESPONSE_TIMEOUT=100
33 Advanced Functions
3.3 DataSource
3.3 DataSource
The ALTIBASE HDB JDBC driver offers a way to connect to the database with a file that contains con-
nection configurations. Datasource is the set of connection information to a database server in the
configuration file.
# comment
[ datasource_name ]
Server=localhost # comment
Port=20300
User=sys
Password=manager
Additional connection properties can be added by writing strings of the “key=value” format in lines.
The JDBC driver searches for the altibase_cli.ini file in the paths of the following order:
1. /altibase_cli.ini
2. $HOME/altibase_cli.ini
3. $ALTIBASE_HOME/conf/altibase_cli.ini
jdbc:Altibase://datasource_name
jdbc:Altibase://datasource_name:20301
jdbc:Altibase://datasource_name:20301?sys=user&password=pwd
When specifying a DSN in a connection URL, port or other properties can be additionally specified. If
a property specified in the altibase_cli.ini file is duplicately specified in a connection URL,
however, the file value is ignored and the connection URL value is used.
• Tomcat 8.x
• WebLogic 12.x
• Jeus 6.x
For further information on how to configure and use the connection pool and JDBC driver on each
web application, please refer to their manuals.
Context configuration
<Context>
</Context>
web.xml configuration
Code example
35 Advanced Functions
3.4 Connection Pool
You can configure the connection pool and the JDBC datasource by referring to the following links:
• http://docs.oracle.com/middleware/1213/wls/WLACH/taskhelp/jdbc/jdbc_datasources/Cre-
ateDataSources.html
• http://docs.oracle.com/middleware/1213/wls/WLACH/pagehelp/JDBCjdbcdatasourcesjdbc-
datasourceconfigconnectionpooltitle.html
• URL: jdbc:Altibase://localhost:20300/mydb
</property>
<property>
<name>DatabaseName</name>
<type>java.lang.String</type>
<value>mydb</value>
</property>
<property>
<name>User</name>
<type>java.lang.String</type>
<value>SYS</value>
</property>
</database>
</data-source>
</resource>
37 Advanced Functions
3.5 Multiple ResultSet
The following is an example of a code which uses multiple result sets returned by a call to a PSM in a
JDBC application.
do{
sRs = sCallStmt.getResultSet();
sRsMd = sRs.getMetaData();
if(sRsMd != null)
{
while(sRs.next())
{
// do something
for(int i=1; i <= sRsMd.getColumnCount(); i++)
{
System.out.println(sRs.getString(i));
}
}
}
}while(stmt.getMoreResults());
sCallStmt.close();
CTF attempts to connect to another server when an attempt to connect to the database is
unsuccessful. CTF can occur when the connect method of a Connection object is called.
STF connects to another server and continuously executes the user-specified operation when
a connection error occurs before the result of a SQL statement is received from the server. STF
can occur on the execution of all methods communicating with the server, excluding the con-
nect method.
For further information on Failover, please refer to the “Failover” chapter of Replication Manual.
3.6.2.1 CTF
The CTF feature can be used by adding the following properties to the Properties object.
For further information on each of the properties, please refer to “Connection Information” of Chap-
ter 1.
3.6.2.2 STF
The STF feature can be used by additionally setting "SessionFailover=on" to the properties which set
the CTF feature.
In communication situations other than attempting to establish connection to the database server,
39 Advanced Functions
3.6 JDBC and Failover
the client first processes CTF and restores the connection when it detects server failure. Thereafter,
the client executes the callback function registered by the user and raises a Failover Success Excep-
tion for the user to acknowledge that a Failover has occurred. If Failover fails to every server, the
driver throws the Exception which originally occurred.
The following is an interface for the Failover callback function written by the user.
The following is a code example which shows the process of a user registering and freeing a Failover
callback function.
...
}
If the Failover callback function written by the user is called by the JDBC driver, one of the Event con-
stants included in the above AltibaseFailoverCallback interface is passed to aFailoverEvent, which is
the third argument of the callback function. The meaning of each Event constant is as follows:
The Result constants included in the AltibaseFailoverCallback interface are values which can be
returned by the callback function written by the user. If values other than Result constants are
returned from the callback function, Failover does not operate normally.
• Result.GO: If this constant value is returned from the callback function, the JDBC driver con-
tinually runs the next process of STF.
• Result.QUIT: If this constant value is returned from the callback function, the JDBC driver
The following is a code example of an object which can be used as the second argument of the
Failover callback function written by the user.
If there is a need to use information of an application implemented by the user during the STF pro-
cess, the object to be passed to the callback function while registering the Failover callback function
can be specified. If this object is specified as the second argument of the registerFailoverCallback
method which registers the callback function, this object is passed when the callback function is
actually called. The following is an example which depicts this process in code.
...
...
The following is an example of a simple code which is regardless of various circumstances; therefore,
it should be noted that it cannot be used as it is in user applications.
switch (aFailoverEvent)
{
// Necessary operations before starting Failover on the user
application logic can be executed.
case Event.BEGIN:
System.out.println(“Failover Started .... “);
break;
// Necessary operations after completing Failover on the user
application logic can be executed.
case Event.COMPLETED:
try
{
41 Advanced Functions
3.6 JDBC and Failover
sStmt = aConnection.createStatement();
}
catch( SQLException ex1 )
{
try
{
sStmt.close();
}
catch( SQLException ex3 )
{
}
return Result.QUIT;
}
try
{
sRes = sStmt.executeQuery("select 1 from dual");
while(sRes.next())
{
if(sRes.getInt(1) == 1 )
{
break;
}
}
}
catch ( SQLException ex2 )
{
try
{
sStmt.close();
}
catch( SQLException ex3 )
{
}
// Terminates the Failover process.
return Result.QUIT;
}
break;
}
// Continues the Failover process.
return Result.GO;
}
}
The following is a code example which checks whether or not STF was successful. Whether STF suc-
ceeded or failed can be confirmed by checking whether ErrorCode of SQLException is identical to
Validation.FAILOVER_SUCCESS. The Failover validation code is inserted inside the while
loop because the operation which was previously under execution must be executed again, even if
Failover succeeds.
catch (SQLException e)
{
// Whether or not the Failover has succeeded.
if (e.getErrorCode() == AltibaseFailoverCallback.FailoverValida-
tion.FAILOVER_SUCCESS)
{
// Since Failover has succeeded, Exception is ignored and the pro-
cess is continued.
continue;
}
System.out.println("EXCEPTION : " + e.getMessage());
}
break;
}
43 Advanced Functions
3.7 JDBC Escapes
The following table is an organization of SQL statements which include the escape syntax supported
by the JDBC specification and SQL statements converted by the JDBC driver for use in ALTIBASE HDB.
SQL statements supported in the JDBC SQL statements converted for use in
Type
specification ALTIBASE HDB
The following are ResultSet object types available for user specification.
• TYPE_FORWARD_ONLY
Unscrollable; the cursor can be moved only forward. Data of the ResultSet is determined at the
point in time at which the cursor opens in the database server.
• TYPE_SCROLL_INSENSITIVE
Scrollable; the cursor can be moved forward, backwards, or moved to a specified location. Data
of the ResultSet is determined at the point in time at which the cursor opens in the database
server. Memory can become scarce, due to caching the ResultSet retrieved from the server on
the client.
• TYPE_SCROLL_SENSITIVE
Scrollable; the cursor can be moved forward, backwards, or moved to a specified location. The
ResultSet is determined at the point in time at which the cursor opens in the database server;
however, data within the ResultSet is determined at the point in time at which the client
45 Advanced Functions
3.8 How to Use ResultSet
3.8.3 Concurrency
This option determines whether or not to allow updates through the ResultSet object. One of the
following two constants is available for use:
• CONCUR_READ_ONLY
• CONCUR_UPDATABLE
3.8.4 Holdability
This option determines whether or not to retain the ResultSet object after the transaction has been
committed. One of the following two constants are available for use:
• CLOSE_CURSORS_AT_COMMIT
• HOLD_CURSORS_OVER_COMMIT
The cursor is left open, even if the transaction is committed. If the transaction has been com-
mitted at least once after the cursor has been opened, the cursor is left open during future
commit and rollback operations. If the transaction has not been committed even once since
the cursor has been opened, however, the cursor is closed when the transaction is rolled back.
3.8.4.1 Notes
• Since the JDBC driver caches as many number of rows as the value set for FetchSize for the
ResultSet object on the client, data left in the cache can be retrieved by the application, even if
the cursor is closed. If you want the application to immediately detect that the cursor has been
closed, set FetchSize to 1.
• The default value of Holdability for the ALTIBASE HDB JDBC driver is
CLOSE_CURSORS_AT_COMMIT, and is different from the default value for the JDBC specifi-
cation, HOLD_CURSORS_OVER_COMMIT.
Open ResultSet objects must be closed prior to switching the autocommit mode with the
setAutoCommit() method in a session where Holdability is
HOLD_CURSORS_OVER_COMMIT. The following is a code example which raises an error.
sCon = getConnection();
sStmt = sCon.createStatement();
byte[] br;
byte[] bb = new byte[48];
for(byte i = 0; i < bb.length;i++) bb[i] = i;
sCon.setAutoCommit(false);
sCon.commit();
sCon.setAutoCommit(true); -> 1
3.8.4.2 Examples
Statement sUpdStmt = sConn.prepareStatement("UPDATE t1 SET val = ? WHERE id =
?");
Statement sSelStmt = sConn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
ResultSet sRS = sSelStmt.executeQuery("SELECT * FROM t1");
while (sRS.next())
{
// TODO : set parameters
sUpdStmt.execute();
sConn.commit();
}
sRS.close();
3.8.5 Restrictions
To use an Updatable ResultSet or a Scrollable ResultSet, a SELECT query statement which retrieves
the ResultSet is restricted in the following ways:
47 Advanced Functions
3.8 How to Use ResultSet
• Only pure columns can be specified in the SELECT list; expressions or functions cannot be
included. Columns with a NOT NULL constraint, and without a default value must be included
in the SELECT list.
When executing PSM, only ResultSet objects of the default type are available for use. If the user
specifies an option which is not of the default type, the option is ignored.
Since for a ResultSet object which is CONCUR_UPDATABLE and TYPE_SCROLL_SENSITIVE, one more
Statement is used within the JDBC driver, it can easily exceed the limited number of Statements;
therefore, the maximum number of Statements must be set for occasions on which such ResultSet
types are used a lot.
Since an updateable and scrollable ResultSet contains a large amount of data, its memory usage is
higher than a forward only ResultSet. A large ResultSet can cause memory to become scarce, so its
use is not recommended.
The characteristics of the ResultSet are determined by the ResultSet type, concurrency type and
holdability type described above. The user can specify random combinations for these three values;
however, depending on the query statement that generates the ResultSet, the user-defined combi-
nation can be invalid. In this case, the driver does not raise an exception, but converts it to a valid
combination. In the following example, the invalid types on the left side are automatically converted
to the valid types on the right side.
When a conversion is made internally, whether or not a conversion has occurred can be confirmed
through warnings.
while (sRS.next())
{
if (sRS.rowDeleted())
{
// HOLE DETECTED!!!
}
else
{
// do something ...
}
}
Valid data cannot be obtained from a Hole, and a ResultSet returns one of the following values for a
Hole: a SQL data type NULL, a reference type NULL, or the value 0.
A value between the range of 0 to 2147483647 can be set for the ALTIBASE HDB JDBC driver. The
JDBC specification defines that an exception must be raised when a value outside of this range is
specified; however, the ALTIBASE HDB JDBC driver does not raise an exception and ignores it, for the
sake of convenience.
If the value is set to 0, the ALTIBASE HDB server voluntarily determines the size to return to the client
in one go. In this case, the number of rows to be returned differ, according to the size of a row.
The FetchSize value is especially important for the Scroll-Sensitive ResultSet. When the user retrieves
data from a Scroll-Sensitive ResultSet, the driver returns the prefetched rows first. Even if data of the
database has been updated, as long as the row exists in the prefetched cache, data of the cache is
returned to the user. If the user wants to see the newest data of the database, FetchSize should be
set to 1. By doing so, however, the frequency of retrieving data from the server increases and perfor-
mance can be lowered.
This method operates when the ResultSet object is of the following types:
49 Advanced Functions
3.9 Atomic Batch
This section explains how to use the Atomic Batch feature which the Altibase JDBC driver supports.
The following method, setAtmoicBatch(), can be used to enable the Atomic Batch feature.
To confirm whether or not Atomic Batch is set for the PreparedStatement object, call the getAtomic-
Batch() method as below.
3.9.2 Restrictions
When using the Atomic Batch feature in ALTIBASE HDB, the following restrictions apply:
• Only supports simple INSERT statements. Consistency for complex INSERT statements or DML
statements, such as UPDATE, DELETE, etc., cannot be assured.
• If a trigger fires with Each Statement as the unit, the trigger fires only once.
3.9.3 Examples
......
Connection con = sConn = DriverManager.getConnection(aConnectionStr, mProps);
Statement stmt = con.createStatement();
try
{
stmt.execute("Drop table " + TABLE_NAME); } catch (SQLException e) { }
stmt.execute("create table " + TABLE_NAME + "(c1 VARCHAR (1000))");
if(i%BATCH_SIZE == 0)
{
sPrepareStmt.executeBatch();
con.commit();
}
}
con.commit();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
......
51 Advanced Functions
3.10 Date, Time, Timestamp
3.10.1 Meanings
• Date: Expresses only the date
• Timestamp: Expresses the date, time, seconds and further subdivisions of time
setDate() - 2134-12-23 - -
00:00:00.0
The values input to
the hour:min-
ute:second are
ignored by the
driver.
setTime() - - 2134-12-23 -
12:34:56.0
setTime- - - - 2134-12-23
stamp() 12:34:56.1234
56
The following table shows the values returned from the DATE type value (1234-01-23
12:23:34.567123) stored in the database with the getDate(), getTime(), and getTime-
stamp() methods.
53 Advanced Functions
3.11 GEOMETRY
3.11 GEOMETRY
This section offers instructions on how to use GEOMETRY type data provided by ALTIBASE HDB in
JDBC applications.
When inserting data(including NULL) to a GEOMETRY type column in the database with the IN
parameter of PreparedStatement, the data type must be specified with the AltibaseTypes.GEOME-
TRY constant.
For further information on how to directly write GEOMETRY type data in a query statement, please
refer to Spatial SQL Reference.
3.11.2 Examples
The following is code example which inserts data to a GEOMETRY type column in a JDBC application.
...
3.12 LOB
This sections offers instructions on how to use LOB type data provided by ALTIBASE HDB in a JDBC
application.
3.12.1 Prerequisites
• ALTIBASE HDB supports the LOB data types, BLOB and CLOB, and each can have the maximum
size of 2Gbytes.
To manipulate LOB data, the autocommit mode of a session must satisfy one of the following condi-
tions.
...
...
...
sPstmt.execute();
...
...
55 Advanced Functions
3.12 LOB
while(sRs.next())
{
Blob sBlob = sPstmt.getBlob(1);
OutputStream sOutputStream = sBlob.setBinaryStream(1);
sOutputStream.write(sBuf);
sOutputStream.close();
...
}
...
sPstmt.execute();
...
...
...
sPstmt.setBlob(1, sBlob);
...
sPstmt.execute();
...
...
sPstmt.setObject(1, sBlob);
...
sPstmt.execute();
...
...
sPstmt.setObject(1, sBlob);
...
sPstmt.execute();
...
...
while(sRs.next())
{
...
sRs.updateBinaryStream(1, sInputStream, sLength);
sRs.updateRow();
...
}
...
...
while(sRs.next())
{
...
sRs.updateBlob(1, sBlob), ;
sRs.updateRow();
...
}
...
57 Advanced Functions
3.12 LOB
...
while(sRs.next())
{
...
sRs.updateObject(1, sBlob);
sRs.updateRow();
...
}
...
...
while(sRs.next())
{
...
sRs.updateObject(1, sBlob, AltibaseTypes.BLOB);
sRs.updateRow();
...
}
...
3.12.2.3 Updating BLOB Data with the SELECT … FOR UPDATE Statement
byte[] sBytes = new byte[sLength];
...
while(sRs.next())
{
...
Blob sBlob = sRs.getBlob(1);
sBlob.setBytes(0, sBytes);
...
}
...
...
while(sRs.next())
{
...
InputStream sInputStream = sRs.getBinaryStream(1);
...
}
...
...
while(sRs.next())
{
...
Blob sBlob = sRs.getBlob(1);
InputStream sInputStream = sBlob.getBinaryStream();
...
}
...
...
final int sReadLength = 100;
while(sRs.next())
{
...
Blob sBlob = sRs.getBlob(1);
long sRemains = sBlob.length();
long sOffset = 0;
while(sRemains > 0)
{
byte[] sReadBytes = sBlob.getBytes(sOffset, sReadLength);
sRemains -= sReadBytes.length;
sOffset += sReadBytes.length;
...
}
59 Advanced Functions
3.12 LOB
...
}
...
Truncation
while(sRs.next())
{
...
int sLength = ... ;
Blob sBlob = sRs.getBlob(2);
...
...
...
...
sPstmt.execute();
...
...
while(sRs.next())
{
Clob sClob = sPstmt.getClob(1);
Writer sWriter = sClob.setCharacterStream(1);
sWriter.write(sBuf);
sWriter.close();
...
}
...
sPstmt.execute();
...
...
...
sPstmt.setClob(1, sClob);
...
sPstmt.execute();
...
61 Advanced Functions
3.12 LOB
...
sPstmt.setObject(1, sClob);
...
sPstmt.execute();
...
...
...
...
sPstmt.execute();
...
...
while(sRs.next())
{
...
sRs.updateCharacterStream(1, sReader, sLength);
sRs.updateRow();
...
...
...
while(sRs.next())
{
...
sRs.updateClob(1, sClob);
sRs.updateRow();
...
}
...
...
while(sRs.next())
{
...
sRs.updateObject(1, sClob);
sRs.updateRow();
...
}
...
...
while(sRs.next())
{
...
sRs.updateObject(1, sClob, AltibaseTypes.CLOB);
sRs.updateRow();
63 Advanced Functions
3.12 LOB
...
}
...
3.12.3.3 Inserting CLOB data With the SELECT … FOR UPDATE Statement
...
while(sRs.next())
{
...
Clob sClob = sRs.getClob(1);
sClob.setString(0, sStr);
...
}
...
...
while(sRs.next())
{
...
Reader sReader = sRs.getCharacterStream(1);
...
}
...
...
while(sRs.next())
{
...
Clob sClob = sRs.getClob(1);
Reader sReader = sClob.getCharacterStream();
...
...
...
final int sReadLength = 100;
while(sRs.next())
{
...
Clob sClob = sRs.getClob(1);
long sRemains = sClob.length();
long sOffset = 0;
while(sRemains > 0)
{
String sStr = sClob.getSubString(sOffset, sReadLength);
sRemains -= sStr.length;
sOffset += sStr.length;
...
}
...
}
...
Truncation
while(sRs.next())
{
...
int sLength = ... ;
Clob sClob = sRs.getClob(2);
...
65 Advanced Functions
3.12 LOB
...
...
Further operations cannot be executed on an object if a Blob object is freed with the free method,
since the corresponding Lob Locator is freed from the server.
...
...
As for Blob objects, further operations cannot be executed on an object if a Clob object is freed with
the free method, since the corresponding Lob Locator is freed from the server.
The following is a code example which frees the BlobInputStream and BlobOutputStream objects.
Further operations cannot be executed on an object if the BlobInputStream object or the BlobOut-
putStream object is freed with the freeLocator method, since the corresponding Lob Locator is freed
from the server.
The following is a code example which frees the ClobReader and ClobWriter objects.
sCallStmt.execute();
Further operations cannot be executed on an object if the ClobReader object or the ClobWriter
object is freed with the freeLocator method, since the corresponding Lob Locator is freed from the
server.
3.12.5 Restrictions
Even if clientside_auto_commit is set to on to enable the JDBC driver to control the auto-
commit operations of transactions the following restrictions still apply to the manipulation of LOB
data.
If LOB data retrieved from the ResultSet object(cursor) is used with the executeUpdate()
method of another Statement before the cursor is closed, no more fetch operations are possible
from the cursor since the Lob locator is freed. The following is a code example which raises such an
error.
PreparedStatement sPreStmt =
sCon.prepareStatement( "INSERT INTO TEST_TEXT " +
"VALUES ( ?, ?, ?, ? )" );
Statement sStmt = sCon.createStatement();
ResultSet sRS = sStmt.executeQuery( "SELECT ID, TEXT " +
" FROM TEST_SAMPLE_TEXT " );
while ( sRS.next() ) -> 2
{
sID = sRS.getInt( 1 );
sClob = sRS.getClob( 2 );
switch ( sID )
{
case 1 :
sPreStmt.setInt( 1, 1 );
sPreStmt.setString( 2, "Altibase Greetings" );
sPreStmt.setClob( 3, sClob );
sPreStmt.setInt( 4, (int)sClob.length() );
break;
case 2 :
sPreStmt.setInt( 1, 2 );
sPreStmt.setString( 2, "Main Memory DBMS" );
sPreStmt.setClob( 3, sClob );
sPreStmt.setInt( 4, (int)sClob.length() );
break;
default :
break;
}
sPreStmt.executeUpdate(); -> 1
}
1: If sPreStmt.executeUpdate() is called while ResultSet sRS is open, the JDBC driver auto-
matically commits transactions and by doing so, the Lob locator of sClob is freed.
Thus, when manipulating LOB data in such a logic as above, the autocommit mode of a session must
first be disabled by calling setAutocommit(false).
67 Advanced Functions
3.13 Controlling Autocommit
When autocommit is disabled, the user must manually commit or rollback with the commit() or
rollback() method.
• It is recommended to use the Stream or Writer object when using LOB data in JDBC applica-
tions. If the size of the LOB data to be used is equal to or smaller than 8192 bytes, the
Lob_Cache_Threshhold connection attribute must be set to an appropriate value.
• It is recommended to execute one operation on one Connection object. For example, if a mul-
tiple number of Statement objects are created in one Connection object and their operations
are executed, this can induce performance loss.
• It is recommended to use the Connection Pool provided by Middleware (WAS) when the Con-
nection object is frequently created and deleted. This is because the cost of connecting and
terminating a Connection is relatively higher than other operations.
71 Error Messages
5.1 SQL States
The following table lists the types of SQLSTATE which can occur in the ALTIBASE HDB JDBC driver
and its meanings briefly.
connection exception 08
no data 02
warning 01
73 Error Messages
5.1 SQL States
cardinality violation 21 Insert value list does not match column list S01
invalid transaction 25
state
savepoint exception 3B
General Error HY
XA error XA
75 Error Messages
5.1 SQL States
BOOLEAN - -
ARRAY - -
DISTINCT - -
STRUCT - -
REF - -
DATALINK - -
JAVA_OBJECT - -
NULL - null
- GEOMETRY byte[]
TIMESTAMP
SMALLINT
INTEGER
DOUBLE
BINARY
BIGINT
FLOAT
CHAR
CLOB
BLOB
DATE
REAL
TIME
BIT
Array
Blob o
Boolean o o o o o o o o o o
byte[] o o o o o
char[] o o o o o o o o o o o o o o o o
Clob o
Double o o o o o o o o o o
VARBINARY/LONGVARBINARY
VARCHAR/LONGVARCHAR
DECIMAL/NUMERIC
TIMESTAMP
SMALLINT
INTEGER
DOUBLE
BINARY
BIGINT
FLOAT
CHAR
CLOB
BLOB
DATE
REAL
TIME
BIT
Float o o o o o o o o o o
Integer o o o o o o o o o o
Java class
BigDecimal o o o o o o o o o o
java.net.URL
java.sql.Date o o o o o
java.sql.Time o o o o o
java.sql.Timestamp o o o o o
java.util.BitSet o
Long o o o o o o o o o o
Ref
Short o o o o o o o o o
String o o o o o o o o o o o o o o o
Struct
InputStream o
Reader o
VARBINARY/LONGVARBINARY
DECIMAL/NUMERIC
CHAR/VARCHAR
LONGVARCHAR
TIMESTAMP
SMALLINT
INTEGER
DOUBLE
BINARY
BIGINT
FLOAT
CLOB
BLOB
DATE
REAL
TIME
BIT
getArray
getAsciiStream o o o o o o o o o o o o o o
getBigDecimal o o o o o o o o o
getBinaryStream o o o o o o o o o o o
getBlob o o
getBoolean o o o o o o o o o o o
getByte o o o o o o o o o o o
getBytes o o o o o o o o o o o o
getCharacterStream o o o o o o o o o o o o o o
getClob o
getDate o o o o o
getDouble o o o o o o o o o o o
getFloat o o o o o o o o o o o
getInt o o o o o o o o o o o
getLong o o o o o o o o o o o
getObject o o o o o o o o o o o o o o o o o
getRef
getShort o o o o o o o o o o o
getString o o o o o o o o o o o o o o
getTime o o o o o
getTimestamp o o o o o
81 Index
J
java.net.preferIPv4Stack 24
java.net.preferIPv6Addresses 24
Jeus 3.x 36
L
LOB 55
Lob_Cache_Threshhold 70
Login Timeout 32
M
Multiple ResultSet 38
N
National Character Set 28
P
PREFER_IPV6 24
PreparedStatement 26
Properties Object 5
R
Refreshing Rows 49
Response Timeout 32
ResultSet 19, 45
S
SQL States 72
SQLSTATE 72
Statement 19, 26
T
Time 52
Timeout 32
Timestamp 52
Tomcat 8.x 35
TYPE_FORWARD_ONLY 45, 50
TYPE_SCROLL_INSENSITIVE 45, 50
TYPE_SCROLL_SENSITIVE 45, 50
V
Version 2
W
WAS 35
Web Application Server 35
WebLogic 6.x 36
Index 82