Computer Science [3] Java Programming II - Laboratory Course - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Computer Science [3] Java Programming II - Laboratory Course

Description:

Faculty of Engineering & IT Software Engineering Department Computer Science [3] Java Programming II - Laboratory Course Lab 9: Accessing Databases with JDBC – PowerPoint PPT presentation

Number of Views:168
Avg rating:3.0/5.0
Slides: 14
Provided by: JohnLe165
Category:

less

Transcript and Presenter's Notes

Title: Computer Science [3] Java Programming II - Laboratory Course


1
Computer Science 3 Java Programming II -
Laboratory Course
Faculty of Engineering IT Software Engineering
Department
  • Lab 9
  • Accessing Databases with JDBC
  • Instructions on Setting Accesses
  • Connecting to and Querying a Database

WWW.PALINFONET.COM
Eng.Omar Al-Nahal
2
JDBC
  • JDBC is a platform-independent Java API for
    executing SQL statements
  • Use JDBC to
  • Connect to a database
  • Send SQL statements
  • Receive results
  • i.e. rows of data
  • Add or update existing rows of data

JDBC
3
General Architecture
  • What design pattern is implied in this
    architecture?

4
JDBC Driver Manager
5
Basic steps to use a database in Java
  • 1.Establish a connection
  • 2.Create JDBC Statements
  • 3.Execute SQL Statements
  • 4.GET ResultSet
  • 5.Close connections

6
JDBC Drivers
  • Before connecting to a database,a driver class
    must first be loaded into the JVM
  • A driver is simply a class in Java
  • Access "sun.jdbc.odbc.JdbcOdbcDriver"
  • MySQL "com.mysql.jdbc.Driver"
  • Oracle "oracle.jdbc.driver.OracleDriver"
  • Oracle driver is located within a JAR fileof the
    Oracle distribution

7
JDBC Drivers
  • To load a driver, use the static forName() method
    of the java.lang.Class class
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • For Oracle
  • Class.forName("oracle.jdbc.driver.OracleDriver")

8
JDBC Connection Strings
  • Connect to a database using its connect
    string(i.e. its Connection URL)
  • Access "jdbcodbcdataSource"
  • MySQL "jdbcmysql//hostname/dbname"
  • Oracle "jdbcoraclethin_at_hostnameportSID"
  • Use a Connection object
  • Connection connection DriverManager.getCo
    nnection( dbConnectURL )

9
Sample Access Database
10
Using JDBC to Process Data
11
Connecting to Oracle via JDBC
  • Load the appropriate Oracle driver
  • Class.forName("oracle.jdbc.driver.OracleDriver")
  • Connect to an Oracle database usingthe
    appropriate Oracle connect string
  • jdbcoraclethinuserid/passwd_at_hostnameportSID

12
Connecting to Oracle via JDBC
  • Always do your best to be sure queriesare
    database-independent
  • Consider using property files to maintain
    database connectivity information
  • Refer to the java.util.Properties class

13
JDBC
  • JDBC Data Access API JDBC Technology Homepage
  • http//java.sun.com/products/jdbc/index.html
  • JDBC Database Access The Java Tutorial
  • http//java.sun.com/docs/books/tutorial/jdbc/index
    .html
  • JDBC Documentation
  • http//java.sun.com/j2se/1.4.2/docs/guide/jdbc/ind
    ex.html
  • java.sql package
  • http//java.sun.com/j2se/1.4.2/docs/api/java/sql/p
    ackage-summary.html
  • JDBC Technology Guide Getting Started
  • http//java.sun.com/j2se/1.4.2/docs/guide/jdbc/get
    start/GettingStartedTOC.fm.html
  • JDBC API Tutorial and Reference (book)
  • http//java.sun.com/docs/books/jdbc/
Write a Comment
User Comments (0)
About PowerShow.com