Java Programming Chpater 7 JDBC Database Access
Java Programming Chpater 7 JDBC Database Access
JDBC Introduction
• Standard Java library for accessing relational databases
– JDBC standardizes:
Database Access with JDBC • Establishing connection to database
• Approach to initiating queries
• Methods for creating stored (parameterized) queries
• The data structures for query results (table)
– Determining the number of columns, metadata, etc.
– API does not standardize SQL syntax
• JDBC is not embedded SQL
– JDBC classes are in the java.sql package
1 2
3 4
1
1/22/2019
Statement Jdbc:oracle:thin:@host:port:database
jdbc:oracle:thin:@localhost:1521:ORCL
ResultSet
5 6
DriverManager Connection
DriverManager Method:
• A Connection represents a session with a specific database.
Connection getConnection (String url, String user, String password) • Within the context of a Connection, SQL statements are
executed and results are returned.
Connects to given JDBC URL with given user name and
• Also provides “metadata” -- information about the database,
password
tables, and fields
Throws java.sql.SQLException
returns a Connection object
• Also methods to deal with transactions
7 8
2
1/22/2019
{ e.printStackTrace(); }
catch (SQLException e)
{ e.printStackTrace(); }
9 10
11 12
3
1/22/2019
13 14
15 16
4
1/22/2019
• The table rows are retrieved in sequence. – returns false if there are no more rows
• void close()
• A ResultSet maintains a cursor pointing to its current
– disposes of the ResultSet
row of data.
– allows you to re-use the Statement that created it
• The 'next' method moves the cursor to the next row. – automatically called by most Statement methods
17 18
19 20
5
1/22/2019
21 22
23 24
6
1/22/2019
27 28
7
1/22/2019
29 30
31 32