Download presentation
Presentation is loading. Please wait.
Published byGeorge Byrd Modified over 9 years ago
1
Computer Science [3] Java Programming II - Laboratory Course Lab 9: Accessing Databases with JDBC Instructions on Setting Accesses Connecting to and Querying a Database Faculty of Engineering & IT Software Engineering Department WWW.PALINFONET.COM Eng.Omar Al-Nahal Eng.Omar Al-Nahal
2
b JDBC is a platform-independent Java API for executing SQL statements b Use JDBC to: Connect to a databaseConnect to a database Send SQL statementsSend SQL statements Receive resultsReceive results –i.e. rows of data Add or update existing rows of dataAdd or update existing rows of data JDBC Java program database JDBC
3
General Architecture b What design pattern is implied in this architecture?
4
JDBC Driver Manager
5
Basic steps to use a database in Java b 1.Establish a connection b 2.Create JDBC Statements b 3.Execute SQL Statements b 4.GET ResultSet b 5.Close connections
6
b Before connecting to a database, a driver class must first be loaded into the JVM b A driver is simply a class in Java Access: "sun.jdbc.odbc.JdbcOdbcDriver"Access: "sun.jdbc.odbc.JdbcOdbcDriver" MySQL: "com.mysql.jdbc.Driver"MySQL: "com.mysql.jdbc.Driver" Oracle: "oracle.jdbc.driver.OracleDriver"Oracle: "oracle.jdbc.driver.OracleDriver" b Oracle driver is located within a JAR file of the Oracle distribution JDBC Drivers
7
To load a driver, use the static forName() method of the java.lang.Class class: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); b For Oracle: Class.forName("oracle.jdbc.driver.OracleDriver"); Class.forName("oracle.jdbc.driver.OracleDriver"); JDBC Drivers
8
b Connect to a database using its connect string (i.e. its Connection URL): Access: "jdbc:odbc:dataSource"Access: "jdbc:odbc:dataSource" MySQL: "jdbc:mysql://hostname/dbname"MySQL: "jdbc:mysql://hostname/dbname" Oracle: "jdbc:oracle:thin:@hostname:port#:SID"Oracle: "jdbc:oracle:thin:@hostname:port#:SID" Use a Connection object: Connection connection = DriverManager.getConnection( dbConnectURL ); Connection connection = DriverManager.getConnection( dbConnectURL ); JDBC Connection Strings
9
Sample Access Database
10
Using JDBC to Process Data
11
b Load the appropriate Oracle driver: Class.forName("oracle.jdbc.driver.OracleDriver"); Class.forName("oracle.jdbc.driver.OracleDriver"); b Connect to an Oracle database using the appropriate Oracle connect string : jdbc:oracle:thin:userid/passwd@hostname:port#:SID jdbc:oracle:thin:userid/passwd@hostname:port#:SID Connecting to Oracle via JDBC
12
b Always do your best to be sure queries are database-independent b Consider using property files to maintain database connectivity information Refer to the java.util.Properties classRefer to the java.util.Properties class Connecting to Oracle via JDBC
13
JDBC JDBC Data Access API – JDBC Technology Homepage http://java.sun.com/products/jdbc/index.htmlhttp://java.sun.com/products/jdbc/index.htmlhttp://java.sun.com/products/jdbc/index.html JDBC Database Access – The Java Tutorial http://java.sun.com/docs/books/tutorial/jdbc/index.htmlhttp://java.sun.com/docs/books/tutorial/jdbc/index.htmlhttp://java.sun.com/docs/books/tutorial/jdbc/index.html b JDBC Documentation http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.htmlhttp://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.htmlhttp://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.html b java.sql package http://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.html b JDBC Technology Guide: Getting Started http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.htmlhttp://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.htmlhttp://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html b JDBC API Tutorial and Reference (book) http://java.sun.com/docs/books/jdbc/http://java.sun.com/docs/books/jdbc/http://java.sun.com/docs/books/jdbc/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.