JDBC


CS-328
JDBC

• Java API for accessing RDBMS
• Allows use of SQL for RDBMS
  programming
• Can be used for:
  – embedded SQL
  – e...
SQL Statement Execution

• Four primary classes used to
  – load a driver
     • (java.sql.DriverManager)
  – connect to t...
Example
   try {
     Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
     String url = “jdbc:odbc:msaccessdb”;
     Conne...
Driver Manager Object

• Loads the proper driver into your java
  object
  – multiple drivers may be loaded to allow
    c...
Driver Manager Object
                JAVA Application



                 Driver Manager



        Driver „A‟           ...
Driver Manager Class Methods
•   getConnection(String url)
•   getDriver
•   registerDriver
•   deregisterDriver
•   getDr...
Connection Object

• Establish link between the JAVA
  application and RDBMS
• allows application to select proper driver
...
Connection Class Methods
•   createStatement
•   prepareStatement
•   prepareCall
•   NativeSQL
•   setAutoCommit
•   GetA...
Statement Object

• Wrapper of a string containing a SQL
  statement
• allows JDBC to decompose the SQL into a
  set of st...
Statement Class Methods

• executeQuery
• executeUpdate
  – insert, update, delete
• close
• getResultSet
ResultSet Object

• A container for the rows and columns (a
  table) acquired as a result of presenting a
  statement obje...
ResultSet Class Methods

•   next
•   close
•   wasNull
•   getString
•   getBoolean
•   getByte
•   getShort
ResultSet Class Methods(cont.)

•   getInt
•   getLong
•   getFloat
•   getDouble
•   getNumeric
•   getBytes
•   getDate
ResultSet Class Methods (cont.)

•   getTime
•   getTimeStamp
•   getAsciiStream
•   getUnicodeStream
•   getBinaryStream
...
JDBC Type Mapping
• Char - String          •   Tinyint - byte
• Varchar - String       •   Smallint = short
• Longvarchar ...
JDBC Type Mapping (cont.)
•   Varbinary - byte[ ]
•   Longvarbinary - byte[ ]
•   Date - java.sql.Date
•   Time - java.sql...
Upcoming SlideShare
Loading in...5
×

jdbc2.ppt

1,049

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
1,049
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
72
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

jdbc2.ppt

  1. 1. JDBC CS-328
  2. 2. JDBC • Java API for accessing RDBMS • Allows use of SQL for RDBMS programming • Can be used for: – embedded SQL – execution of stored queries
  3. 3. SQL Statement Execution • Four primary classes used to – load a driver • (java.sql.DriverManager) – connect to the DB • (java.sql.Connection) – create a SQL statement • (java.sql.Statement) – execute a SQL statement • (java.sql.ResultSet)
  4. 4. Example try { Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”); String url = “jdbc:odbc:msaccessdb”; Connection con = DriverManager.getConnection(url,””,””); // create sql statement String qs = “select * from loadtest”; Statement stmt = con.createStatement(); // execute the statement ResultSet rs = stmt.executeQuery(qs); // process the result set boolean more = rs.next; while (more) { System.out.println ............}}
  5. 5. Driver Manager Object • Loads the proper driver into your java object – multiple drivers may be loaded to allow connections to multiple databases • Provides a common interface to RDBMSs for JDBC Drivers
  6. 6. Driver Manager Object JAVA Application Driver Manager Driver „A‟ Driver „B‟ Connection Connection Statement Statement Result Set Result Set
  7. 7. Driver Manager Class Methods • getConnection(String url) • getDriver • registerDriver • deregisterDriver • getDrivers • setLoginTimeout • getLoginTimeout • setLogStream • getLogStream
  8. 8. Connection Object • Establish link between the JAVA application and RDBMS • allows application to select proper driver when it needs to • uses the database URL to make the connection – jdbc:<subprotocol>:<subname) • jdbc:odbc:Mydatabase • jdbc:db2:157.287.43.11/Mydatabse
  9. 9. Connection Class Methods • createStatement • prepareStatement • prepareCall • NativeSQL • setAutoCommit • GetAutoCommit • commit • rollback • close • isclosed
  10. 10. Statement Object • Wrapper of a string containing a SQL statement • allows JDBC to decompose the SQL into a set of steps to present to the database via the driver • the connection object forwards the statement object to the database to obtain a results set object
  11. 11. Statement Class Methods • executeQuery • executeUpdate – insert, update, delete • close • getResultSet
  12. 12. ResultSet Object • A container for the rows and columns (a table) acquired as a result of presenting a statement object to the RDBMs using the “executeQuery” statement method
  13. 13. ResultSet Class Methods • next • close • wasNull • getString • getBoolean • getByte • getShort
  14. 14. ResultSet Class Methods(cont.) • getInt • getLong • getFloat • getDouble • getNumeric • getBytes • getDate
  15. 15. ResultSet Class Methods (cont.) • getTime • getTimeStamp • getAsciiStream • getUnicodeStream • getBinaryStream • getMetaData • etc...
  16. 16. JDBC Type Mapping • Char - String • Tinyint - byte • Varchar - String • Smallint = short • Longvarchar - String • Integer - int • Numeric - • Bigint - long java.sql.Numeric • Real - float • Decimal - • Float - double java.sql.Numeric • Double - double • Bit - boolean • Binary - byte[ ]
  17. 17. JDBC Type Mapping (cont.) • Varbinary - byte[ ] • Longvarbinary - byte[ ] • Date - java.sql.Date • Time - java.sql.Time • Timestamp - java.sql.Timestamp
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×