Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Postgres For Java Programmers

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Postgres For Java Programmers

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Postgres for Java Programmers!

Postgres for Java Developer and


Architects

EnterpriseDB, Postgres Plus and Dynatune are trademarks of


EnterpriseDB Corporation. Other names may be trademarks of their 1
respective owners. © 2010. All rights reserved.
Course Objectives!

In this course we will teach Java Developers and Architects:

-The Postgres Architecture

-How to design and develop applications with Postgres and Java

-Intricacies of JDBC and ORM’s with Postgres

-How trouble shoot and performance tune Postgres Queries

-How to write backend server code in Java and PL/pgSQL

© 2011 EnterpriseDB. All rights reserved. 2


Course Outline!
1. Why developed this course
2. Postgres Architecture
3. Create and Manage Databases in Postgres
4. How to install Postgres and Setup a Postgres Development
environment
5. Survey of Database Development tools for Postgres
6. How to use PSQL and PG-Admin for basic database tasks
7. Postgres Security
8. SQL Primer
9. Understanding the Postgres Query Planner
10. JDBC together with Postgres
11. Hibernante and Postgres
12. PL/ PGSQL
13. PL/Java
14. Connection pooling and architecture considerations

© 2011 EnterpriseDB. All rights reserved. 3


The Case for Postgres and Java!

© 2011 EnterpriseDB. All rights reserved. 4


Module Objectives!
 Go over some industry trends around Postgres and Java
 Cover some common misconceptions
 The case for Java and Postgres

© 2011 EnterpriseDB. All rights reserved. 5


The Open Source Web Application!
 The Enterprise Web app. Follows a very basic architecture

Web Client Application Server RDBMS

DB2

© 2011 EnterpriseDB. All rights reserved. 6


Development Configuration Options!
IDE Choices App. Server Databases
Choices
Why we developed this course!
 Very common platform for our customers
• Linux + Java + Postgres !
 It is the most common platform for our largest customers
 Many Java applications migrating to Postgres
 Many Java Programmers know very little about Postgres
 Many Postgres DBAs know very little about Java,
• Especially Java ORMʼs!
 Architechts need to understand the Postgres eco-system
 Seeing many migrations of Java applications to Postgres
 Natural tension exists between the two technologies
 Need info. on the growth of Postgres
 Need info. On the growth of Java

© 2011 EnterpriseDB. All rights reserved. 8


Agenda!
 Day 1
• Course introduction (tom)!
• EnterpriseDB (bruce)!
• Postgres Overview History and Features (bruce)!
• Postgres Architechture (bruce)!
• Installation (bruce)!
• Setting up a Java Development Envrionment with Postgres (tom)!
 Day 2
• Configuration and Performance Parameters of Postgres (bruce)!
• Creating and managing Postgres Databases (tom)!
• PSQL (bruce)!
• PG Admin (bruce)!
• Security (bruce)!
• SQL Primer (bruce)!
• PL/pgsQL (bruce)!

© 2011 EnterpriseDB. All rights reserved. 9


Agenda Continued!
 Day 3
• PL / Java (tom)!
• Query Planner (tom / robert)!
• Trouble shooting Slow Queries (tom / robert)!
• Hibernate and Postgres (tom)!
• Architechtural decisions Postgres and Java Application Servers!

© 2011 EnterpriseDB. All rights reserved. 10


Setting up a Java Development
Environment!

EnterpriseDB, Postgres Plus and Dynatune are trademarks of


EnterpriseDB Corporation. Other names may be trademarks of their 11
respective owners. © 2010. All rights reserved.
Module Objectives!
 Create a new user and database for Java Development
 Establish a connection from a Java program to new database
 Understand basic usage from common Java IDEs
• Eclipse!
• NetBeans!

© 2012 EnterpriseDB. All rights reserved. 12


Creating a database and user!
 Try avoid doing development with the super user
 Best practice for Web Apps. is to have a single user for normal
access.
 Have second user for administrative access
 If developing on a local machine
• Use same database name to used in production!
• Use same user name to be used in production!
 If new to Postgres use PG Admin to setup environment

© 2012 EnterpriseDB. All rights reserved. 13


Connect to the server!

Connect
to newly
installed
server

© 2012 EnterpriseDB. All rights reserved. 14


Establishing a PG Admin Connection!

© 2012 EnterpriseDB. All rights reserved. 15


Create a new login role!

© 2012 EnterpriseDB. All rights reserved. 16


About the new role!
 Role should not be a super user
 Should not be able to create databases
 Should be able to create database objects
• Triggers, Tables etc.!
 Should not be able to create streaming replication backups
 Specify a connect limit that makes sense

© 2012 EnterpriseDB. All rights reserved. 17


Create a new database!

© 2012 EnterpriseDB. All rights reserved. 18


About the new database!
 Should be owned by the role you created
 Choose propper encoding for your application
 Consider using previous databases as templates

© 2012 EnterpriseDB. All rights reserved. 19


Establish a Java Connection!
 Java program access Postgres (and any RDBMS) through
JDBC
 JDBC Connections are accomplished via a connection URL
• [protocol][database type]://[host]:[port]/[database name]!
 A Postgres JDBC URL connection string
• jdbc:postgres://localhost:5432/tom!
• Where database cluster is running on the local host on port 5432!
• Where database name is tom!

© 2012 EnterpriseDB. All rights reserved. 20


Getting the Postgres JDBC Driver!
 Easiest way to get it is via Stackbuilder
 Stackbuilder is a free tool from EnterpriseDB
 Includes many commonly used Postgres components
 Componentes have been integrated and tested together

© 2012 EnterpriseDB. All rights reserved. 21


Getting the JDBC Driver installed!
 Download and install via stack builder

© 2012 EnterpriseDB. All rights reserved. 22


Establishing a connection via Java!

private static void helloPostgres(String database, String username, !


String password, String host, String port)!
throws ClassNotFoundException, SQLException {!

Connection dbConnect;!
Statement sql; // Our statement to run queries with!
DatabaseMetaData dbmd; // This is basically info the driver delivers!
// about the DB it just connected to. I use!
// it to get the DB version to confirm the!
// connection in this example.!

Class.forName("org.postgresql.Driver"); //load the driver!


dbConnect = DriverManager.getConnection("jdbc:postgresql://"+ host + ":" + port !
! +"/" + database, username, password); d!
dbmd = dbConnect.getMetaData(); //get MetaData to confirm connection!
System.out.println("Connection to " + dbmd.getDatabaseProductName() + " " +
!dbmd.getDatabaseProductVersion() + " successful.\n");!

}!

© 2012 EnterpriseDB. All rights reserved. 23


PL / pgSQL!

© 2011 EnterpriseDB. All rights reserved.


Objectives!
 In this module you will learn:!
• Procedural Languages Overview!
• PL/pgSQL Overview!
• PL/pgSQL Structure!
• PL/pgSQL Declarations!
• PL/pgSQL Basic Statements!
• PL/pgSQL Control and Loop Structures !
• Checking For Errors!
• PL/pgSQL Cursors!
• Triggers!

© 2011 EnterpriseDB. All rights reserved. 25


Proceedure Language Overview!
 PostgreSQL allows user-defined functions to be written in a
variety of procedural languages. !
 PostgreSQL currently supports several standard procedural
languages : !
• PL/pgSQL !
• PL/Tcl !
• PL/Perl!
• PL/Python !
• PL/Java!
• PL/Ruby!
• Other languages can be defined by users!

© 2011 EnterpriseDB. All rights reserved. 26


PL/pgSQL Overview!
 “Native” Programming Language !
 Advantages of using PL/pgSQL!
• Its Portable and Easy to Learn!
• Removes Extra Round Trips Between Client and Server!
• Intermediate results that the client does not need do not have to be
marshaled or transferred between server and client!
• Multiple rounds of query parsing can be avoided!
• The most expressive Postgres Programming Language!

© 2011 EnterpriseDB. All rights reserved. 27


PL/pgSQL Structre!
 PL/pgSQL is a block-structured language. The complete text of
a function definition must be a block. A block is defined as:!

[ <<label>> ]
[ DECLARE
declarations ]
BEGIN
statements
END [ label ];

 Each declaration and each statement within a block is


terminated by a semicolon !
 A block that appears within another block must have a
semicolon after END!
 The final END that concludes a function body does not require a
semicolon.!

© 2011 EnterpriseDB. All rights reserved. 28


PL/pgSQL Structure (Contd)!
 All key words and identifiers can be written in mixed upper and
lower case. Identifiers are implicitly converted to lowercase
unless double-quoted.!
 There are two types of comments in PL/pgSQL. !
• -- starts a comment that extends to the end of
the line. !
• /* multi-line comments */!
 Variables declared in the declarations section preceding a block
are initialized to their default values every time the block is
entered, not only once per function call.!

© 2011 EnterpriseDB. All rights reserved. 29


PL/pgSQL Structure - Example!

CREATE FUNCTION samplefunc() RETURNS integer AS $$


DECLARE
quantity integer := 30;
BEGIN
RAISE NOTICE 'Quantity here is %', quantity; -- Quantity here is
30
quantity := 50;
-- Create a subblock
DECLARE
quantity integer := 80;
BEGIN
RAISE NOTICE 'Quantity here is %', quantity; -- Quantity
here is 80
END;
RAISE NOTICE 'Quantity here is %', quantity; -- Quantity here is
50
RETURN quantity;
END;
$$ LANGUAGE plpgsql;

© 2011 EnterpriseDB. All rights reserved. 30


PL/pgSQL Declarations!
 All variables used in a block must be declared in the
declarations section of the block. !
 PL/pgSQL variables can be any valid SQL data type and can
contain a DEFAULT or CONSTANT clause!
 Examples!
• user_id integer;!
• quantity numeric(5);!
• url varchar;!
• myrow tablename%ROWTYPE;!
• myfield tablename.columnname%TYPE;!
• arow RECORD;!
• quantity integer DEFAULT 32;!
 The general syntax of a variable declaration is:!
• name [ CONSTANT ] type [ NOT NULL ] [ { DEFAULT
| := } expression ];!

© 2011 EnterpriseDB. All rights reserved. 31


PL/pgSQL Basic Statements!
 Assignment!
• identifier := expression;!
• user_id := 20;!
• tax := subtotal * 0.06;!

 SELECT INTO!
• SELECT INTO target select_expressions FROM ...;!
• SELECT INTO myrec * FROM emp WHERE empname = myname;!

© 2011 EnterpriseDB. All rights reserved. 32


Execute!
 Executing Dynamic Commands!
• EXECUTE command-string [ INTO target ];!
• SELECT INTO is not currently supported within EXECUTE!

EXECUTE 'UPDATE tbl SET '!


|| quote_ident(colname)!
|| ' = '!
|| quote_literal(newvalue)!
|| ' WHERE key = '!
|| quote_literal(keyvalue);!

© 2011 EnterpriseDB. All rights reserved. 33


FOUND!
 FOUND, which is of type boolean. Starts out false within each
PL/pgSQL function call. !
• It is set by each of the following types of statements:!
– A SELECT INTO statement sets FOUND true if it returns a row, false if no row is
returned.!
– A PERFORM statement sets FOUND true if it produces (and discards) a row, false
if no row is produced.!
– UPDATE, INSERT, and DELETE statements set FOUND true if at least one row is
affected, false if no row is affected.!
– A FETCH statement sets FOUND true if it returns a row, false if no row is returned.!
– A FOR statement sets FOUND true if it iterates one or more times, else false. !
• FOUND is a local variable within each PL/pgSQL function; any changes to it
affect only the current function. !

© 2011 EnterpriseDB. All rights reserved. 34


NEED A FOUND Example!
 Found Example

35
PL/pgSQL Control Structures !
 RETURN!
• RETURN expression;!
• This form is to be used for PL/pgSQL functions that do not return a set.!

© 2012 EnterpriseDB. All rights reserved. 36


IF-THEN-ESLE!
 IF-THEN
IF boolean-expression THEN
statements
END IF;

 IF-THEN-ELSE
IF boolean-expression THEN
statements
ELSE
statements
END IF;

© 2012 EnterpriseDB. All rights reserved. 37


LOOP and EXIT!
LOOP
-- some computations
IF count > 0 THEN
EXIT; -- exit loop
END IF;
END LOOP;

LOOP
-- some computations
EXIT WHEN count > 0; -- same result as
previous example
END LOOP;

BEGIN
-- some computations
IF stocks > 100000 THEN
EXIT; -- causes exit from the BEGIN
block
END IF;
END;

© 2012 EnterpriseDB. All rights reserved. 38


Continue!
 CONTINUE!
• CONTINUE [ label ] [ WHEN expression ];
 If no label is given, the next iteration of the innermost loop is
begun. !
 If WHEN is specified, the next iteration of the loop is begun only
if expression is true. Otherwise, control passes to the statement
after CONTINUE.!
 CONTINUE can be used with all types of loops; it is not limited
to use with unconditional loops.!
LOOP
-- some computations
EXIT WHEN count > 100;
CONTINUE WHEN count < 50;
-- some computations for count IN [50 .. 100]
END LOOP;

© 2012 EnterpriseDB. All rights reserved. 39


WHILE and FOR Loops

 WHILE
WHILE amount_owed > 0 AND gift_certificate_balance >
0 LOOP
-- some computations here
END LOOP;
 FOR
FOR i IN 1..10 LOOP
-- some computations here
RAISE NOTICE 'i is %', i;
END LOOP;

FOR i IN REVERSE 10..1 LOOP


-- some computations here
END LOOP;

40
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 40
Loop through Query Results

[ <<label>> ]
FOR record_or_row IN query LOOP
statements
END LOOP [ label ];

FOR mviews IN SELECT * FROM cs_materialized_views ORDER


BY sort_key LOOP
-- Now "mviews" has one record from
cs_materialized_views
PERFORM cs_log('Refreshing materialized view ' ||
quote_ident(mviews.mv_name) || ' ...');
EXECUTE 'TRUNCATE TABLE ' || quote_ident
(mviews.mv_name);
EXECUTE 'INSERT INTO ' || quote_ident
(mviews.mv_name) || ' ' || mviews.mv_query;
END LOOP;

41
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 41
FOR-IN-EXECUTE

[ <<label>> ]
FOR record_or_row IN EXECUTE text_expression LOOP
statements
END LOOP [ label ];

• This is like the previous form, except that the source SELECT statement is specified
as a string expression, which is evaluated and replanned on each entry to the FOR
loop.

• This allows the programmer to choose the speed of a preplanned query or the
flexibility of a dynamic query, just as with a plain EXECUTE statement

42
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 42
Checking For Errors

 By default, any error occurring in a PL/pgSQL function aborts execution
of the function

 You can trap errors and recover from them by using a BEGIN block
with an EXCEPTION clause

INSERT INTO mytab(firstname, lastname) VALUES('Tom',


'Jones');
BEGIN
UPDATE mytab SET firstname = 'Joe' WHERE
lastname = 'Jones';
x := x + 1;
y := x / 0;
EXCEPTION
WHEN division_by_zero THEN
RAISE NOTICE 'caught division_by_zero';
RETURN x;
END;

43
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 43
Checking For Errors (cont)

CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
LOOP
UPDATE db SET b = data WHERE a = key;
IF found THEN
RETURN;
END IF;

BEGIN
INSERT INTO db(a,b) VALUES (key, data);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- do nothing
END;
END LOOP;
END;
$$ LANGUAGE plpgsql;

44
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 44
PL/pgSQL Cursors

 Declaring Cursors

• DECLARE curs1 refcursor;!
• DECLARE curs2 CURSOR FOR SELECT * FROM tenk1;!
• DECLARE curs3 CURSOR (key integer) IS SELECT * FROM
tenk1 WHERE unique1 = key;!

 Opening Cursors

• OPEN FOR query!
• OPEN unbound_cursor FOR query;!
– OPEN curs1 FOR SELECT * FROM foo WHERE key = mykey;!
• OPEN FOR EXECUTE!
• OPEN unbound_cursor FOR EXECUTE query_string;!
– OPEN curs1 FOR EXECUTE 'SELECT * FROM ' || quote_ident
($1);!

45
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 45
PL/pgSQL Cursors (cont)

 Opening a Bound Cursor

• OPEN bound_cursor [ ( argument_values ) ];

• Opening CursorsOPEN FOR query


DECLARE
curs2 CURSOR FOR SELECT * FROM tenk1;
curs3 CURSOR (key integer) IS SELECT * FROM tenk1 WHERE
unique1 = key;
OPEN curs2;
OPEN curs3(42);

46
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 46
PL/pgSQL Cursors (cont)

 FETCH

• FETCH cursor INTO target;

• FETCH retrieves the next row from the cursor into a target, which may
be a row variable, a record variable, or a comma-separated list of
simple variables, just like SELECT INTO.

• As with SELECT INTO, the special variable FOUND may be checked
to see whether a row was obtained or not.

– FETCH curs1 INTO rowvar;!
– FETCH curs2 INTO foo, bar, baz;!
 CLOSE

• CLOSE closes the portal underlying an open cursor.

• This can be used to release resources earlier than end of transaction, or
to free up the cursor variable to be opened again.

– CLOSE curs1;!

47
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 47
Triggers

 Created with the CREATE FUNCTION command

 Several special variables are created automatically in the top-level
block.

• NEW

– Data type RECORD; variable holding the new database row
for INSERT/UPDATE operations in row-level triggers. This
variable is NULL in statement-level triggers.

• OLD

– Data type RECORD; variable holding the old database row
for UPDATE/DELETE operations in row-level triggers. This
variable is NULL in statement-level triggers.

• TG_NAME

– Data type name; variable that contains the name of the trigger
actually fired.

48
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 48
Triggers (Contd.)

• TG_WHEN

– Data type text; a string of either BEFORE or AFTER
depending on the trigger's definition.

• TG_LEVEL

– Data type text; a string of either ROW or STATEMENT
depending on the trigger's definition.

• TG_OP

– Data type text; a string of INSERT, UPDATE, or DELETE
telling for which operation the trigger was fired.

• TG_RELNAME

– Data type name; the name of the table that caused the trigger
invocation.

• TG_NARGS

– Data type integer; the number of arguments given to the
trigger procedure in the CREATE TRIGGER statement.

49
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 49
Triggers (cont)

• Note: A trigger function must return either NULL or a record/row value having
exactly the structure of the table the trigger was fired for.

• The return value of a BEFORE or AFTER statement-level trigger or an AFTER
row-level trigger is always ignored; it may as well be null. However, any of these
types of triggers can still abort the entire operation by raising an error.

50
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 50
Summary

 In this module you learned:



• Procedural Languages Overview

• PL/pgSQL Overview

• PL/pgSQL Structure

• PL/pgSQL Declarations

• PL/pgSQL Basic Statements

• PL/pgSQL Control and Loop Structures

• Checking For Errors

• PL/pgSQL Cursors

• Triggers

51
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 51
Lab Exercises 1

• Evaluate each of the following declarations. Determine which of them
are not legal and explain why.
– DECLARE v_name,v_dept VARCHAR(14);
– DECLARE v_test NUMERIC(5);
– DECLARE V_MAXSALARY NUMERIC(7,2) = 5000;
– DECLARE V_JOINDATE BOOLEAN := NOW();
• In each of the following assignments, determine the data type of the
resulting expression.
– v_email := v_firstname || to_char(v_empno);
– v_confirm := to_date('20-JAN-1999', 'DD-MON-YYYY');
– v_sal := (1000*12) + 500
– v_test := FALSE;
– v_temp := v_temp1 < (v_temp2/ 3);
– v_var := now();

52
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 52
Lab Exercise 2

DECLARE
v_custid numeric(4) := 1600;
v_custname VARCHAR(300) := 'Women Sports Club';
v_new_custid numeric(3) := 500;
BEGIN
DECLARE
v_custid numeric(4) := 0;
v_custname VARCHAR(300) := 'Shape up Sports Club';
v_new_custid numeric(3) := 300;
v_new_custname VARCHAR(300) := 'Jansports Club';
BEGIN
v_custid := v_new_custid;
v_custname := v_custname || ' ' || v_new_custname;

END;
v_custid := (v_custid *12) / 10;

END;
Evaluate the PL/SQL block given above and determine the data type and value of each of the following
variables according to the rules of scoping:
a. The value of V_CUSTID at position 1 is:
b. The value of V_CUSTNAME at position 1 is:
c. The value of V_NEW_CUSTID at position 2 is:
d. The value of V_NEW_CUSTNAME at position 1 is:
e. The value of V_CUSTID at position 2 is:
f. The value of V_CUSTNAME at position 2 is:
g. v_custid at position 3 and v_custname at position 3.

53
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 53
Lab Exercise 3

 Create a PL/pgSQL function which can take integer and returns
integer.

 Create a PL/pgSQL block to retrieve the name and department ID of
each employee from the EMP table for those employees whose
EMPNO is less than 7898.

 From the values retrieved from the emp table, populate two tables
using PL/pgSQL, one to store the records of the employee names and
the other to store the records of their department IDs.

 Using a loop, retrieve the employee name information and the salary
information from the PL/pgSQL and display it in the window. Display
these details for the first 15 employees in the PL/SQL tables.

 Eg.

Employee Name: Steve Department_id: 90

Employee Name: Mac Department_id: 80

54
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 54
Lab Exercise 4

 Create a Table DeptEmpCount:



• Department_id numeric

• Totalemp numeric

 DeptEmpCount table stores total number of employees in each
department. Create necessary triggers on emp table so that when a new
employee joins a department respected deptempcount table row should
be incremented and when a new employee leaves the department or
leave the organization the respected row in deptempcount table should
be decremented.

55
Copyright 2010 EnterpriseDB Corporation. All rights Reserved 55

You might also like