COMPE 438 JAVA PROGRAMMING - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

COMPE 438 JAVA PROGRAMMING

Description:

String DATABASE_URL = 'jdbc:odbc:phonebook' String DATABASE_URL = 'jdbc:mysql://localhost:80/phonebook' 7 Steps to use JDBC(with example) ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 15
Provided by: CEM4
Category:

less

Transcript and Presenter's Notes

Title: COMPE 438 JAVA PROGRAMMING


1
COMPE 438JAVA PROGRAMMING
  • F. Cemile Serçe

2
Java DataBase Connectivity(JDBC)
3
JDBC
  • The JDBC API is a Java API that can access any
    kind of tabular data
  • JDBC helps you to write java applications that
    manage three programming activities
  • Connect to a data source, like a database
  • Send queries and update statements to the
    database
  • Retrieve and process the results received from
    the database in answer to your query

4
Example
Connection con DriverManager.getConnection
( "jdbcmyDrivermyDB,"myLogin","myPassw
ord") Statement stmt con.createStatement(
) ResultSet rs stmt.executeQuery("SELECT
a, b, c FROM Table1") while (rs.next())
int x rs.getInt("a") String s
rs.getString("b") float f rs.getFloat("c")

5
JDBC Drivers
  • JDBC consists of two parts
  • JDBC API, a purely Java-based API
  • JDBC Driver Manager,which communicates with
    vendor-specific drivers that perform the real
    communication with the database.

6
JDBC Data Types
7
7 Steps to use JDBC
  • Load the driver
  • Define the Connection URL
  • Establish the Connection
  • Create a Statement object
  • Execute a query and
  • Process the results
  • Close the connection

8
7 Steps to use JDBC(with example)
  • Load the driver
  • Before a connection to a database can be
    established, load the JDBC driver

try driverName sun.jdbc.odbc.JdbcOdbcDrive
r Class.forName(driverName) catch
(ClassNotFoundException e) // Could not find
the driver
try // Load the JDBC driver String (MySQL)
driverName "org.gjt.mm.mysql.Driver"
Class.forName(driverName) catch
(ClassNotFoundException e) // Could not find
the driver
9
7 Steps to use JDBC(with example)
  • Define the Connection URL
  • jdbcsubprotocolsubname
  • protocol name i.e. jdbc
  • subprotocol represents the database you want to
    connect to e.g. mysql, oracle, odbc etc.
  • subname provides additional information on how
    and where to connect

String DATABASE_URL "jdbcodbcphonebook"
String DATABASE_URL "jdbcmysql//localhost80/p
honebook"
10
7 Steps to use JDBC(with example)
  • Establish the Connection

String username admin" String password
sa" Connection connection DriverManager.getCo
nnection(DATABASE_URL, username, password)
11
7 Steps to use JDBC(with example)
  • Create a Statement object
  • Statement object for invocation
  • Statement stmt
  • connection.createStatement()
  • Execute a Query and 6. Process Results
  • ResultSet object for result processing
  • ResultSet rset stmt.executeQuery("SELECT
    address,script,type FROM worklist")
  • Close the connection
  • connection.close()

12
7 Steps to use JDBC(with example)
  • Create a Statement object
  • Statement object for invocation
  • Statement stmt
  • connection.createStatement()
  • Execute a Query and 6. Process Results
  • ResultSet object for result processing
  • ResultSet rset stmt.executeQuery("SELECT
    address,script,type FROM worklist")
  • Close the connection
  • connection.close()

13
Online Resources
14
Online Resources
  • Suns JDBC Site
  • http//java.sun.com/products/jdbc/
  • JDBC Tutorial
  • http//java.sun.com/docs/books/tutorial/jdbc/
  • List of Available JDBC Drivers
  • http//industry.java.sun.com/products/jdbc/drivers
    /
  • API for java.sql
  • http//java.sun.com/j2se/1.4/docs/api/java/sql/pac
    kage-summary.html
Write a Comment
User Comments (0)
About PowerShow.com