Download Connection - Chen Li -

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

DBase wikipedia , lookup

IBM Notes wikipedia , lookup

Commitment ordering wikipedia , lookup

Serializability wikipedia , lookup

IMDb wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Access wikipedia , lookup

Btrieve wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Functional Database Model wikipedia , lookup

Oracle Database wikipedia , lookup

Ingres (database) wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Concurrency control wikipedia , lookup

Database wikipedia , lookup

SQL wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Versant Object Database wikipedia , lookup

PL/SQL wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Transcript
CS122B: Projects in Databases and Web
Applications
Spring 2017
Notes 2: Database Connectivity
Professor Chen Li
Department of Computer Science
UC Irvine
1
Outline
•
•
•
•
•
Motivation
Architecture
7 steps
Sample program
Metadata
CS122B
Notes 02: Database Connectivity
2
Motivation
• Most popular form of database system
is the relational database system.
– Examples: MS Access, Sybase, Oracle, MS
Sequel Server, Postgres.
• Structured Query Language (SQL) is
used among relational databases to
construct queries.
• Applications need to query databases
CS122B
Notes 02: Database Connectivity
3
Simple Database Application
A
P
P
L
I
C
A
T
I
O
N
CS122B
DBMS
DB
Ex: Access
Oracle
Sybase
Notes 02: Database Connectivity
4
Multi-Databases
A
P
P
L
I
C
A
T
I
O
N
CS122B
DBMS 1
DB
DBMS 2
DB
DBMS 3
DB
Notes 02: Database Connectivity
5
Standard Access to DB
A
P
P
L
I
C
A
T
I
O
N
CS122B
D
R
I
V
E
R
M
G
R
DBMS
Driver 1
DBMS 1
DB
DBMS
Driver 2
DBMS 2
DB
DBMS 3
DB
DBMS
Driver 3
Notes 02: Database Connectivity
6
ODBC Architecture
Application
Class1
Class2
ODBC
Driver Manager
DriverType1
DataSource1
CS122B
DriverType2
DriverType3
DataSource2
Notes 02: Database Connectivity
DataSource3
7
Open Database Connectivity
(ODBC) Standard
• ODBC standard is an interface by which application
programs can access and process SQL databases in a
DBMS-independent manner. It contains:
– A Data Source that is the database, its associated DBMS,
operating system and network platform
– A DBMS Driver that is supplied by the DBMS vendor (e.g.,
Oracle, IBM, MS) or independent software companies
– A Driver Manager that is supplied by the vendor of the O/S
platform (e.g., Windows/UNIX/Mac) where the application is
running
– E.g Control Panel->Administrative Tools->Data Sources (ODBC)
in Windows. You can use ODBC to access MS Access or even
Excel documents using the corresponding drivers.
CS122B
Notes 02: Database Connectivity
8
ODBC Interface
• System-independent interface to database
environment
– requires an ODBC driver to be provided for each
database system from which you want to manipulate
data.
• The ODBC driver manager bridges the
differences between
– The ODBC DBMS-independent interface and the
DBMS-dependent interface of the DBMS driver
CS122B
Notes 02: Database Connectivity
9
An Example
Application
DriverManager
MySQL Driver
CS122B
DB2 driver
Notes 02: Database Connectivity
SQLServer driver
10
Java Support for Database
Connectivity
• When applications written in Java want to access
data sources, they use classes and associated
methods provided by Java DBC (JDBC) API.
• JDBC is specified an “interface.”
• An interface in Java can have many
“implementations.”
– it provides a convenient way to realize many “drivers”
• JDBC can be an interface to an ODBC driver
manager.
• Also it provides a JDBC API as a standard way to
directly connect to common relational databases.
CS122B
Notes 02: Database Connectivity
11
Application in Java
Application in
Java
DriverManager
MySQL Driver
CS122B
DB2 driver
Notes 02: Database Connectivity
SQLServer driver
12
Application in Java
Jdbc API
Application in
Java
CS122B
MySQL Driver
Notes 02: Database Connectivity
13
Java Support for SQL
• Java supports embedded SQL.
– Embedded SQL in Java (SQLJ is one version of it) provides a
standardized way for Java programs to interact with multiple
databases, but at a higher level than the existing JDBC
API.
– Embedded SQL allows connecting to a database by including
SQL code right in the program.
– An Embedded SQL preprocessor converts the SQL statements to Java
code at pre-compile time. The preprocessor generates code that
includes the driver functionality for direct connection to the DBMS via
JDBC.
• java.sql package and an extensive exception
hierarchy.
• We will examine a direct pure Java JDBC driver
using sample code.
CS122B
Notes 02: Database Connectivity
14
An JDBC example
• JdbcExample.java
CS122B
Notes 02: Database Connectivity
15
Data Source and Driver
• Data source: database created using any of
the common database applications.
• Your system should have the driver for the
database you will be using.
– E.g., MS SQL Server on a Windows system.
• There are a number of JDBC drivers available.
CS122B
Notes 02: Database Connectivity
16
JDBC Components
• Driver Manager: Loads database drivers, and
manages the connection between application & driver.
• Driver: Translates API calls to operations for a
specific data source.
• Connection: A session between an application and a
driver.
• Statement: A SQL statement to perform a query or
an update operation.
• Metadata: Information about the returned data,
driver and the database.
• Result Set : Logical set of columns and rows returned
by executing a statement.
CS122B
Notes 02: Database Connectivity
17
JDBC Classes
• Java supports DB facilities by providing
classes and interfaces for its components
• DriverManager class
• Connection interface (abstract class)
• Statement interface (to be instantiated with
values from the actual SQL statement)
• ResultSet interface
CS122B
Notes 02: Database Connectivity
18
java.sql
• JDBC is implemented via classes in the
java.sql package
– Supports SQL-2 entry level
• Define objects for:
– Remote connection to DB
– Executing query
• 8 interfaces to define objects:
– Statement, CallableStatement, PreparedStatement,
DatabaseMetaData, ResultSetMetaData, ResultSet,
Connection, Driver
Notes 02: Database Connectivity
Seven Steps
•
•
•
•
•
•
•
Load the driver
Define the Connection URL
Establish the Connection
Create a Statement object
Execute a query
Process the result
Close the connection
DriverManager
Driver
Connection
Statement
ResultSet
CS122B
Notes 02: Database Connectivity
20
Loading the Driver
• Registering the driver directly
automatically:
Class.forName(“com.mysql.jdbc.Driver");
• Calling Class.forName, which automatically
– creates an instance of the driver
– registers the driver with the DriverManager
CS122B
Notes 02: Database Connectivity
21
Identifying Data Sources
• Gives the required information for
making the connection to the
database
• Specified using the URL format.
<scheme>: <sub_scheme>:<scheme-specific-part>
• Example:
jdbc:postgresql://foo.ics.uci.edu/mydatabase
jdbc:postgresql://localhost/testdb
jdbc:mysql:///mydb
CS122B
Notes 02: Database Connectivity
22
Connection
• A Connection represents a session with
a specific database
• Within the context of a Connection,
SQL statements are executed and
results are returned
CS122B
Notes 02: Database Connectivity
23
Connections
• There can be multiple connections to
a database
• A connection provides “metadata”, i.e.,
information about the database,
tables, and fields
• Connection object has methods to
deal with transactions
CS122B
Notes 02: Database Connectivity
24
Creating a Connection
• Use getConnection on the Driver
Connection getConnection
(String url,
String user,
String password)
• Connects to given JDBC URL with given
user name and password
• Throws java.sql.SQLException
• returns a Connection object
CS122B
Notes 02: Database Connectivity
25
Creating a Connection
Connection connection =
DriverManager.getConnection("jdbc:my
sql:///mydb", "testuser",
"mypassword");
CS122B
Notes 02: Database Connectivity
26
Statements
• Statement createStatement()
– returns a new Statement object
– Used for general queries
• PreparedStatement prepareStatement(String sql)
– returns a new PreparedStatement object
– For a statement called multiple times with different values
(precompiled to reduce parsing time)
• CallableStatement prepareCall(String sql)
– returns a new CallableStatement object
– For stored procedures
CS122B
Notes 02: Database Connectivity
27
Statements
• A Statement object is used for
executing a static SQL statement and
obtaining the results produced by it
• executeQuery is used for statements
that return an output result
• executeUpdate is used for statements
that need not return an output
CS122B
Notes 02: Database Connectivity
28
Executing Queries and Updates
• ResultSet executeQuery(String)
– Execute a SQL statement that returns a
single ResultSet
• int executeUpdate(String)
– Execute a SQL INSERT, UPDATE or
DELETE statement
– Used for CREATE TABLE, DROP TABLE and
ALTER TABLE
– Returns the number of rows changed
CS122B
Notes 02: Database Connectivity
29
Timeout
• Use setQueryTimeOut to set a timeout
for the driver to wait for a statement
to be completed
• If the operation is not completed in the
given time, an SQLException is thrown
• What is it good for?
CS122B
Notes 02: Database Connectivity
30
Cursor
• What is the result of a query?
• How can a database send the result of a
query through communication lines?
• The answer: using a cursor
CS122B
Notes 02: Database Connectivity
31
Result Set
• A ResultSet provides access to a table of data
generated by executing a Statement
• Only one ResultSet per Statement can be
open at once
• The table rows are retrieved in sequence
• A ResultSet maintains a cursor pointing to its
current row of data
• The 'next' method moves the cursor to the
next row
– you can’t rewind
CS122B
Notes 02: Database Connectivity
32
Working with ResultSet
• boolean next()
– activates the next row
– the first call to next() activates the first row
– returns false if there are no more rows
• void close()
– disposes of the ResultSet
– automatically called by most Statement methods
CS122B
Notes 02: Database Connectivity
33
Getting Values from Rows
• Type getType(int columnIndex)
– returns the given field as the given type
– E.g., int getInt(5); string getString(3);
– fields indexed starting at 1 (not 0)
• Type getType(String columnName)
– same, but uses name of field
– less efficient
• int findColumn(String columnName)
– looks up column index given column name
Notes 02: Database Connectivity
isNull
• In SQL, NULL means the field is empty
• Not the same as 0 or “”
• In JDBC, you must explicitly ask if a
field is null by calling
ResultSet.isNull(column)
Notes 02: Database Connectivity
Mapping Java Types to SQL Types
SQL type
Java Type
CHAR, VARCHAR, LONGVARCHAR
String
NUMERIC, DECIMAL
java.math.BigDecimal
BIT
boolean
TINYINT
byte
SMALLINT
short
INTEGER
int
BIGINT
long
REAL
float
FLOAT, DOUBLE
double
BINARY, VARBINARY, LONGVARBINARY
byte[]
DATE
java.sql.Date
TIME
java.sql.Time
TIMESTAMP
java.sql.Timestamp
Notes 02: Database Connectivity
Database Time
• Times in SQL are nonstandard
• Java defines three classes to help
• java.sql.Date
– year, month, day
• java.sql.Time
– hours, minutes, seconds
• java.sql.Timestamp
– year, month, day, hours, minutes, seconds,
nanoseconds
– usually use this one
Notes 02: Database Connectivity
Optimized Statements
• Prepared Statements
– SQL calls that you make again and again
– allows driver to optimize (compile) queries
– created with Connection.prepareStatement()
• Stored Procedures
– written in DB-specific language
– stored inside database
– accessed with Connection.prepareCall()
CS122B
Notes 02: Database Connectivity
38
Prepared Statement Example
PreparedStatement updateSales;
String updateString = "update COFFEES " +
"set SALES = ? where COF_NAME like ?";
updateSales = con.prepareStatement(updateString);
int [] salesForWeek = {175, 150, 60, 155, 90};
String [] coffees = {"Colombian", "French_Roast",
"Espresso","Colombian_Decaf","French_Roast_Decaf"};
int len = coffees.length;
for(int i = 0; i < len; i++) {
updateSales.setInt(1, salesForWeek[i]);
updateSales.setString(2, coffees[i]);
updateSales.executeUpdate();
}
CS122B
Notes 02: Database Connectivity
39
JDBC Class Diagram
Notes 02: Database Connectivity
Metadata
• Connection:
– DatabaseMetaData getMetaData()
• ResultSet:
– ResultSetMetaData getMetaData()
CS122B
Notes 02: Database Connectivity
41
ResultSetMetaData
•
•
•
•
•
What's the number of columns in the ResultSet?
What's a column's name?
What's a column's SQL type?
What's the column's normal max width in chars?
What's the suggested column title for use in
printouts and displays?
• What's a column's number of decimal digits?
• Does a column's case matter?
• and so on...
Notes 02: Database Connectivity
DatabaseMetaData
•
•
•
•
What tables are available?
What's our user name as known to the database?
Is the database in read-only mode?
If table correlation names are supported
(association of a column with the table it comes
from, when multiple columns of the same name
appear in the same query - multi-table queries) ,
are they restricted to be different from the
names of the tables?
• and so on…
Notes 02: Database Connectivity
Useful Methods of Metadata
•
•
•
•
•
getColumnCount
getColumnDisplaySize
getColumnName
getColumnType
isNullabale
Imagine the case where you want to print
the result
CS122B
Notes 02: Database Connectivity
44
Transaction Management
• A transaction: a sequence of SQL statements
• Transactions are not explicitly opened and
closed
• Instead, the connection has a state called
AutoCommit mode
• if AutoCommit is true, then every statement is
automatically committed
• default case: true
CS122B
Notes 02: Database Connectivity
45
AutoCommit
Connection.setAutoCommit(boolean)
• if AutoCommit is false, then every
statement is added to an ongoing
transaction
• you must explicitly commit or rollback
the transaction using
Connection.commit() and
Connection.rollback()
CS122B
Notes 02: Database Connectivity
46
AutoCommit (cont)
Check:
• http://docs.oracle.com/javase/tutorial/jdbc
/basics/transactions.html
• http://www.devx.com/tips/Tip/15015
• There's a slight difference between doing
them in SQL and JDBC: auto-commit
control and commit call in JDBC are Java
calls (not SQL).
CS122B
Notes 02: Database Connectivity
47