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