Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 15 Using a Database.

Similar presentations


Presentation on theme: "Chapter 15 Using a Database."— Presentation transcript:

1 Chapter 15 Using a Database

2 Database tables Relational database keep data in tables
do not include redundant information Key identifies the data in a row Sales database example has five tables Customer, Salesperson, Item, Orders, and OrderItem

3 CustomerID CustomerName Address BalanceDue
1234 Fred Flynn 22 First St. 5678 Darnell Davis 33 Second St. 130.95 4321 Marla Martinez 44 Third St. 8765 Carla Kahn 55 Fourth St. Figure The Customer table

4 SalespersonID SalespersonName Address
12 Peter Patterson 66 Fifth St. 98 Donna Dubarian 77 Sixth St. Figure The Salesperson table

5 ItemNumber Description Quantity
222222 radio 32 333333 television 14 444444 computer 9 Figure The Item table

6 OrderNumber CustomerID SalespersonID OrderDate
1 1234 12 4/3/99 2 5678 3/22/99 3 8765 98 2/19/99 4 4/5/99 5 2/28/99 Figure The Orders table

7 OrderNumber ItemNumber Quantity UnitPrice
1 222222 4 27.00 333333 2 210.50 444444 569.00 230.95 3 5 725.00 Figure The OrderItem table

8 Type Standard SQL Description
CHAR(N) Fixed size string of length N VARCHAR(N) Variable size string up to length N INTEGER bit integer DATE Year, month, and day DECIMAL Used for dollars and cents Figure SQL data types

9 CREATE TABLE tablename
(fieldname1 TYPE1, fieldname2 TYPE2, ... , fieldnameN TYPEn) INSERT INTO tablename VALUES (field1value,field2value, ..., fieldNvalue) DELETE FROM tablename WHERE fieldname1 = value1 ... AND fieldnameN = valueN UPDATE tablename SET fieldnameToSet = newValue WHERE fieldname1ToCheck = value1ToCheck SELECT fieldname1, ..., fieldnameN FROM table1, ..., tableM WHERE condition1 ... AND conditionN Figure Some patterns for SQL statements

10 Client1 Database Server Client 2 Figure Client-server database access

11 Client1 Application Server Database Server Client2 Figure A three-tiered system architecture

12 Database Drivers JDBC statements are database independent
A driver translates from JDBC to vendor’s database commands Database vendors provide JDBC drivers e.g Oracle, mySQL Microsoft Access has an ODBC driver Use JdbcOdbc bridge to convert from JDBC to ODBC driver Must register ODBC data source

13 Connecting to Microsoft Access
String connect = "Provider=Microsoft.JET.OLEDB.4.0;“ source= c:\booksharp\gittleman\ch15\Sales.mdb"; OleDbConnection con = new OleDbConnection(connect); con.Open(); Console.WriteLine ("Made the connection to the Sales database"); con.Close();

14 Creating a database Do not need C#, can do it in Access
OleDbCommand cmd = con.CreateCommand(); cmd.CommandText = “SQL here”; cmd.ExecuteNonQuery(); SQL CREATE to create a new table SQL INSERT to insert data

15 Retrieving Information
cmd.CommandText = "SELECT CustomerName, Address " + "FROM Customer ORDER BY CustomerName"; reader = cmd.ExecuteReader(); while(reader.Read()) Console.WriteLine("{0}\t{1}", reader.GetString(0), reader.GetString(1)); reader.Close();

16 Java method SQL type GetInt INTEGER GetString VARCHAR GetDecimal DECIMAL GetDate DATE Figure C# methods for SQL types

17 Customer ordering computers
SELECT DISTINCT CustomerName FROM Customer, Item, Orders, OrderItem WHERE Customer.CustomerID = Orders.CustomerID AND Orders.OrderNumber = OrderItem.OrderNumber AND OrderItem.ItemNumber = Item.ItemNumber AND Description = 'computer'

18 Database Info The GetOleDbSchemaTable method returns information about the data set. This method has two parameters a Guid and an Object[] The Guid is a globally unique 128-bit identifier that is unique across all computers and networks. We use OleDbSchemaGuid values. The two we illustrate are Tables and Columns. Using Tables will return a table that describes each table in the data set. Using Columns will return a table that describes each column.

19 DataSet Create a DataSet and process the data offline
Use an OleDbDataAdapter to fill the DataSet Get the DataTable objects

20 Stored Procedures Compile once, run many times with different parameters cmd.CommandText = "SELECT Quantity FROM Item " + "WHERE Description = ?"; OleDbParameter param = new OleDbParameter(); cmd.Parameters.Add(param); param.Value = "radio"; OleDbDataReader reader = cmd.ExecuteReader(); while(reader.Read()) Console.WriteLine("{0}", reader.GetInt32(0)); reader.Close();

21 Transactions All parts must succeed for transaction to succeed
OleDbTransaction trans = con.BeginTransaction(); cmd.Transaction = trans; trans.Commit(); // if transaction succeeds trans.Rollback(); // undoes changes from start // or last Commit


Download ppt "Chapter 15 Using a Database."

Similar presentations


Ads by Google