m08640010120114036m0864pok-p11 database access

Upload: singgihpratamaputra

Post on 02-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    1/18

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    2/18

    Database AccessSession 11

    Course : M0864 - Programming IYear : September 2011

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    3/18

    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 .

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    4/18

    Bina Nusantara

    Lecture Outline Relational Database Systems Introduction to SQL Database Table

    JDBC

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    5/18

    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 andmanage data in a database and program application that providesdata and allow user to interact with the database system

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    6/18

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    7/18

    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)

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    8/18

    Relational Database Systems

    Primary Key : identity of a data, unique, cannot be the same in 1 table Fore ign 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

    Pr imary Key

    Foreign K ey

    Column/ Field / Attribute

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    9/18

    Introduction to SQL Structured Query Language (SQL) is a language to

    define table and integrity and for data access and datamanipulation.

    SQL can be used on MySQL, Oracle, Sybase, IBM DB2,IBM Informix, Borland Interbase, MS Access, etc

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    10/18

    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 whichattribute that will act as primary key.

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    11/18

    Database TableTo 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;

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    12/18

    Database Table To get the information content of a table :

    select * from table_name where [cond i t i on ]

    - 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 tablename- 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

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    13/18

    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, thatexecuted SQL statement, get the result, display data in the user-friendly interface and provide more changes behind the database.

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    14/18

    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

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    15/18

    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 fromwww.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

    http://www.cs.armstrong.edu.liang/intro7e/book/lib/mysqljdbc.jarhttp://www.cs.armstrong.edu.liang/intro7e/book/lib/0jdbc14.jarhttp://www.cs.armstrong.edu.liang/intro7e/book/lib/0jdbc14.jarhttp://www.cs.armstrong.edu.liang/intro7e/book/lib/mysqljdbc.jar
  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    16/18

    JDBC2. Connection

    - Connect to database using interface Connection .- Syntax : Connection con = DriverManager.getConnection( URL_Pattern );

    - Sample : database Access : data.mdbConnection 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

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    17/18

    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 executedtable.

    Sample : get field binusianId on table EnrollmentString binusian_Id;If(rs.next())

    binusian_Id = rs.getString(binusianId); atau

    binusian_Id = rs.getString(0);

  • 8/11/2019 M08640010120114036M0864POK-p11 Database Access

    18/18

    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

    http://download.oracle.com/javase/tutorial/jdbc/basics/jdbcswing.htmlhttp://oreilly.com/catalog/javadata/chapter/ch04.htmlhttp://www.javamex.com/tutorials/database/http://download.oracle.com/javase/tutorial/jdbc/overview/database.htmlhttp://download.oracle.com/javase/tutorial/jdbc/overview/database.htmlhttp://download.oracle.com/javase/tutorial/jdbc/overview/database.htmlhttp://www.javamex.com/tutorials/database/http://www.javamex.com/tutorials/database/http://oreilly.com/catalog/javadata/chapter/ch04.htmlhttp://download.oracle.com/javase/tutorial/jdbc/basics/jdbcswing.htmlhttp://download.oracle.com/javase/tutorial/jdbc/basics/jdbcswing.html