18
1 Embedded postgreSQL postgreSQL in JAVA using JDBC @TCU

jdb@tcu

  • Upload
    tess98

  • View
    322

  • Download
    0

Embed Size (px)

Citation preview

Page 1: jdb@tcu

1

Embedded postgreSQLpostgreSQLin JAVA

using JDBC @TCU

Page 2: jdb@tcu

2

Entities

1. Reviews:   (Movie, Role, Critic, Review)

2. Movies: ( Movie, OverallReview, Budget)

3. Players:   (Movie, Role, Category, Actor, Award)

4. Profits: (Movie, Week, Income)

Profits:Movie Week Income

Reviews:Movie Role Critic Review

Movies: Movie OverallReview Budget

Players: Movie Role Category Actor Award

Page 3: jdb@tcu

3

JDBC Overall Architecture

Your program

Main or applet

PostgreSQL

database

riogrande.cs.tcu.eduriogrande.cs.tcu.edu

your machineyour machine

Page 4: jdb@tcu

4

What is JDBC

• It’s a javasoft specification that defines:

• An api that drivers have to complie to– Includes X/Open types– The DBMS must support SQL92

• A uniform api that programmer can use– Write one code, access many DB– Oracle, postgreSQL, DB2, mySQL

Page 5: jdb@tcu

5

Motivations

• Need for accessing DB from Java application, applet, servlet, etc.

• Complete java cross-platform with a cross platform DB access

• DB are core system of many client/server systems

• Internet openess implies many different sources (Sybase, DB2, Oracle, etc.)

• Benefits of ODBC and Java technologies

Page 6: jdb@tcu

6

Details

• Drivers do not need to be pre-installed• Some facilities for applets (special security

are in the sandbox)• URL syntax for locating a resource• Driver Manager instantiate appropriate driver• SQL types are mapped into java types

Page 7: jdb@tcu

7

How-to start

• Register and instantiate the driver– jdbc.driver property (from command line)

• Java –Djdbc.drivers=org.postgresql.Driver example

– Hardcoded• Class.forName(« org.postgresql.Driver »);

– This allow runtime loading of a specific driver !

Page 8: jdb@tcu

8

Connecting

• Connecting to a DB– Db=DriverManager.getConnection(url, usr, pwd);

• No DB specification (the driver has been loaded and will be selected by the DriverManager regarding url).

• URL syntax:– Jdbc:postgresql:database

– Jdbc:postgresql://host/dabase

– Jdbc:postgresql://host:port/dabase

– Jdbc:postgresql:database?user=me

– Jdbc:postgresql:database?user=me&password=mypass

Page 9: jdb@tcu

9

Connection methods

• setAutoCommit();– Transaction mode– Rollback();– Commit();

• PreparedStatement

• CreateStatement

Page 10: jdb@tcu

10

Doing a statement

• St = db.createStatement();– St.executeUpdate(« sql code »);– St.executeQuery(« sql code »);– St.execute(« sql code »);– St.addBatch(« sql statement »);– St.executeBatch();

Page 11: jdb@tcu

11

JAVA with JDBC/postgreSQL @TCUInterfaces, classes and exceptions of the

java.sql packagejava.sql package

Page 12: jdb@tcu

12

JAVA with JDBC/postgreSQL @TCU

Methods for the StatementStatement class/interface

Page 13: jdb@tcu

13

JAVA with JDBC/postgreSQL @TCU

Methods for the ConnectionConnection class/interface

Page 14: jdb@tcu

14

Resultset

• Result=executeUpdate(« select * from toto»);– Result.getMetaData();

Retrieves the number, types and properties of this ResultSet object's columns

– Result.next() • Moves the cursor down one row from its current position

– Result.getObject(i) Gets the value of the designated column in the current row

of this ResultSet object as an Object in the Java programming language

Page 15: jdb@tcu

15

JAVA with JDBC/postgreSQL @TCUthings to remember

Include the jar filepostgresql-8.0-314.jdbc3.jar

into your classpath when you compile your program

Include the the following Import in your program import java.sql.*; // postgreSQL classes

Follow the format of the program provided

Provide the adequate parameters in the connection c = DriverManager.getConnection ("jdbc:postgresql://riogrande.cs.tcu.edu/name", ”name", ”pwd")

Page 16: jdb@tcu

16

JAVA with JDBC/postgreSQL @TCUthings to remember 2

When using read You must turn on the readOnly flag

of the connection using setReadOnly(true);When using rollback/commit

You must turn off the auto commit of the connection using setAutoCommit(false);

Always review the API of the SQL classes

http://java.sun.com/j2se/1.3/docs/api/java/sql/Connection.htmlhttp://java.sun.com/j2se/1.3/docs/api/java/sql/Statement.html

Page 17: jdb@tcu

17

Using large object

• create table images (imgname name,imgoid oid);

• To insert an image, you would use: File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); PreparedStatement ps = conn.prepareStatement("insert

into images values (?,?)"); ps.setString(1,file.getName());

ps.setBinaryStream(2,fis,file.length()); ps.executeUpdate(); ps.close(); fis.close();

Page 18: jdb@tcu

18

Retrieving a large object

PreparedStatement ps = con.prepareStatement("select oid from images where name=?");

ps.setString(1,"myimage.gif"); ResultSet rs = ps.executeQuery(); if(rs!=null) {

while(rs.next()) { InputStream is = rs.getBinaryInputStream(1); // use the stream in some way here is.close();

} rs.close();

} ps.close();