Database and Java Database Connectivity

34
Database and Java Database Connectivity Gary 2013/06/13

Transcript of Database and Java Database Connectivity

Page 1: Database and Java Database Connectivity

Database andJava Database Connectivity

Gary 2013/06/13

Page 2: Database and Java Database Connectivity

Outline

• Database

• Distributed database

• Relational Database

• JDBC

• JDBC Implementation

• Future Work

Page 3: Database and Java Database Connectivity

Database

• What is database?

– A database is an organized collection of data. The data is typically organized to model relevant aspects of reality (for example, the availability of rooms in hotels), in a way that supports processes requiring this information (for example, finding a hotel with vacancies).

Page 4: Database and Java Database Connectivity

Database

• Database system consists of

– User

– Data

– Hardware

• Where data resides

– Software

• Database management system(DBMS)

• Controls storage and retrieval

Page 5: Database and Java Database Connectivity

Database

• The advantage of database

– Reduce redundancy

– Avoid inconsistency

– Share data

– Enforce standards

– Security restrictions

– Data integrity

– Balance conflicting requirement

Page 6: Database and Java Database Connectivity

Database

• Non-database systems

– Each application has its own files

• Redundant

• Lack centralized control

Page 7: Database and Java Database Connectivity

Database

• Data independence

– Applications not dependent on how data stored or accessed

– Applications can have different views of data

– Change storage/retrieval strategy without changing applications

• Data dependency

– Change in storage or retrieval technique forces program change

Page 8: Database and Java Database Connectivity

Database

• Database language

– Host languages

• Used to access database

• Can use high-level languages– Java, C, C++, Visual Basic, COBOL, PL/I, Pascal

– Database sublanguage(DSL)

• Specifics of database objects and operations

• Combination of – Data definition language(DDL)

– Data manipulation language(DML)

Page 9: Database and Java Database Connectivity

Database

• Embedding SQL statements in a host language

Page 10: Database and Java Database Connectivity

Distributed database

• A distributed database is a database in which storage devices are not all attached to a common processing unit such as the CPU, controlled by a distributed database management system. It may be stored in multiple computers, located in the same physical location; or may be dispersed over a network of interconnected computers.

Page 11: Database and Java Database Connectivity

Distributed database

• Pros– Local autonomy or site autonomy

• a department can control the data about them (as they are the ones familiar with it)

– Protection of valuable data• if there were ever a catastrophic event such as a fire, all of the

data would not be in one place, but distributed in multiple locations

– Modularity• systems can be modified, added and removed from the distributed

database without affecting other modules

– Continuous operation• even if some nodes go offline (depending on design, like backup)

Page 12: Database and Java Database Connectivity

Distributed database

• Cons

– Complexity

– Economics

– Security

– Difficult to maintain integrity

Page 13: Database and Java Database Connectivity

Relational Database

• A relational database is a database that has a collection of tables of data items, all of which is formally described and organized according to the relational model.

Page 14: Database and Java Database Connectivity

Relational Database

• Composed of tables

– Rows called records(tuples)

– Columns are fields(attributes)

• First field usually primary key

– Unique for each record

– Primary key can be more than one field

– Cannot allow null values

Page 15: Database and Java Database Connectivity

Relational Database

• Example

Page 16: Database and Java Database Connectivity

Relational Database

• Operations

– Projection

• Taking a subset of a table

– Join

• Combining tables to form a larger on (by foreign key)

Page 17: Database and Java Database Connectivity

Relational Database

• Example of projection

Page 18: Database and Java Database Connectivity

Relational Database

• Advantages of relational database

– Tables easy to use, understand, and implement

– Projection and join operations easy to implement

– Easy to modify

– Great clarity and visibility

Page 19: Database and Java Database Connectivity

JDBC

• JDBC is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the JVM host environment.

Page 20: Database and Java Database Connectivity

JDBC

• Architecture

Page 21: Database and Java Database Connectivity

JDBC

• There are four type of JDBC

– JDBC bridge

– Native-API bridge

– JDBC-Net

– Native-Protocol

Page 22: Database and Java Database Connectivity

JDBC

• JDBC bridge

• Ex: Microsoft Access Database

Java APP

JDBC API

JDBC-ODBCBridge

ODBC API

ODBC Layer

DB

Page 23: Database and Java Database Connectivity

JDBC

• Native-API bridge

Java APP

JDBC API

Native-APIBridge

Native API

DB

Page 24: Database and Java Database Connectivity

JDBC

• JDBC-Net

Java APP

JDBC API

JDBC-NetMiddleware

or Server

DB

Page 25: Database and Java Database Connectivity

JDBC

• Native-Protocol

• Ex: MySQL

Java APP

JDBC API

Native-Protocol

DB

Page 26: Database and Java Database Connectivity

JDBC Implementation

• Establish database

Page 27: Database and Java Database Connectivity

JDBC Implementation

– Has classes and Interfaces for using relational databases

– Implements interface Connection

– Manages connection between database and program

4 import java.sql.*;

11 private Connection connection;

Page 28: Database and Java Database Connectivity

JDBC Implementation

– Database URL(location)username to log in password

– URL

• Protocol for communcation(jdbc)

• Subprotocol(odbc)

• Database name(Books)

19 String url = "jdbc:odbc:Books";

20 String username = "anonymous";

21 String password = "guest";

Page 29: Database and Java Database Connectivity

JDBC Implementation

– Static method forName ()

– Static method getconnection

• Attempt connection to database

• Name and password required

25 Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

27 connection = DriverManager.getConnection(

28 url, username, password );

Page 30: Database and Java Database Connectivity

JDBC Implementation

• Other JDBC driver usage

– MySQL

– Oracle

– Sybase

– Postgresql

Page 31: Database and Java Database Connectivity

JDBC Implementation

– The statement object• Submits query to database

– Returns resultset object containing results

– Statement.executeUpdate• Change data in database

– Statement.executeQuery• Only query

49 Statement statement;

55 statement = connection.createStatement();

53 String query = "SELECT * FROM Authors";

50 ResultSet resultSet;

56 resultSet = statement.executeQuery( query );

Page 32: Database and Java Database Connectivity

JDBC Implementation

– Statement closed when done

– Unlock database

58 statement.close();

Page 33: Database and Java Database Connectivity

Conclusion

Page 34: Database and Java Database Connectivity

Future Wrok

• Servelets

– A servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web servers, so they can be thought of as Java Applets that run on servers instead of in web browsers.