Você está na página 1de 18

Database Access

Session 11
Course : M0864 - Programming I
Year : September 2011
Bina Nusantara
Learning Outcomes
At the end of this session, the students will be able to :
Apply the concept of database access on GUI
Programming in Java.
Bina Nusantara
Lecture Outline
Relational Database Systems
Introduction to SQL
Database Table
JDBC



Relational Database Systems
Database System is not only for data store, but also provides data
access, update, manipulation and analysis facilities as well.

A database system contains of database, application to store and
manage data in a database and program application that provides
data and allow user to interact with the database system

Relational Database Systems



Database is a storage of data in the form of information
Application Users
Application Programs
Database Management System (DBMS)
Database
System Users
Relational Database Systems
Database system vendors :
Microsoft
MySql
Oracle
IBM
Sybase
3 keys component in relational database systems :
Structure : represent by the data on table
Integrity : key on data (primary key and foreign key)
Language : for data access and manipulation (SQL)
Relational Database Systems
Primary Key : identity of a data, unique, cannot be the same in 1 table
Foreign Key : primary key that refers to other table
binusianId courseId dataRegisted grade
1100755660 CS001 8/14/2009 A
1100755660 CS002 8/14/2009 B
1100755660 CS003 8/14/2009 C
courseId subjectId courseNumber title SKS
CS001 CSCI 1320 Introduction to Java I 4
CS002 CSCI 1321 Introduction to Java II 4
CS003 CSCI 3741 Database System 6
Enrollment Table
Course Table
Primary Key
Foreign Key
Column/ Field / Attribute
Introduction to SQL
Structured Query Language (SQL) is a language to
define table and integrity and for data access and data
manipulation.

SQL can be used on MySQL, Oracle, Sybase, IBM DB2,
IBM Informix, Borland Interbase, MS Access, etc
Database Table
Table are essentials objects in a database.

To create a table, use the statement create table that
contains of table name, attributes, andtype also which
attribute that will act as primary key.
Database Table
To create table Course using SQL :
create table Enrollment (
binusianId char(10) not null,
courseId char(5) not null,
dataRegisted date,
grade char(1),
primary key (binusianId)
);
binusianId courseId dataRegisted grade
1100755660 CS001 8/14/2009 A
1100755660 CS002 8/14/2009 B
1100755660 CS003 8/14/2009 C
To delete table Course using SQL :
drop table Enrollment;
Database Table
To get the information content of a table :
select * from table_name where [condition]

- select : to define the column/field that will be displayed
- * : to display all columns/fields of the tabel
- from : from which table data will be gotten, followed by table
name
- where : condition for data/row that will be displayed.

sample :
select courseId from enrollment where binusianId = 1100755660 and grade = A

Bina Nusantara
courseId
CS001
CS002
CS003
JDBC
Java Database Connection (JDBC) :
provide interface for database access and manipulation.

Java API to develop Java database application.

JDBC API, writing application on Java programming language, that
executed SQL statement, get the result, display data in the user-
friendly interface and provide more changes behind the database.

JDBC
Relationship Java, JDBC, database
Java Programs
JDBC API
MySQL JDBC Driver Oracle JDBC Driver JDBC-ODBC JDBC Driver
Local or Remote MySQL DB Local or Remote MySQL DB Local or Remote MySQL DB
Microsoft ODBC JDBC Driver
JDBC
4 keys in the java database application development:
1. Driver
- Application load database
- Interface dari java.sql.Driver
- Syntax : Class.forName(JDBC_Driver_Class);




- For JDBC-ODBC driver Access is in the JDK
- For MySQL JDBC (mysqljdbc.jar) download from
www.cs.armstrong.edu.liang/intro7e/book/lib/mysqljdbc.jar
- For Oracle JDBC (ojdbc14.jar) download from
www.cs.armstrong.edu.liang/intro7e/book/lib/0jdbc14.jar
Bina Nusantara
Database JDBC_Driver_Class Source
Access "sun.jdbc.odbc.JdbcOdbcDriver" Already in JDK
MySql "com.mysql.jdbc.Driver" Companion Web Site
Oracle "oracle.jdbc.driver.OracleDriver" Companion Web Site
JDBC
2. Connection
- Connect to database using interface Connection.
- Syntax : Connection con = DriverManager.getConnection(URL_Pattern);





- Sample : database Access : data.mdb
Connection con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=data.mdb);
Closing connection : con.close();

3. Statement
Make SQL statement
Syntax and sample :
Statement st = con.createStatement();
4. ResultSet
Execution of SQL statements
Syntax : ResulSet rs = st.executeQuery(String SQL);
Sample : ResulSet rs = st.executeQuery(Select * from Enrollment);

Database URL_Pattern
Access jdbc:odbc:datasource
MySql jdbc:mysql://hostname/dbname
Oracle jdbc:oracle:thin:@hostname:port#oracleDBSID
To get execution result, use the method on ResultSet :
rs.next() : pointer record moves to the next row, return boolean.
rs.previous() : pointer record moves to previous row, return boolean.
rs.getString(Field_label) : get 1 data from 1 field with string type.
rs.getDouble(Field_label) : get 1 data from 1 field with double type.
rs.getInt(Field_label) : get 1 data from 1 field with integer type.
rs.getDate(Field_label) : get 1 data from 1 field with date type.

For field_label is the fields name or fields index on the executed
table.
Sample : get field binusianId on table Enrollment
String binusian_Id;
If(rs.next())
binusian_Id = rs.getString(binusianId);
atau
binusian_Id = rs.getString(0);
Bina Nusantara
Reference
Introduction to java programming comprehensive version, Part I (Chapter 2-6 ), Part II
(Chapter 7)

http://download.oracle.com/javase/tutorial/jdbc/basics/jdbcswing.html

http://oreilly.com/catalog/javadata/chapter/ch04.html

http://www.javamex.com/tutorials/database/

http://download.oracle.com/javase/tutorial/jdbc/overview/database.html

Você também pode gostar