1 / 15

JDBC

JDBC. Java Databases Connectivity. Objectifs. Fournir un accès homogène aux SGBDR Abstraction des SGBDR cibles Requêtes SQL Simple à mettre en oeuvre Core API (1.1). Fonctionnement. JDBC interagit avec le SGBDR par un driver

Download Presentation

JDBC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JDBC Java Databases Connectivity Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  2. Objectifs • Fournir un accès homogène aux SGBDR • Abstraction des SGBDR cibles • Requêtes SQL • Simple à mettre en oeuvre • Core API (1.1) Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  3. Fonctionnement • JDBC interagit avec le SGBDR par un driver • Il existe des drivers pour Oracle, Sybase, Informix, DB2, ... • 4 types de drivers : • 1. Bridge ODBC (fourni avec JDBC) • 2. Native-API partly-Java driver • 3. JDBC-Net all-Java driver • 4. Native-protocol all-Java driver • 1. et 2. nécessite des architectures 3-tiers pour les applets Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  4. Drivers JDBC Client Application Java JDBC API JDBC-ODBC Bridge JDBC-Native Bridge JDBC-Net Bridge All Java JDBC Driver (Type 2) (Type 4) (Type 1) (Type 3) Native API (C, C++) ODBC Driver JDBC NetDriver SGBDR Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  5. Architectures 2-tier et 3-tier J D B C Application ou Applet TCP / Protocole propriètaire (SqlNet Oracle) SGBDR Architecture 2-tier J D B C Applet Frontend SGBDR Architecture 3-tier Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  6. Accès aux données 1 Charger le driver 2 Connexion à la base 3 Création d'un statement 4 Exécution de la requête 5 Lecture des résultats Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  7. Chargement du driver • Utiliser la méthode forName de la classe Class : • Class.forName("sun.jdbc.odbc.JdbcOrdbDriver").newInstance(); • Class.forName("postgres95.pgDriver").newInstance(); Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  8. Connexion à la base (1/2) • Accès via un URL qui spécifie : • l'utilisation de JDBC • le driver ou le type du SGBDR • l'identification de la base • Exemple : String url="jdbc:odbc:ma_base"; String url="jdbc:pg95:mabase?username=toto:password=titi"; • Ouverture de la connexion : Connection conn = DriverManager.getConnection(url, user, password); Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  9. Création d'un Statement • 3 types de statement : • statement: requêtes simples • prepared statement: requêtes précompilées • callable statement : procédures stockées • Création d'un statement : Statement stmt = conn.createStatement(); Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  10. Execution d'une requête (1/2) • 3 types d'executions : • executeQuery: pour les requêtes qui retournent un ResultSet • executeUpdate: pour les requêtes INSERT, UPDATE, DELETE, CREATE TABLE et DROP TABLE • execute : pour quelques cas rares (procédures stockées) Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  11. Execution d'une requête (2/2) • Execution de la requête : String myQuery = "SELECT prenom, nom, email " + "FROM employe " + "WHERE (nom='Dupont') AND (email IS NOT NULL) " + "ORDER BY nom"; ResultSet rs = stmt.executeQuery(myQuery); Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  12. Lecture des résultats (1/2) • executeQuery() renvoit un ResultSet • Le RS se parcourt itérativement row par row • Les colonnes sont référencées par leur numéro ou par leur nom • L'accès aux valeurs des colonnes se fait par les méthodes getXXX() où XXX représente le type de l'objet • Pour les très gros row, on peut utiliser des streams. Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  13. Lecture des résultats (2/2) java.sql.Statement stmt = conn.createStatement(); ResultSetrs = stmt.executeQuery("SELECT a, b, c FROM Table1"); while (rs.next()) { // print the values for the current row. int i = rs.getInt("a"); String s = rs.getString("b"); byte b[] = rs.getBytes("c"); System.out.println("ROW = " + i + " " + s + " " + b[0]); } Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  14. Accès aux méta-données • La méthode getMetaData() permet d'obtenir les méta-données d'un ResultSet. • Elle renvoit des ResultSetMetaData. • On peut connaitre : • Le nombre de colonne : getColumnCount() • Le nom d'une colonne : getColumnName(int col) • Le type d'une colonne : getColumnType(int col) • ... Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

  15. Exemple complet : public class TestJDBC { public static void main(String[] args) throws Exception { Class.forName("postgres95.pgDriver"); Connection conn; conn = DriverManager.getConnection("jdbc:pg95:mabase", "dedieu", ""); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from employe"); while (rs.next()) { String nom = rs.getString("nom"); String prenom = rs.getString("prenom"); String email = rs.getString("email"); } } } Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr

More Related