JDBC - PowerPoint PPT Presentation

About This Presentation
Title:

JDBC

Description:

JDBC. CS 122. JDBC. Java Database Connectivity. Database Access Interface ... Driver that interfaces with ODBC (Object Database Connectivity--also an access interface) ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 13
Provided by: johnpaul3
Category:

less

Transcript and Presenter's Notes

Title: JDBC


1
JDBC
  • CS 122

2
JDBC
  • Java Database Connectivity
  • Database Access Interface
  • provides access to a relational database (by
    allowing SQL statements to be sent and executed
    through a Java program)
  • JDBC package set of Java classes that
    facilitate this access (java.sql.)
  • Comes with JDK (since 1.1)

3
JDBC Driver
  • Need a driver, specific to the DB product, to
    mediate between JDBC and the database
  • the driver is a Java class that needs to be
    loaded first

Relational DBMS
Java Program - load driver - establish
connection - send SQL statements
4
JDBC-ODBC Bridge
  • Driver that interfaces with ODBC (Object Database
    Connectivity--also an access interface)
  • Easiest way to access databases created by
    Microsoft products
  • register database as an ODBC data source
  • use JDBC-ODBC bridge as the JDBC driver (included
    in JDK 1.2 distribution)

5
Key Classes in JDBC
  • Connection
  • need to create an instance of this class when
    establishing a connection to the database
  • Statement
  • for issuing SQL statements
  • ResultSet (interface)
  • a ResultSet object represents the table returned
    by an SQL select statement

6
Establishing a Connection
  • Use the getConnection() method
  • under the DriverManager class
  • String argument "jdbcdrivername
  • returns a Connection object
  • Class.forName(sun.jdbc.odbc.JdbcOdbcDriver)
  • // above line loads the jdbc-odbc driver
  • String dbname jdbcodbcMyDB
  • Connection c Driver.getConnection(dbname)

7
Creating aStatement Object
  • Execute the createStatement() method on the
    Connection object
  • returns a Statement object
  • afterwards, run methods on the Statement object
    to execute an SQL statement
  • Statement s c.createStatement()

8
Methods of theStatement Class
  • executeQuery()
  • requires a String argument (a select statement)
  • returns a ResultSet object
  • executeUpdate()
  • requires a String argument (an insert, update, or
    delete statement)
  • returns an int (row count, in most cases)

9
The ResultSet Interface
  • A ResultSet object represents the table returned
    by the select statement sent
  • Navigation/retrieval methods
  • next() moves to the next row (first row if
    called for the first time), returns false if no
    rows remain
  • getXXX methods return the value of a field for
    the current row

10
get Method Example getInt()
  • ResultSet rs
  • rs s.executeQuery(SELECT FROM Orders)
  • rs.next() // gets the first row
  • // suppose the Orders table has an integer field
  • // called quantity
  • int myvar rs.getInt(quantity)
  • // if you knew that quantity is the 2nd field in
    the table
  • myvar rs.getInt(2)

11
Exercise
  • Create a Microsoft Access table
  • insert sample rows
  • Add an ODBC data source
  • use the Microsoft Access driver
  • associate with the created database
  • Create a Java program
  • use JDBC-ODBC bridge
  • create a loop that lists all rows of the table

12
Summary
  • JDBC allows you to write Java programs that
    manipulate a database
  • A driver (often a separate product) is required
    that facilitates access
  • Key classes Connection, Statement, and
    ResultSet
  • Other features metadata, parameterized
    statements, and stored-proc invocation
Write a Comment
User Comments (0)
About PowerShow.com