JDBC Programming Tasks
This section highlights important concepts for JDBC programming.
Connection String Overview
The JDBC driver requires a URL to connect to a database. The URL syntax for the JDBC driver is:
jdbc:pervasive://machinename:port number/datasource[;encoding=;encrypt=;encryption=]
machinename is the host name or ip address of the machine that runs the PSQL server.
port number is the port on which the PSQL server is listening. By default it is 1583.
datasource is the name of the ODBC engine data source on the PSQL server that the application intends to use.
encoding= is the character encoding, which allows you to filter data you read through a specified code page so that it is formatted and sorted correctly. The value “auto” will determine the database code page at connection time and then set the encoding to that character encoding. The value “auto” will also preserve NCHAR literals in SQL queries. If not “auto”, SQL queries are converted to the database code page.
encrypt= specifies whether the JDBC driver should use encrypted network communications, also known as wire encryption.
encryption= specifies the minimum level of encryption allowed by the JDBC driver.
*Note: A PSQL v12 engine needs to be running on the specified host to run JDBC applications.
Connection String Elements
The following shows how to connect to a PSQL database using JDBC:
Driver Classpath
com.pervasive.jdbc.v2
Statement to Load Driver
Class.forName(“com.pervasive.jdbc.v2.Driver”);
URL
jdbc:pervasive://server:port/DSN[;encoding=;encrypt=;encryption=]
or
jdbc:pervasive://server:port/DSN[?pvtranslate=&encrypt=&encryption=]
 
 
Values: always, never
If the value always is specified, the JDBC driver uses encryption or else return an error if wire encryption is not allowed by the server. If the value never is specified, the JDBC driver does not use encryption and returns an error if wire encryption is required by the server.
Values: low, medium, high
Default: medium
JDBC Connection String Example
The following code shows how to connect to a PSQL database using the JDBC driver:
// Load the PSQL JDBC driver
Class.forName("com.pervasive.jdbc.v2.Driver")
 
// PSQL JDBC URL Syntax:
// jdbc:pervasive://<hostname or ip address > :
// <port num (1583 by default)>/<odbc engine DSN>
 
String myURL = "jdbc:pervasive://127.0.0.1:1583/demodata";
try
{
 
// m_Connection = DriverManager.getConnection(myURL,username, password);
}
catch(SQLException e)
{
e.printStackTrace();
 
// other exception handling
 
}
Using Character Encoding
Java uses wide characters for strings. If the encoding in the database is not also wide character (e.g., UCS-2), the driver has to know the database code page in order to correctly exchange character data with the database engine. The database character data encoding is specified using the “encoding” attribute in the connection string passed to the driver manager.
Encoding Attribute
The encoding attribute specifies a particular code page to use for translating character data. This can be automated by setting the encoding attribute to “auto”. This directs the driver to automatically use the code page used in the database. You can also specify a specific code page. If the encoding attribute is absent, the default operating system code page for the client machine is used. The assumption is that the client and server use the same operating system encoding.
Setting the encoding attribute to “auto” also results in SQL query text being sent to the engine using UTF-8 encoding instead of using the database code page encoding. This will preserve NCHAR string literals in query text.
Example of Using Character Encoding
public static void main(String[] args)
{
//specify latin 2 encoding
String url = "jdbc:pervasive://MYSERVR:1583/SWEDISH_DB;encoding=cp850";
try{
Class.forName("com.pervasive.jdbc.v2.Driver");
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from SwedishTable");
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Notes on Character Encoding
The PSQL JDBC driver uses Java native support for code pages. The list of supported code pages can be obtained from the Oracle Corporation website.