database and java database connectivity
TRANSCRIPT
Database andJava Database Connectivity
Gary 2013/06/13
Outline
• Database
• Distributed database
• Relational Database
• JDBC
• JDBC Implementation
• Future Work
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).
Database
• Database system consists of
– User
– Data
– Hardware
• Where data resides
– Software
• Database management system(DBMS)
• Controls storage and retrieval
Database
• The advantage of database
– Reduce redundancy
– Avoid inconsistency
– Share data
– Enforce standards
– Security restrictions
– Data integrity
– Balance conflicting requirement
Database
• Non-database systems
– Each application has its own files
• Redundant
• Lack centralized control
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
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)
Database
• Embedding SQL statements in a host language
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.
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)
Distributed database
• Cons
– Complexity
– Economics
– Security
– Difficult to maintain integrity
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.
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
Relational Database
• Example
Relational Database
• Operations
– Projection
• Taking a subset of a table
– Join
• Combining tables to form a larger on (by foreign key)
Relational Database
• Example of projection
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
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.
JDBC
• Architecture
JDBC
• There are four type of JDBC
– JDBC bridge
– Native-API bridge
– JDBC-Net
– Native-Protocol
JDBC
• JDBC bridge
• Ex: Microsoft Access Database
Java APP
JDBC API
JDBC-ODBCBridge
ODBC API
ODBC Layer
DB
JDBC
• Native-API bridge
Java APP
JDBC API
Native-APIBridge
Native API
DB
JDBC
• JDBC-Net
Java APP
JDBC API
JDBC-NetMiddleware
or Server
DB
JDBC
• Native-Protocol
• Ex: MySQL
Java APP
JDBC API
Native-Protocol
DB
JDBC Implementation
• Establish database
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;
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";
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 );
JDBC Implementation
• Other JDBC driver usage
– MySQL
– Oracle
– Sybase
– Postgresql
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 );
JDBC Implementation
– Statement closed when done
– Unlock database
58 statement.close();
Conclusion
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.