CECS 323: Mimi Opkins
CECS 323: Mimi Opkins
CECS 323: Mimi Opkins
Mimi Opkins
Introduction UML design
Basic UML & SQL Many-to-Many
Models Many-to-Many 2
Classes & Schemes Subkeys
Rows & Tables Repeated Attributes
Associations Multi-Valued Attributes
Keys Domains
SQL technique Enumerated Domains
SQL and RA Subclasses
DDL & DML Aggregation
Join Recursive Associations
Multiple Joins Normalization
Join Types BCNF
Functions Transactions
Subqueries JDBC
Union & Minus
Views & Indexes
Check Constraints
Introduction
What is a database?
Why do we need one?
Avoid redundancy
duplication of information in multiple tables within a database
Data integrity
Refers to the validity of data
Referential Integrity
ensures that relationships between tables remain consistent
Deletion Anomalies
Deletion of one row of a table results in the deletion of
unintended information
Different Types of Databases
Paper
Flatfile
Hierarchical
Network
Relational
Object
Introduction – Redundancy
The duplication of information in multiple tables
within a database
Ex. School Records may all have info about you
Admissions
Enrollment Services
Department
Student Union
Introduction – Data Integrity
Refers to the validity of data.
Data integrity can be compromised in a number
of ways:
Human errors when data is entered
Errors that occur when data is transmitted from one
computer to another
Software bugs or viruses
Hardware malfunctions, such as disk crashes
Natural disasters, such as fires and floods
Introduction – Referential Integrity
Referential integrity ensures that relationships
between tables remain consistent.
When one table has a foreign key to another table,
the concept of referential integrity states that you may
not add a record to the table that contains the foreign
key unless there is a corresponding record in the
linked table.
It also includes the techniques known as cascading
update and cascading delete, which ensure that
changes made to the linked table are reflected in the
primary table.
Introduction – Anomalies
An anomaly is an inconsistent, incomplete, or
contradictory state of the database
Insertion anomaly – user is unable to insert a
new record when it should be possible to do so
Deletion anomaly – when a record is deleted,
other information that is tied to it is also deleted
Update anomaly –a record is updated, but other
appearances of the same items are not updated
Anomaly Examples: NewClass
Table
NewClass(courseNo, stuId, stuLastName, fID, schedule,
room, grade)
Each attribute of the Customers scheme is assigned a value from its domain:
Customer first name is assigned the value "Tom", from the domain of
people's first names.
Customer last name is assigned the value "Jewett", from the domain of
people's last names.
Customer phone is assigned the value "714-555-1212", from the domain
valid telephone numbers.
Customer street is assigned the value "10200 Slater", from the domain of
street addresses.
Customer zip code is assigned the value "92708", from the domain of zip
codes designated by the United States Postal Service.
Each of the assignments results in a data cell of the row or tuple.
Basic Structures: Rows and Tables
– Formal Notation
In formal notation, we could show the assignments
explicitly, where t represents a tuple:
tTJ = ‹cfirstname := 'Tom', clastname := 'Jewett',
cphone := '714-555-1212', cstreet := '10200
Slater', czipcode := '92708'›
Database:
INSERT INTO customers (cfirstname,
clastname, cphone, cstreet, czipcode)
VALUES ('Tom', 'Jewett', '714-555-1212',
'10200 Slater', '92708');
UPDATE customers SET cphone = '714-555-2323'
WHERE cphone = '714-555-1212';
Basic Structures: Rows and Tables
- Database
A database table is simply a collection of zero or
more rows. This follows from the relational
model definition of a relation as a set of tuples
over the same scheme. Order is not important.
Additional rows are built on the Customers scheme as before. The table or
relation consists of all rows.
Three of the attributes in the Customers scheme are now identified as the
primary key, which is explained later on.
Basic Structures: Rows and Tables
- Tuples
Knowing that the relation (table) is a set of tuples (rows)
tells us more about this structure, as we saw with
schemes and domains.
Each tuple/row is unique; there are no duplicates
Tuples/rows are unordered; we can display them in any way we
like and the meaning doesn’t change. (SQL gives us the
capability to control the display order.)
Tuples/rows may be included in a relation/table set if they are
constructed on the scheme of that relation; they are excluded
otherwise. (It would make no sense to have an Order row in the
Customers table.)
We can define subsets of the rows by specifying criteria for
inclusion in the subset. (Again, this is part of a SQL query.)
We can find the union, intersection, or difference of the rows in
two or more tables, as long as they are constructed over the
same scheme.
Basic Structures: Rows and Tables
– Insuring Unique Rows
Each row in a table must be distinct. So
there must be a set of attributes in each
relation that guarantee uniqueness. Any
set of attributes that can do this is called a
super key of the relation.
The database designer picks of the
possible super key sets to serve as the
primary key or unique identifier of each
row.
Basic Structures: Rows and Tables
– Super Keys
cfirstname,clastname,cphone,cstreet,czipcode
cfirstname,clastname,cphone,cstreet
cfirstname,clastname,cphone,czipcode
cfirstname,clastname,cstreet,czipcode
cfirstname,clastname,cphone,
cfirstname,clastname,cstreet
Basic Structures: Rows and Tables
– Insuring Unique Rows - SQL
No, I'm not making this up. There really were three fields to hold
information about degrees that instructors had earned. The data
in each degree field looked something like: MS in Computer
Science, 1980, UC Santa Barbara.
Exercise: Faculty Degrees
Revise the class diagram to correct any
problems that you find in this design.
Then draw the relation scheme for your
corrected model.
Design Pattern: Multi-Valued
Attributes (Hobbies)
When there are many distinct values entered in
the same column of the table we have another
design problem called multi-valued attributes .
This makes it difficult to search the table for any
one particular value and it is impossible to
create a query that will individual list the values
in that column. For example: hobbies.
Design Pattern: Multi-Valued
Attributes (Hobbies) – Class Diagram
Design Pattern: Multi-Valued
Attributes (Hobbies) - Corrected
Exercise: Software List
Sometimes it takes more than just a glance at
the class diagram to spot problems with a
design.
Consider the following class type that might be
used by a software vendor to list software titles
that are available.
Exercise: Software List
There is nothing obviously wrong with this design.
However, the users of this database might enter data
that would cause problems, as shown in this table:
NewStu(StuId,
S1060 Jones lastName,
CSC 25 major, Freshman
credits,064624738
status,
socSecNo) – Assume students can have more
than one major
The major attribute is not single-valued for each
tuple
Ensuring 1NF
FDs:
{courseNo,stuId} → {lastName}
{courseNo,stuId} →{facId}
{courseNo,stuId} →{schedule}
{courseNo,stuId} →{room}
{courseNo,stuId} →{grade}
courseNo → facId **partial FD
courseNo → schedule **partial FD
courseNo →room ** partial FD
stuId → lastName ** partial FD
…plus trivial FDs that are partial…
Second Normal Form-2NF
A relation is in second normal form (2NF) if it is
in first normal form and all the non-key attributes
are fully functionally dependent on the key.
No non-key attribute is FD on just part of the key
If key has only one attribute, and R is 1NF, R is
automatically 2NF
Converting to 2NF
Identify each partial FD
Remove the attributes that depend on each of
the determinants so identified
Place these determinants in separate relations
along with their dependent attributes
In original relation keep the composite key and
any attributes that are fully functionally
dependent on all of it
Even if the composite key has no dependent
attributes, keep that relation to connect logically
the others
2NF Example
NewClass(courseNo, stuId, stuLastName, facId, schedule, room, grade )
Example:
NewStudent (stuId, lastName, major, credits, status)
FD:
credits→status (and several others)
By transitivity:
stuId→credits credits→status implies stuId→status
S1010
TransitiveBurns Art Stuid 63
Dependency: Junior
Credits and Credits Status
NewStu2 Stats
Stuid lastName Major Credits Credits Status
S1001 Smith History 90 15 Freshman
S1060
S1003 Jones
Jones CSC
Math 25
95 Freshman
S1006 Lee CSC 15 25 Freshman
63 Junior
FDs:
office → dept
facName,dept → office, rank, dateHired
facName,office → dept, rank, dateHired
Note we have lost a functional dependency in Fac2 – no longer able to see that
{facName, dept} is a determinant, since they are in different relations
Example Boyce-Codd Normal Form
Faculty
facName dept office rank dateHired
Adams Art A101 Professor 1975
Byrne Math M201 Assistant 2000
Davis Art A101 Associate 1992
Gordon Math M201 Professor 1982
Hughes Mth M203 Associate 1990
Smith CSC C101 Professor 1980
Smith History H102 Associate 1990
Tanaka CSC C101 Instructor 2001
Vaughn CSC C101 Associate 1995
Fac1 Fac2
office dept facName office rank dateHired
A101 Art Adams A101 Professor 1975
C101 CSC Byrne M201 Assistant 2000
C105 CSC Davis A101 Associate 1992
H102 History Gordon M201 Professor 1982
M201 Math Hughes M203 Associate 1990
M203 Math Smith C101 Professor 1980
Smith H102 Associate 1990
Tanaka C101 Instructor 2001
Vaughn C101 Associate 1995
Converting to BCNF
Identify all determinants and verify that they are
superkeys in the relation
If not, break up the relation by decomposition
for each non-superkey determinant, create a separate
relation with all the attributes it determines, also
keeping it in original relation
Preserve the ability to recreate the original relation by
joins.
Repeat on each relation until you have a set of
relations all in BCNF
Normalization Example
Relation that stores information about projects in
large business
Work (projName, projMgr, empId, hours, empName,
budget, startDate, salary, empMgr, empDept, rating)
prijName projMgr empId hours Emp budget startDate salary Emp Emp rating
Name Mgr Dept
Jupiter Smith E101 25 Jones 100000 01/15/04 60000 Levine 10 9
Emp
Proj Work1
prijName empId hours rating
prijName projMgr budget startDate Jupiter E101 25 9
Jupiter Smith 100000 01/15/04
Jupiter E105 40
Maxima Lee 200000 03/01/04
Jupiter E110 10 8
Maxima E101 15
Maxima E110 30
Maxima E120 15
FDs:
office → dept
facName,dept → office, rank, dateHired
facName,office → dept, rank, dateHired
If, after all possible changes have been made to S, a row is made
up entirely of a symbols, a(1, a(2, …,a(n), then the join is lossless.
If there is no such row, the join is lossy.
Fourth Normal Form (4NF)
Defined as a relation that is in Boyce-Codd
Normal Form and contains no nontrivial multi-
valued dependencies.
MVD between attributes A, B, and C in a relation
using the following notation:
A −>> B
A −>> C
206
Multi-valued Dependency
In R(A,B,C) if each A values has associated with
it a set of B values and a set of C values such
that the B and C values are independent of each
other, then A multi-determines B and A multi-
determines C
Multi-valued dependencies occur in pairs
Example: JointAppoint(facId, dept, committee)
assuming a faculty member can belong to more
than one department and belong to more than
one committee
Table must list all combinations of values of
department and committee for each facId
4NF Defined
A table is 4NF if it is BCNF and has no multi-
valued dependencies
Example: remove MVDs in JointAppoint
Appoint1(facId,dept)
Appoint2(facId,committee)
Consider the following example:
209
Dependencies
The dependencies are:
{Restaurant} ↠ {Pizza Variety}
{Restaurant} ↠ {Delivery Area}
These non-trivial multivalued dependencies on a
non-superkey reflect the fact that the varieties of
pizza a restaurant offers are independent from
the areas to which the restaurant delivers.
This state of affairs leads to redundancy in the
table.
Redundancy
For example, we are told three times that A1
Pizza offers Stuffed Crust, and if A1 Pizza starts
producing Cheese Crust pizzas then we will
need to add multiple rows, one for each of A1
Pizza's delivery areas.
There is nothing to prevent us from doing this
incorrectly: we might add Cheese Crust rows for
all but one of A1 Pizza's delivery areas, thereby
failing to respect the multivalued dependency
{Restaurant} ↠ {Pizza Variety}.
Normalizing to 4NF
To eliminate the possibility of these anomalies, we
must place the facts about varieties offered into a
different table from the facts about delivery areas,
yielding two tables that are both in 4NF:
Modified Example
In contrast, if the pizza varieties offered by a
restaurant sometimes did legitimately vary from
one delivery area to another, the original three-
column table would satisfy 4NF.
Another 4NF Example
BranchStaffOwner
Has two independent, mulivalued dependencies in
same table:
branchNo ->> sName (branches have multiple staff)
branchNo ->> oName (branches have multiple properties
which may have different owners)
End up having to duplicate tuples to show each staff
member with each owner
E.g., add a new staff member, have to add 2 new tuples, one
for each oName
Solution: make a separate relation for each
dependency
4NF - Example
215
Fifth Normal Form (5NF)
A relation is 5NF if there are no remaining non-
trivial lossless projections
In 5NF if cannot be decomposed into further
relations with a lossless join
In other words, in 5NF, all relations are
decomposed into as many relations as possible
without introducing errors
All tables are key, and attributes that depend on key;
no other fd
Only rarely does a 4NF not conform to 5NF
5NF – Example (table is in 4NF)
Traveling Salesman Brand Product Type
218
Pearson Education © 2014
5NF - Example
Instead, decompose into 3 relations:
Traveling Salesman -> Product Type
Traveling Salesman -> Brand
Brand -> Product Type
219
Pearson Education © 2014
Traveling Salesman Brand
Mary Jones
Acme
Robusto
The scheme of the result of πXr is X. The tuples resulting from this
operation are tuples of the original relation, r, cut down to the attributes
contained in X.
For X to be a subscheme of R, it must be a subset of the attributes in R, and
preserve the assignment rule from R (that is, each attribute of X must have the
same domain as its corresponding attribute in R).
If X is a super key of r, then there will be the same number of tuples in the result
as there were to begin with in r. If X is not a super key of r, then any duplicate
(non-distinct) tuples are eliminated from the result.
Just as in the SQL statement, we can apply the project operator to the
output of the select operation to produce the results that we want: πXσθr or
πcLastName, cFirstName, cPhone σcZipCode='90840'customers.
Basic queries: SQL and RA – Select
and Project Step 4
4. Since RA considers relations strictly as sets of
tuples, there is no way to specify the order of
tuples in a result relation.
Basic SQL Statements: DDL and DML
SQL statements are divided into two major
categories:
data definition language (DDL) - used to build and
modify the structure of your tables and other objects
in the database
data manipulation language (DML) - used to work
with the data in tables
All of the information about objects in your
schema is contained in a set of tables called the
data dictionary.
Basic SQL Statements: DDL and DML
– DDL- CREATE Statement
The CREATE TABLE statement does exactly that:
CREATE TABLE <table name> (
<attribute name 1> <data type 1>,
...
<attribute name n> <data type n>);
The data types that you will use most frequently are
character strings, which might be called VARCHAR or
CHAR for variable or fixed length strings; numeric types
such as NUMBER or INTEGER, which will usually
specify a precision; and DATE or related types.
Data type syntax is variable from system to system; the
only way to be sure is to consult the documentation for
your own software.
Basic SQL Statements: DDL and DML
– DDL- ALTER TABLE Statement
The ALTER TABLE statement may be used as you have seen to
specify primary and foreign key constraints, as well as to make
other modifications to the table structure. Key constraints may also
be specified in the CREATE TABLE statement.
ALTER TABLE <table name>
ADD CONSTRAINT <constraint name>
PRIMARY KEY (<attribute list>);
You get to specify the constraint name. Get used to following a
convention of tablename_pk (for example, Customers_pk), so you
can remember what you did later.
The attribute list contains the one or more attributes that form this
PK; if more than one, the names are separated by commas.
Basic SQL Statements: DDL and DML
– DDL- Foreign Key Constraint
The FOREIGN KEY CONSTRAINT is a bit more complicated, since
we have to specify both the FK attributes in this (child) table, and
the PK attributes that they link to in the parent table.
ALTER TABLE <table name>
ADD CONSTRAINT <constraint name>
FOREIGN KEY (<attribute list>)
REFERENCES <parent table name> (<attribute list>);
Name the constraint in the form childtable_parenttable_fk (for
example, Orders_Customers_fk). If there is more than one attribute
in the FK, all of them must be included (with commas between) in
both the FK attribute list and the REFERENCES (parent table)
attribute list.
You need a separate foreign key definition for each relationship in
which this table is the child.
Basic SQL Statements: DDL and DML
– DDL- DROP statement
If you totally mess things up and want to start over, you can always
get rid of any object you’ve created with a drop statement. The
syntax is different for tables and constraints.
DROP TABLE <table name>;
310
Transactions - Concurrency
The goal in a ‘concurrent’ DBMS is to allow multiple
users to access the database simultaneously without
interfering with each other.
A problem with multiple users using the DBMS is that it
may be possible for two users to try and change data in
the database simultaneously. If this type of action is not
carefully controlled, inconsistencies are possible.
To control data access, we first need a concept to allow
us to encapsulate database accesses. Such
encapsulation is called a ‘Transaction’.
Transactions - What is a Transaction?
315
Transactions - Reasons for Abort
System crash
Transaction aborted by system
Execution cannot be made atomic (a site is down)
Execution did not maintain database consistency
(integrity constraint is violated)
Execution was not isolated
Resources not available (deadlock)
Transaction requests to roll back
316
Transactions – Problem 1
Ex. Reserving a seat for a flight --
If concurrent access is allowed to data in a DBMS, two users may try to book the same seat simultaneously
Agent 1 finds
time seat 35G
empty Agent 2 finds
seat 35G empty
Agent 1 sets
seat 35G occupied
Agent 2 sets
seat 35G
occupied
Transactions - Schedules
A transaction schedule is a tabular representation of how a set of transactions were executed
over time. This is useful when examining problem scenarios. Within the diagrams various
nomenclatures are used:
READ(a) - This is a read action on an attribute or data item called ‘a’.
WRITE(a) - This is a write action on an attribute or data item called ‘a’.
WRITE(a[x]) - This is a write action on an attribute or data item called ‘a’, where the value ‘x’ is written into
‘a’.
tn (e.g. t1,t2,t10) - This indicates the time at which something occurred. The units are not important, but tn
always occurs before tn+1.
Transactions – Problem 2
Problems can occur when concurrent transactions execute in an
uncontrolled manner.
Examples of one problem.
A original equals 100, after executing T1 and T2, A is supposed to be
100+10-8=102 but A is 92
Read(A) 100
A=A-8 100
Write(A) 110
Write(A) 92
Transactions – Problem 3
Consider transaction A, which loads in a bank account balance X (initially $20) and
adds $10 to it. Such a schedule would look like this:
Time Transaction
t1 TOTAL:=READ(X)
t2 TOTAL:=TOTAL+10
t3 WRITE(X[30])
Transactions – Problem 3
Now consider that, at the same time as trans A runs, trans B runs.
Transaction B gives all accounts a 10% increase. Will X be 32 or 33?
t2 TOTAL:=READ(X)
t3 TOTAL:=TOTAL+10
t4 WRITE(X[30])
t5 BONUS:=BALANCE*110%
t6 WRITE(X[22])
Woops… X is 22! Depending on the interleaving, X can also be 32, 33,
or 30.
Transactions –
Lost Update Scenario
Time Transaction A Transaction B
t1 READ(R)
t2 READ(R)
t3 WRITE(R)
t4 WRITE(R)
t1 WRITE(R)
t2 READ(R)
t3 ABORT
t3 40 50 30 READ(Z)
t4 40 50 20 WRITE(Z[20])
t5 40 50 20 READ(X)
t6 50 50 20 WRITE(X[50])
t7 50 50 20 COMMIT
t8 50 50 20 SUM+=READ(Z) 110
327
Transactions – Atomicity (cont.)
Partial effects of a transaction must be undone
when
User explicitly aborts the transaction using
ROLLBACK
Application asks for user confirmation in the last step and
issues COMMIT or ROLLBACK depending on the response
An error, exception, or constraint violation occurs
during a transaction
The DBMS crashes before a transaction commits
How is atomicity achieved?
Logging
Transactions - Isolation
Serial Execution: transactions execute in sequence
Each one starts after the previous one completes.
Execution of one transaction is not affected by the
operations of another since they do not overlap in time
The execution of each transaction is isolated from all
others.
If the initial database state and all transactions are
consistent, then the final database state will be
consistent and will accurately reflect the real-world
state, but
Serial execution is inadequate from a performance
perspective
329
Transactions – Isolation (cont.)
Concurrent execution offers performance benefits:
A computer system has multiple resources capable of
executing independently (e.g., cpu’s, I/O devices), but
A transaction typically uses only one resource at a time
Hence, only concurrently executing transactions can make
effective use of the system
Concurrently executing transactions yield interleaved
schedules
330
Transactions – Isolation (cont.)
Transactions must appear to be executed in a serial
schedule (with no interleaving operations)
For performance, DBMS executes transactions using a
serializable schedule
In this schedule, operations from different transactions can
interleave and execute concurrently
But the schedule is guaranteed to produce the same effects as a
serial schedule
How is isolation achieved?
Locking, multi-version concurrency control (method commonly
used to provide concurrent access to the database)
Transactions – Database Consistency
Enterprise (Business) Rules limit the occurrence of
certain real-world events
Student cannot register for a course if the current
number of registrants equals the maximum allowed
Correspondingly, allowable database states are
restricted
cur_reg <= max_reg
These limitations are called (static) integrity
constraints: assertions that must be satisfied by all
database states
332
Transactions – Transaction Consistency
A consistent database state does not necessarily model
the actual state of the enterprise
A deposit transaction that increments the balance by the
wrong amount maintains the integrity constraint balance 0,
but does not maintain the relation between the enterprise
and database states
A consistent transaction maintains database consistency
and the correspondence between the database state and
the enterprise state (implements its specification)
Specification of deposit transaction includes
balance = balance + amt_deposit ,
(balance is the next value of balance)
333
Transactions - Consistency
Consistency of the database is guaranteed by
constraints and triggers declared in the
database and/or transactions themselves
When inconsistency arises, abort the transaction or fix
the inconsistency within the transaction
Transactions - Durability
335
Transactions – Durability (cont.)
Effects of committed transactions must survive
DBMS crashes
How is durability achieved?
DBMS manipulates data in memory; forcing all
changes to disk at the end of every transaction is very
expensive
Logging
Transactions – Implementing Durability
Database stored redundantly on mass storage
devices to protect against media failure
Architecture of mass storage devices affects type
of media failures that can be tolerated
Related to Availability: extent to which a (possibly
distributed) system can provide service despite
failure
Non-stop DBMS (mirrored disks)
Recovery based DBMS (log)
337
Transactions – Isolation Levels
Strongest isolation level: SERIALIZABLE
Complete isolation
SQL default
Weaker isolation levels: REPEATABLE READ,
READ COMMITTED, READ UNCOMMITTED
Increase performance by eliminating overhead and
allowing higher degrees of concurrency
Trade-off: sometimes you get the wrong answer
Transactions – Example Schema
CREATE TABLE Account
(accno INTEGER NOT NULL PRIMARY KEY,
name CHAR(30) NOT NULL,
balance FLOAT NOT NULL
CHECK(balance >= 0));
Transactions – Read Uncommitted
Can read dirty data
A data item is dirty if it is written by an uncommitted
transaction
Problem: What if the transaction that wrote the dirty data
eventually aborts?
Example: wrong average
-- T1: -- T2:
UPDATE Account
SET balance = balance - 200
WHERE accno = 142857; SELECT AVG(balance)
FROM Account;
ROLLBACK;
COMMIT;
Transactions – Read Committed
No dirty reads, but non-repeatable reads possible
Reading the same data item twice can produce different results
Example: different averages
-- T1: -- T2:
SELECT AVG(balance)
FROM Account;
UPDATE Account
SET balance = balance - 200
WHERE accno = 142857;
COMMIT;
SELECT AVG(balance)
FROM Account;
COMMIT;
Transactions – Repeatable Read
Reads are repeatable, but may see phantoms
A phantom read occurs when, in the course of a transaction, two identical
queries are executed, and the collection of rows returned by the second
query is different from the first.
Example: different average (still!)
-- T1: -- T2:
SELECT AVG(balance)
FROM Account;
INSERT INTO Account
VALUES(428571, 1000);
COMMIT;
SELECT AVG(balance)
FROM Account;
COMMIT;
Transactions - Serializable
None of these problems can happen
Transactions – Summary of SQL
Isolation Levels
Isolation Level/Anomaly Dirty Reads Non-Repeatable Reads Phantoms
JDBC Driver
JDBC Driver Installation
Must download the driver, then add the .jar file to
your $CLASSPATH or NetBeans project
To set up your classpath on Windows
Control panel
Search for Environment Variables
Add the jar file to your CLASSPATH variable
JDBC Driver Management
All drivers are managed by the DriverManager
class
Example - loading an Oracle JDBC driver:
In the Java code:
Class.forName(“oracle.jdbc.driver.OracleDriver”)
Driver class names:
Oracle: oracle.jdbc.driver.OracleDriver
MySQL: com.mysql.jdbc.Driver
MS SQL Server:
com.microsoft.jdbc.sqlserver.SQLServerDriver
Establishing a Connection
• Create a Connection object
• Use the DriverManager to grab a connection
with the getConnection method
• Necessary to follow exact connection syntax
• Problem 1: the parameter syntax for
getConnection varies between JDBC drivers
• Problem 2: one driver can have several
different legal syntaxes
Establishing a Connection (cont.)
Oracle Example
• Connection con =
DriverManager.getConnection(string,
“username", “password");
• what to supply for string ?
• “jdbc:oracle:thin:@augur.seas.gwu.edu:1521:orcl10g2”
ResultSet rs=pstmt.executeQuery();
// rs is now a cursor
While (rs.next()) {
// process the data
}
ResultSets (cont.)
A ResultSet is a very powerful cursor:
previous(): moves one row back
absolute(int num): moves to the row with the
specified number
relative (int num): moves forward or backward
first() and last()
Retrieving the results
The ResultSet interface sports getDate, getInt,
getString, and so on to convert from the
database representation of the data to a Java
representation of the data.
Matching Java-SQL Data Types
SQL Type Java class ResultSet get method
BIT Boolean getBoolean()
CHAR String getString()
VARCHAR String getString()
DOUBLE Double getDouble()
FLOAT Double getDouble()
INTEGER Integer getInt()
REAL Double getFloat()
DATE java.sql.Date getDate()
TIME java.sql.Time getTime()
TIMESTAMP java.sql.TimeStamp getTimestamp()
Data About the Data
ResultSetMetaData rsmd = rs.getMetaData();
Use the import: java.sql.* for the ResultSetMetaData
interface.
For more information, see the documentation at:
https://docs.oracle.com/javase/8/docs/api/java/sql/Res
ultSetMetaData.html
.
What I’m going to cover is just a fraction of what this
class can do.
Bear in mind that these methods are all operating on
the entire result set, not any given row in the result
set.
Useful methods
int – getColumnCount() returns the # of columns in the
result set. Good first call to make when looping through
the meta data for a given result set.
These utilities all start at 1 for the first column. Do not let
that confuse you with starting at 0 for arrays.
int – getColumnDisplaySize (int) – returns the
designated column’s maximum width.
String – getColumnName (int)
int getColumnType (int) – returns an integer
corresponding to the column type. See java.sql.Types
for the list of valid types that can be returned.
String getColumnTypeName (int) – returns name of the
column type.
You Also Might be Interested
In:
int isNullable (int) – you receive an integer
{columnNoNulls, columnNullable,
columnNullableUnknown} in return
String getTableName (int) – returns the table
name from the specified column index
boolean isReadOnly (int)
boolean isWriteable (int)
JDBC: Exceptions and Warnings
Most of java.sql can throw and SQLException if
an error occurs (use try/catch blocks to find
connection problems)
SQLWarning is a subclass of SQLException; not
as severe (they are not thrown and their
existence has to be explicitly tested)
JDBC MySQL example:
Excellent instructions can be found at:
http://www.tutorialspoint.com/jdbc/
Sample Code can be found at:
http://www.csulb.edu/~mopkins/cecs323/FirstEx
ample.java