LSINF1124 Projet de programmation



Similar documents
Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Using Netbeans and the Derby Database for Projects Contents

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

CS/CE 2336 Computer Science II

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

Configuring Apache Derby for Performance and Durability Olav Sandstå

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Package sjdbc. R topics documented: February 20, 2015

The JAVA Way: JDBC and SQLJ

A Brief Introduction to MySQL

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

CHAPTER 3. Relational Database Management System: Oracle. 3.1 COMPANY Database

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

SQL. Short introduction

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

A basic create statement for a simple student table would look like the following.

How To Create A Table In Sql (Ahem)

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

JDBC (Java / SQL Programming) CS 377: Database Systems

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

Accesssing External Databases From ILE RPG (with help from Java)

1 SQL Data Types and Schemas

Brazil + JDBC Juin 2001, douin@cnam.fr

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

SQL and programming languages

Using Temporary Tables to Improve Performance for SQL Data Services

Performance Tuning for the JDBC TM API

Using DOTS as Apache Derby System Test

Self-test Database application programming with JDBC

Oracle Database 10g Express

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

Security Module: SQL Injection

Linas Virbalas Continuent, Inc.

Database Access Through Java Technologies

Microsoft SQL Server Features that can be used with the IBM i

MYSQL DATABASE ACCESS WITH PHP

UQC103S1 UFCE Systems Development. uqc103s/ufce PHP-mySQL 1

Abstract. Introduction. Web Technology and Thin Clients. What s New in Java Version 1.1

Achieving Database Interoperability Across Data Access APIs through SQL Up-leveling

Chapter 4: SQL. Schema Used in Examples

Database System Concepts

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

Apéndice C: Código Fuente del Programa DBConnection.java

Apache Derby Security. Jean T. Anderson

Java and RDBMS. Married with issues. Database constraints

Introduction Web Portal Main Page Group Management Create group Modify Group Member List... 5

An Introduction to SQL Injection Attacks for Oracle Developers. January 2004 INTEGRIGY. Mission Critical Applications Mission Critical Security

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Applying Java Technologies in the Development of Software Application of the Hospital Information Subsystem

SQL Server An Overview

Financial Data Access with SQL, Excel & VBA

Database Query 1: SQL Basics

SQL 2: GETTING INFORMATION INTO A DATABASE. MIS2502 Data Analytics

Creating Database Tables in Microsoft SQL Server

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL

How I hacked PacketStorm ( )

Using SQL Server Management Studio

Programming Database lectures for mathema

Persistent Stored Modules (Stored Procedures) : PSM

DbSchema Tutorial with Introduction in SQL Databases

Using Apache Derby in the real world

A table is a collection of related data entries and it consists of columns and rows.

Tutorial on Relational Database Design

Application note: Connecting the to a Database

CSC 443 Data Base Management Systems. Basic SQL

DbSchema Tutorial with Introduction in MongoDB

Using JML to protect Java code against SQL injection. Johan Janssen June 26, 2007

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

Getting Started using the SQuirreL SQL Client

Database Administration with MySQL

Database: SQL, MySQL

Lab 2: PostgreSQL Tutorial II: Command Line

CS346: Database Programming.

DBMS SQL JDBC 1. Integrated data

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

EECS 647: Introduction to Database Systems

Java DataBase Connectivity (JDBC)

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

Oracle to MySQL Migration

Advance DBMS. Structured Query Language (SQL)

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Chapter 22 Database: SQL, MySQL,

Seminar Datenbanksysteme

Download: Server-side technologies. WAMP (Windows), MAMP (Mac),

SQL, the underestimated Big Data technology

Risks to Database Activities

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Transcription:

LSINF1124 Projet de programmation Database Programming with Java TM Sébastien Combéfis University of Louvain (UCLouvain) Louvain School of Engineering (EPL) March 1, 2011

Introduction A database is a collection of records, logically related A Database Management System (DBMS) is software that organizes the storage of data A database model is the structure of a database (flat, relational, object,...) A query language used to make queries on a database (SQL, XQuery, Datalog,...) 2

Java DB : Apache Derby Java DB is Sun s supported distribution of the open source Apache Derby 100% Java technology database. It is fully transactional, secure, easy-to-use, standards-based? SQL, JDBC API, and Java EE yet small, only 2.5 MB. A Java RDBMS, that can be embedded in Java programs Included in Java SE (since Java 6) 3

SQL query language SQL (Structured Query Language) used to query database Let s discover SQL using the Apache Derby s ij tool 1 $ java -jar /Users/combefis/javadb 10.5.3.0/lib/derbyrun.jar ij 2 version ij 10.5 3 ij> To get help about allowed commands, type help; To quit ij, type exit; 4

Database The first step is to open a connection to the database Can create a new database with create=true attribute 1 ij> connect jdbc :derby :MyNewDB ;create=true ; 2 ij> show connections ; 3 CONNECTION0 jdbc:derby:mynewdb 4 = connexion en cours 5 ij> disconnect CONNECTION0 ; 6 ij> show connections ; 7 Aucune connexion disponible. 8 ij> exit ; 9 10 $ ls 11 MyNewDB derby.log All the content of the database is stored in the MyNewDB folder 5

Table A database is composed of tables A table is created with the CREATE TABLE SQL command 1 ij> connect jdbc :derby :MyNewDB ; 2 ij> CREATE TABLE students 3 > ( 4 > firstname VARCHAR(50), 5 > lastname VARCHAR(50), 6 > sex CHAR(1) 7 > ) ; 8 0 lignes insérées/mises à jour/supprimées 9 10 ij> DESCRIBE students ; 11 COLUMN_NAME TYPE_NAME DEC& NUM& COLUM& COLUMN_DEF CHAR_OCTE& IS_NULL& 12 13 FIRSTNAME VARCHAR NULL NULL 50 NULL 100 YES 14 LASTNAME VARCHAR NULL NULL 50 NULL 100 YES 15 SEX CHAR NULL NULL 1 NULL 2 YES 16 3 lignes sélectionnées 6

Populate the table Can add record in a table with INSERT INTO SQL command Can get record from a table with SELECT FROM SQL command 1 ij> INSERT INTO students VALUES ( Florence, Turine, F ) ; 2 1 ligne insérée/mise à jour/supprimée 3 4 ij> INSERT INTO students VALUES ( Nicolas, Laurent, M ) ; 5 1 ligne insérée/mise à jour/supprimée 6 7 ij> SELECT * FROM students ; 8 FIRSTNAME LASTNAME SEX 9 10 Florence Turine F 11 Nicolas Laurent M 12 2 lignes sélectionnées 7

Query the table Queries done with SELECT FROM SQL command Can use WHERE clause to specify conditions 1 >ij SELECT firstname, lastname FROM students WHERE sex= F ; 2 FIRSTNAME LASTNAME 3 4 Florence Turine 5 1 ligne sélectionnée 6 7 >ij SELECT * FROM students WHERE firstname LIKE Thor% ; 8 FIRSTNAME LASTNAME 9 10 0 ligne sélectionnée 8

Update a row Update a row with the UPDATE SQL command 1 >ij INSERT INTO students (firstname, sex) VALUES ( Thoralf, M ) ; 2 1 ligne insérée/mise à jour/supprimée 3 4 >ij SELECT * FROM students WHERE firstname LIKE Thor% ; 5 FIRSTNAME LASTNAME SEX 6 7 Thoralf NULL M 8 1 ligne sélectionnée 9 10 >ij UPDATE students SET lastname= G. WHERE firstname= Thoralf ; 11 1 ligne insérée/mise à jour/supprimée 12 13 >ij SELECT * FROM students WHERE firstname LIKE Thor% ; 14 FIRSTNAME LASTNAME SEX 15 16 Thoralf G. M 17 1 ligne sélectionnée 9

Delete a row Delete a row with the DELETE SQL command 1 >ij INSERT INTO students VALUES ( Olivier, Bonaventure, M ) ; 2 1 ligne insérée/mise à jour/supprimée 3 4 ij> SELECT * FROM students ; 5 FIRSTNAME LASTNAME SEX 6 7 Florence Turine F 8 Nicolas Laurent M 9 Thoralf G. M 10 Olivier Bonaventure M 11 4 lignes sélectionnées 12 13 >ij DELETE FROM students WHERE firstname= olivier AND lastname= bonaventure ; 14 1 ligne insérée/mise à jour/supprimée 15 16 >ij SELECT COUNT(*) AS nb FROM students ; 17 NB 18 19 3 20 1 ligne sélectionnée 10

And now, in Java! I Loading the driver 1 try 2 { 3 Class.forName ("org.apache.derby.jdbc.embeddeddriver"); 4 System.out.println ("Driver loaded"); 5 } 6 catch (ClassNotFoundException exception) 7 { 8 System.err.println ("Unable to find the driver : " + exception.getmessage()); 9 } The driver depends on the DB you want to connect to PostgreSQL : org.postgresql.driver MySQL : com.mysql.jdbc.driver 11

And now, in Java! II Connecting to the database and closing the connection 1 String dbname = "/Users/combefis/MyNewDB"; 2 3 try 4 { 5 Connection conn = 6 DriverManager.getConnection ("jdbc:derby:" + dbname + ";create=true"); 7 8 // [...] 9 10 conn.close(); 11 } 12 catch (SQLException exception) 13 { 14 System.err.println ("SQL error : " + exception.getmessage()); 15 } 12

And now, in Java! III Querying the database and retrieving results 1 Statement s = conn.createstatement(); 2 3 ResultSet res = s.executequery ("SELECT FROM students"); 4 while (res.next()) 5 { 6 System.out.printf ("%s %s %s\n", res.getstring (1), 7 res.getstring (2), res.getstring ("sex")); 8 } 9 res.close(); 10 11 s.close(); Can get values of a row with methods from ResultSet : by column number (first = 1) or by column name Don t forget res.close() to free resource 13

And now, in Java! IV Shutting down the database 1 boolean SQLexc = false; 2 try 3 { 4 DriverManager.getConnection ("jdbc:derby:;shutdown=true"); 5 } 6 catch (SQLException exception) 7 { 8 SQLexc = exception.getsqlstate().equals ("XJ015"); 9 } 10 11 if (SQLexc) 12 { 13 System.out.println ("Database shut down normally"); 14 } 15 else 16 { 17 System.out.println ("Database did not shut down normally"); 18 } 14

Updating the database With a Statement, use executequery to get a result executeupdate to perform an update (returns a ResultSet) (returns a int) 1 Statement s = conn.createstatement(); 2 3 int r = s.executeupdate ("DELETE FROM students"); 4 System.out.printf ("%d row(s) deleted\n", r); 5 6 s.close(); 15

Prepared statements and batch processing Use PreparedStatement to build a not complete statement that need to be filled Can prepared queries that will be executed in a batch 1 PreparedStatement pstmt = 2 conn.preparestatement ("INSERT INTO students VALUES (?,?,?)"); 3 4 for (int i = 0; i < firstnames.length; i++) 5 { 6 pstmt.setstring (1, firstnames[i]); 7 pstmt.setstring (2, lastnames[i]); 8 pstmt.setstring (3, sexs[i]); 9 pstmt.addbatch(); 10 } 11 12 int[] rs = pstmt.executebatch(); 16

SQLData or how to define a new type Seems that it is not available with Java DB 17

Primary key Rows indexed for better search performance Should defined keys and indexes Primary key should uniquely identify a row 1 >ij CREATE TABLE students 2 > ( 3 > id_student INTEGER NOT NULL 4 > PRIMARY KEY GENERATED ALWAYS AS IDENTITY 5 > (START WITH 1, INCREMENT BY 1), 6 > firstname VARCHAR(50), 7 > lastname VARCHAR(50), 8 > sex CHAR(1) NOT NULL 9 > ) ; 10 0 lignes insérées/mises à jour/supprimées 18

Establishing relations between tables We have a table describing students We want to add, for each student, the other students with which they are in a love relationship One student may have several active partners! id_students firstname lastname sex partners 1 Florence Turine F P. 2 Nicolas Laurent M Pénéloppe 3 Thoralf G. M 4 P. NULL M Florence 5 Péneloppe X F Nicolas, Nicole, Thoralf, Florence 6 Nicole Y F Pénéloppe Good solution? 19

Establishing relations between tables We have a table describing students We want to add, for each student, the other students with which they are in a love relationship One student may have several active partners! id_students firstname lastname sex partners 1 Florence Turine F 4 2 Nicolas Laurent M 6 3 Thoralf G. M 4 P. NULL M 1 5 Péneloppe X F 2, 6, 3, 1 6 Nicole Y F 5 What about that one? 19

Foreign key I 1 >ij CREATE TABLE relations 2 > ( 3 > p1 INTEGER NOT NULL, 4 > p2 INTEGER NOT NULL, 5 > PRIMARY KEY (p1, p2), 6 > FOREIGN KEY (p1) REFERENCES students(id_student), 7 > FOREIGN KEY (p2) REFERENCES students(id_student) 8 > ) ; 9 0 lignes insérées/mises à jour/supprimées PRIMARY KEY constraint defines the table s primary key FOREIGN KEY constraint states that a key in that table refers to a key in another one 20

Foreign key II The constraints are checked when the tables are updated 1 >ij INSERT INTO relations VALUES (42, 69) ; 2 ERREUR 23503 : INSERT sur la table RELATIONS a entrainé la violation de la 3 contrainte de clé externe SQL100228172518851 pour la clé (42). 4 L instruction a été annulée. Argh! Why? 21

Foreign key III http://www.smbc-comics.com/ 22

Foreign key IV Here are the two tables id_students firstname lastname sex 1 Florence Turine F 2 Nicolas Laurent M 3 Thoralf G. M 4 P. NULL M 5 Péneloppe X F 6 Nicole Y F p1 p2 1 4 2 6 4 1 5 2 5 6 5 3 5 1 6 5 23

Making joins Making queries on several tables, joining on some columns 1 >ij SELECT S1.firstname, S2.firstname FROM relations, students S1, students S2 2 WHERE p1=s1.id_student AND p2=s2.id_student ; 3 FIRSTNAME FIRSTNAME 4 5 Florence P. 6 Nicolas Nicole 7 P. Florence 8 Péneloppe Florence 9 Péneloppe Nicolas 10 Péneloppe Thoralf 11 Péneloppe Nicole 12 Nicole Péneloppe 13 14 8 lignes sélectionnées 24

Resources and References I JDBC : Practical Guide for Java TM Programmers (2001) Gregory Speegle Morgan Kaufmann 978-1558607361 Java TM Database Best Practices (2003) George Reese O Reilly 978-0596005221 25

Resources and References II Sun Java TM tutorial The Sun tutorial on JDBC Apache Derby tutorial http://java.sun.com/docs/books/tutorial/jdbc/ How to use derby : install it, use the ij tool and query databases, embedded and network derby http://db.apache.org/derby/papers/derbytut/index.html 26