Click here to load reader

EDB Postgres Advanced Server JDBC Connector · PDF file 2016-07-14 · EDB Postgres™ Advanced Server JDBC Connector Guide EDB Postgres™ Advanced Server 9.5 formerly Postgres Plus

  • View
    7

  • Download
    0

Embed Size (px)

Text of EDB Postgres Advanced Server JDBC Connector · PDF file 2016-07-14 · EDB...

  • EDB Postgres™ Advanced Server JDBC Connector Guide

    EDB Postgres™ Advanced Server 9.5 formerly Postgres Plus Advanced Server 9.5

    March 7, 2016

  • Copyright © 2008 - 2016 EnterpriseDB Corporation. All rights reserved. 2

    2

    EDB Postgres™ Advanced Server JDBC Connector Guide, Version 9.5

    by EnterpriseDB® Corporation

    Copyright © 2008 - 2016 EnterpriseDB Corporation. All rights reserved.

    EnterpriseDB Corporation, 34 Crosby Drive, Suite 100, Bedford, MA 01730, USA

    T +1 781 357 3390 F +1 978 589 5701 E [email protected] www.enterprisedb.com

  • EDB Postgres Advanced Server JDBC Connector Guide

    Copyright © 2008 - 2016 EnterpriseDB Corporation. All rights reserved. 3

    3

    Table of Contents

    1 Introduction ................................................................................................................. 5

    1.1 Typographical Conventions Used in this Guide ................................................. 6

    2 Advanced Server JDBC Connector Overview ............................................................ 7

    2.1 JDBC Driver Types............................................................................................. 7

    2.2 The JDBC Interface ............................................................................................ 8

    2.3 JDBC Classes and Interfaces .............................................................................. 9

    2.4 The JDBC DriverManager ................................................................................ 10

    3 JDBC Installation ...................................................................................................... 11

    3.1 Configuring the Advanced Server JDBC Connector ........................................ 12

    4 Using the Advanced Server JDBC Connector with Java applications ..................... 13

    4.1 Loading the Advanced Server JDBC Connector .............................................. 14

    4.2 Connecting to the Database .............................................................................. 15

    4.2.1 Additional Connection Properties ................................................................. 15

    4.3 Executing SQL Statements through Statement Objects.................................... 17

    4.4 Retrieving Results from a ResultSet Object ..................................................... 18

    4.5 Freeing Resources ............................................................................................. 19

    4.6 Handling Errors ................................................................................................. 19

    5 Executing SQL Commands with executeUpdate() ................................................... 20

    5.1 Using executeUpdate() to INSERT Data .......................................................... 20

    5.1.1 executeUpdate() Syntax Examples ............................................................... 22

    6 Adding a Graphical Interface to a Java Program ...................................................... 23

    7 Advanced JDBC Connector Functionality ................................................................ 26

    7.1 Reducing Client-side Resource Requirements.................................................. 26

    7.1.1 Modifying the Batch Size of a Statement Object.......................................... 27

    7.2 Using PreparedStatements to Send SQL Commands ....................................... 28

    7.3 Executing Stored Procedures ............................................................................ 30

    7.3.1 Invoking Stored Procedures .......................................................................... 30

    7.3.1.1 Executing a Simple Stored Procedure ................................................... 30

    7.3.1.2 Executing Stored Procedures with IN parameters ................................ 31

    7.3.1.3 Executing Stored Procedures with OUT parameters ............................ 33

    7.3.1.4 Executing Stored Procedures with IN OUT parameters ....................... 34

  • EDB Postgres Advanced Server JDBC Connector Guide

    Copyright © 2008 - 2016 EnterpriseDB Corporation. All rights reserved. 4

    4

    7.4 Using REF CURSORS with Java ..................................................................... 37

    7.4.1 Using a REF CURSOR to retrieve a ResultSet ............................................ 37

    7.5 Using BYTEA Data with Java .......................................................................... 40

    7.5.1 Inserting BYTEA Data into an Advanced Server database .......................... 41

    7.5.2 Retrieving BYTEA Data from an Advanced Server database ...................... 42

    7.6 Using Object Types and Collections with Java ................................................ 44

    7.6.1 Using an Object Type ................................................................................... 44

    7.6.2 Using a Collection......................................................................................... 46

    7.7 Asynchronous Notification Handling with NoticeListener .............................. 49

    8 Reference - JDBC Data Types .................................................................................. 52

  • EDB Postgres Advanced Server JDBC Connector Guide

    Copyright © 2008 - 2016 EnterpriseDB Corporation. All rights reserved. 5

    5

    1 Introduction

    Notice: The names for EDB™’s products have changed. The product formerly referred to

    as Postgres Plus Advanced Server is now referred to as EDB Postgres™ Advanced

    Server. Until a new version of this documentation is published, wherever you see

    Postgres Plus Advanced Server you may substitute it with EDB Postgres Advanced

    Server. Name changes in software and software outputs will be phased in over time.

    The JDBC connector (distributed with the Advanced Server installer) provides

    connectivity between a Java application and an Advanced Server database. This guide

    provides installation instructions, usage instructions, and examples that demonstrate the

    Advanced Server specific functionality of the JDBC Connector.

    Throughout the document, examples demonstrate JDBC connector usage:

     Section 4.3 shows how to query an Advanced Server database.

     Section 5 describes using the executeUpdate() method to execute SQL commands.

     Section 7.3 explains different methods to invoke stored procedures.

     Section 7.4 demonstrates declaring and using a REF CURSOR with Java.

     Section 7.5 demonstrates using BYTEA data with Java.

     Section 7.6 demonstrates using object types and collections with Java.

    The JDBC connector is written in Java and conforms to Sun's JDK architecture, as

    described in section 2.1.

    The JDBC connector is built on and supports all of the functionality of the PostgreSQL

    community driver. For more information about the features and functionality of the

    driver, please refer to the community documentation at:

    https://jdbc.postgresql.org/documentation/head/index.html

    https://jdbc.postgresql.org/documentation/head/index.html

  • EDB Postgres Advanced Server JDBC Connector Guide

    Copyright © 2008 - 2016 EnterpriseDB Corporation. All rights reserved. 6

    6

    1.1 Typographical Conventions Used in this Guide

    Certain typographical conventions are used in this manual to clarify the meaning and

    usage of various commands, statements, programs, examples, etc. This section provides

    a summary of these conventions.

    In the following descriptions a term refers to any word or group of words which may be

    language keywords, user-supplied values, literals, etc. A term’s exact meaning depends

    upon the context in which it is used.

     Italic font introduces a new term, typically, in the sentence that defines it for the first time.

     Fixed-width (mono-spaced) font is used for terms that must be given

    literally such as SQL commands, specific table and column names used in the

    examples, programming language keywords, etc. For example, SELECT * FROM emp;

     Italic fixed-width font is used for terms for which the user must

    substitute values in actual usage. For example, DELETE FROM table_name;

     A vertical pipe | denotes a choice between the terms on either side of the pipe. A vertical pipe is used to separate two or more alternative terms within square

    brackets (optional choices) or braces (one mandatory choice).

     Square brackets [ ] denote that one or none of the enclosed term(s) may be

    substituted. For example, [ a | b ], means choose one of “a” or “b” or neither

    of the two.

     Braces {} denote that exactly one of the enclosed alternatives must be specified.

    For example, { a | b }, means exactly one of “a” or “b” must be specified.

     Ellipses ... denote that the proceeding term may be repeated. For example, [ a |

    b ] ... means that you may have the sequence, “b a a b a”.

  • EDB Postgres Advanced Server JDBC Connector Guide

    Copyright © 2008 - 2016 EnterpriseDB Co