13 JDBC 10092024 014625pm
13 JDBC 10092024 014625pm
CSC-210
Object-Oriented Programming
Lecturer
Sameena Javaid
https://sites.google.com/site/sameenajavaidcs
JAVA DATABASE
What is JDBC?
• JDBC is a Java library which allows Java
programs to execute SQL inside databases.
Oracle DB
JDBC in Use
driver
for Oracle
Sybase DB
Java JDBC driver
program
for Sybase
Connectivity
Data processing : // many more
Utilities
non-MS driver
for Access
Access DB
jdbc-odbc odbc
bridge driver
Using UCanAccess
• Download UCanAccess-3.0.0-bin.zip from:
– http://sourceforge.net/projects/ucanaccess/
– unzip in directory with my code and batch files
• Documentation at:
– http://ucanaccess.sourceforge.net/site.html
JDBC as a Diagram
creates creates creates
DriveManager Connection Statement ResultSet
SQL data
DriveManager
• It is responsible for establishing the connection
to the database through the driver.
• e.g.
Class.forName(
"net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn =
DriveManager.getConnection(url);
name of the database
e.g. jdbc:ucanaccess://Books.accdb
Statement Object
• The Statement object provides a ‘workspace’
where SQL queries can be created, executed,
and results collected.
• e.g.
Statement st = conn.createStatement():
ResultSet rs = st.executeQuery(
“select * from Authors”);
:
st.close();
ResultSet Object
• Stores the results of a SQL query.
Continued
© Bahria University Department of Computer Science | Bahria University 16
Discovering Knowledge
Cursor
cursor
23 John
5 Mark
17 Paul
• Cursor operations: 98 Peter
– first(), last(), next(), previous(), etc.
• Typical code:
while( rs.next() ) {
// process the row;
}
© Bahria University Department of Computer Science | Bahria University 17
Discovering Knowledge
Statement st = conn.createStatement();
SimpleJDBC.java
while( rs.next() )
System.out.println( rs.getString("lastName") + ", " +
rs.getString("firstName") );
st.close();
conn.close();
}
catch (ClassNotFoundException e) {
System.out.println("Could not load UCanAccess library: " +
e);
}
catch (SQLException e) {
System.out.println("SQL Exception: " + e);
}
} // end of main()
Execution
Accessing a ResultSet
• The ResultSet class contains many methods
for accessing the value of a column of the
current row
– can use the column name or position
– e.g. get the value in the lastName column:
rs.getString("lastName")
OR
rs.getString(2);
Accessing a ResultSet
• There are many methods for accessing the
data, e.g.
– getString(), getDate(), getInt(),
getFloat(), getObject()
Using MS Access
• MS Access changed its file formats when
Access 2007 was released:
– for Access 2003 (and earlier) you should use
Books.mdb
– for Access 2007 and later, you should use
Books.accdb
TableRelationships in Books.accdb
More Information
• Java: How to Program, 10th edition
Paul Deitel and Harvey Deitel, Pearson, 2015,
Chapter 24
Personal Data
Application
Connecting with
MS-ACCESS
ucanaccess
• http://sourceforge.net/projects/ucanaccess/files/
/*
Retrieving Data from ResultSet
* Reads the fields of Person Table from PersonalInfo database and print the
screen.
*/
// File JdbcExample.java
//step 1: import package
import java.sql.*;
public class JdbcExample {
public static void main (String args[ ]) {
try {
//Step 2: load driverClass.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//Step 3: define the connection URL
String url = "jdbc:ucanaccess://C:/Users/INTEL/Documents/
NetBeansProjects/JDBCDemo/personInfo.accdb";
//Step 4: establish the connection
Connection con = DriverManager.getConnection(url);
//Step 5: create Statement
Statement st = con.createStatement();
//Step 6: preapare & execute the query
String sql = “SELECT * FROM Person”;
ResultSet rs = st.executeQuery(sql);
1.
References
Sharon Zakhour et al, The Java Tutorial Fourth Edition,
http://java.sun.com/docs/books/tutorial
2. Cay Horstmann, Big Java: Earl Objects 5th Edition, John Wiley & Sons,
2013
3. Deitel & Deitel, Java Howto Program 9th Edition, Prentice Hall, 2012
4. Richard M. Reese, Oracle Certified Associate Java SE 7 Programmer
Study Guide, Packt Publishing, 2012
5. Walter Savitch, Absolute Java 5th Edition, Pearson Education, 2013
6. Mark Allen Weiss, Data Structures and Algorithm Analysis in Java 3rd
Edition, Pearson Education, 2012
7. Anany Levitin, Introduction to the Design and Analysis of Algorithms 3rd
Edition, Pearson Education, 2012
8. Ying Bai, Practical Database Programming with Java, John Wiley & Sons,
2011
© Bahria University Department of Computer Science | Bahria University 40