|
This section highlights important concepts for JDBC programming.
The JDBC driver requires a URL to connect to a database. The syntax for URL for the Pervasive JDBC driver is :
jdbc:pervasive://<machinename>:<port number>/Datasource<machinename> is the host name or ip address of the machine that runs the Pervasive PSQL server
<portnumber> is the port on which the Pervasive PSQL server is listening - by default it is 1583
<datasource> is the name of the ODBC engine data source on the Pervasive PSQL server that the application intends to use.
Note
A Pervasive PSQL v9 Service Pack 2 engine needs to be running on the specified host in order to run JDBC applications.
The Pervasive JDBC driver now supports character encoding, which allows you to filter data you read through a specified code page so that it is formatted and sorted correctly.
The following shows how to connect to a Pervasive PSQL database using JDBC:
jdbc:pervasive://server:port/DSN;encoding
The following code shows how to connect to a Pervasive database using the JDBC driver:
// Load the Pervasive PSQL JDBC driver Class.forName("com.pervasive.jdbc.v2.Driver") // Pervasive 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 }
Character encoding is specified using a parameter in the connection string passed to the driver manager.
Encoding parameter allows user to specify the code page to be used for translating the string data stored in the database. The default system encoding is used if no encoding is explicitly specified.
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(); } }
|
Chapter contents
Prev topic: Programming with the Pervasive JDBC 2 Driver
|