Sie sind auf Seite 1von 16

Relational Databases

The Information System


Information is a commodity that, when acquired, results in knowledge that can be used to benefit those in the know. Data is not information Data must be turned into information A good information system will turn data into information

Relational Databases
A database contains
The data Metadata about the schema Metadata about the indexes

The data can be accessed directly using SQL (Structured Query Language) this is the most important programming language in business applications (sorry, Java) You can communicate with a database using TCP/IP network packets.

Parts of a database
Tables: Hold the data Queries: Present data from one or many tables that are joined together Stored Procedures: Compiled SQL statements residing on the database. They may have input and/or output parameters Triggers: An event listener that is triggered when data changes in a given table

SQL Statements
SELECT column1, column2, column3 FROM tableName WHERE column3 = value AND column4=value; UPDATE tableNAME SET column1=value1, column2=value2 where keyColumn=keyValue; DELETE FROM tableName where column1=value; INSERT INTO tableName(col1, col2) VALUES(value1, value2);

Primary key
Used to uniquely identify a record Must exist for every record in every table Can be a business key
A single unique field, or Multiple fields that, when combined, are unique

Can be an auto-generated value

Foreign Keys
A foreign key is a relationship to another table (object) Can be represented object-wise by an object that HAS-An array of other objects Can be
One to Many Many to Many (requires an intermediary table) One to one (consider if you really need it)

SQL Dialect
Depending on the database product, the SQL used is slightly different It should be mostly the same across all for simple SQL commands; however, each DBMS (database management system) provides its own set of goodies that arent standard across all products

SQL To create a table in MS Access


CREATE table person (
personId int DEFAULT AUTOINCREMENT, firstName varchar(30) NOT NULL, lastName varchar(30) NOT NULL )

SQL To create a table in mySQL


CREATE DATABASE myDatabase; USE myDatabase; CREATE table person (
personId int PRIMARY KEY AUTO_INCREMENT, firstName varchar(30) NOT NULL, lastName varchar(30) NOT NULL )

JDBC Java Database Connection


Based on ODBC (open database connection), which was introduced by Microsoft in the early 1990s Uses drivers to provide an interface so that you can write the same Java code against many databases

JDBC driver types


Type I
Piggy-back on ODBC Two layers (JDBC + ODBC) Simple, but not as efficient

Type II
Uses additional drivers at the network connectivity layer Written in native code Must be installed on every machine Example: IBM DB2 Connect

JDBC Driver Types


Type III
100% Java code on the client side Must still install software drivers on the server

Type IV
Written in Java and contains networking code in its classes No additional installation on the client or server, connects to services the DB already provides

Plug-and-play
Once you install a driver, the language you write (both Java and SQL) is the same so you can swap out connection types by changing only your connection code If you switch to use a different database (SQL Server to mySQL, for example), your Java code does not change, but your SQL might, depending on how close the dialect is

Classpath
If your type IV driver is in a jar file, dont forget the conventions: To compile javac ConnectToMySQL.java -classpath ".\mysql-connector-java-3.1.12bin.jar To run: D:\docs\ITT\Java II\Week6\examples>java -classpath ".\mysql-connector-java-3.1.12bin.jar;." ConnectToMySQL

Other resources
http://www.developer.com/java/data/article.php/3417381

http://www.kitebird.com/articles/jdbc.html

Das könnte Ihnen auch gefallen