Your SlideShare is downloading. ×
jdb@tcu
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

jdb@tcu

303
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
303
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Embedded postgreSQL in JAVA using JDBC @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)
  • 3. JDBC Overall Architecture Your program Main or applet PostgreSQL database riogrande.cs.tcu.edu your machine
  • 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
  • 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
  • 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
  • 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 !
  • 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
  • 9. Connection methods
    • setAutoCommit();
      • Transaction mode
      • Rollback();
      • Commit();
    • PreparedStatement
    • CreateStatement
  • 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();
  • 11. JAVA with JDBC/postgreSQL @TCU
    • Interfaces, classes and exceptions of the
    • java.sql package
  • 12. JAVA with JDBC/postgreSQL @TCU
    • Methods for the Statement class/interface
  • 13. JAVA with JDBC/postgreSQL @TCU
    • Methods for the Connection class/interface
  • 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
  • 15. JAVA with JDBC/postgreSQL @TCU things to remember Include the jar file  postgresql-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")
  • 16. JAVA with JDBC/postgreSQL @TCU things 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.html http://java.sun.com/j2se/1.3/docs/api/java/sql/Statement.html
  • 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();
  • 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();

×