Java DataBase Connectivity.
L'ensemble de ces interfaces JDBC est implémenté par les éditeurs sous forme de pilotes (drivers), classés selons 4 catégories :
| Client | Application | |||||
|---|---|---|---|---|---|---|
| API standard | J2C | |||||
| JDBC Connector | ||||||
| JDBC | ||||||
| Pilote JDBC | Type 1 | Type 2 (Java) | Type 3 (Java) | Type 4 (Java) | ||
| Infrastructure | ODBC (natif) | Pilote client SGBDR (natif) | ||||
| Pilote ODBC (natif) | ||||||
| Protocole | SGBDR | SGBDR | Pilote (généralement réseau, comme HTTP, etc.) | SGBDR | ||
| Serveur | SGBDR | SGBDR | Serveur pilote | SGBDR | ||
| Pilote type 2 | Pilote type 4 | |||||
| SGBDR A | SGBDR B | |||||
JDBC est une API de J2SE comprenant :
Le cœur de l'API de JDBC est intégrée à J2SE depuis J2SE 1.1. Son extension est requise par J2EE.
| JDBC | Version | 1 | 2 | 3 | Commentaire | |||||
|---|---|---|---|---|---|---|---|---|---|---|
| Partie | Technologie | Release | 0 | 1 | 2 | 0 | 1 | 0 | ||
| Plate-forme | J2SE | 1.1 | 1.2 | 1.3 | 1.4 | Plate-forme Java intégrante | ||||
| Core | DriverManager | Oui | cœur obligatoire | |||||||
| Connection | Oui | Connexion | ||||||||
| Statement | Oui | Requête | ||||||||
| getGeneratedKeys | Non | Oui | Clés auto-générées | |||||||
| getMoreResults | Non | Oui | ||||||||
| getConnection | Non | Oui | ||||||||
| Batch updates | Non | Optionnel | Ajoute une requête SQL pour executeBatch | |||||||
| resultSetType | Non | Oui | En avant seulement, en avant/arrière, et isolé ou non | |||||||
| resultSetConcurrency | Non | Oui | Modifiable ou en lecture seule | |||||||
| fetchDirection | Non | Oui | ||||||||
| fetchSize | Non | Oui | Nombre de lignes pour une page | |||||||
| PreparedStatement | Oui | Requête précompilée (SQL dynamique) | ||||||||
| CallableStatement | Oui | Procédure stockée | ||||||||
| ResultSet | ARRAY | Non | getArray | java.sql.Array | ||||||
| Non | updateArray | |||||||||
| TINYINT | getByte | |||||||||
| SMALLINT | getShort | |||||||||
| SMALLINT | getInt | |||||||||
| BIGINT | getLong | |||||||||
| REAL | getFloat | |||||||||
| FLOAT | getDouble | |||||||||
| DOUBLE | ||||||||||
| DECIMAL | getBigDecimal | java.math.BigDecimal | ||||||||
| NUMERIC | ||||||||||
| BIT | getBoolean | |||||||||
| VARCHAR | getString | |||||||||
| CHAR | ||||||||||
| VARBINARY | getBytes | byte[] | ||||||||
| BINARY | ||||||||||
| DATE | Non | getDate | java.sql.Date | |||||||
| TIME | Non | getTime | java.sql.Time | |||||||
| TIMESTAMP | Non | getTimeStamp | java.sql.TimeStamp | |||||||
| LONGVARCHAR | getAsciiStream | Retourne un java.io.InputStream pour un | ||||||||
| getUnicodeStream | Retourne un java.io.InputStream pour un | |||||||||
| LONGVARBINARY | getBinaryStream | Retourne un java.io.InputStream pour un | ||||||||
| Tous | getObject | Le type SQL est retourné comme un java.lang.Objet | ||||||||
| Indice de ligne | Non | getRow | Numéro de ligne courant | |||||||
| Type structuré | Non | getRef | Référence sur un type structuré (object) SQL3. Peut être lazy | |||||||
| Non | updateRef | |||||||||
| BLOB | Non | updateBlob | Binary Large Object | |||||||
| CLOB | Non | updateClob | Binary Character Object | |||||||
| URL | Non | getURL | ||||||||
| Extension | DataSource | Non | Oui | Optionnel | ||||||
| ConnectionEvent | Non | Oui | ||||||||
| RowSet | Non | Oui | ||||||||
Des exemples de pilotes JDBC sont :
Un exemple de code JDBC en environnement J2EE est :
Context namingContext = new InitialContext();<br> <strong>DataSource </strong>dataSource = (<strong>DataSource</strong>)
namingContext.lookup (<span class="codeString">"java:comp/env/jdbc/MyDataSource"</span>);<br> <strong><br>
Connection </strong>dataSourceConnection = dataSource.<strong>getConnection</strong>();<br> <br> try {<br>
<strong> PreparedStatement </strong>selectStatement = connection.<strong>prepareStatement </strong> (<span class="codeString">"SELECT ID, SHORT_NAME, RELATIONSHIPS.TYPE
FROM SKILL, RELATIONSHIPS WHERE ID=RELATIONSHIPS.ID_TO AND RELATIONSHIPS.ID_FROM=?"</span>);
selectStatement.<strong>setInt </strong> (1, 123);<br> selectStatement.<strong>setString </strong>(2, <span class="codeString">"UnType"</span>);
<strong> ResultSet</strong> skillSet = selectStatement.<strong>executeQuery</strong>();
List linkedSkills = new ArrayList();<br> while (skillSet.<strong>next</strong>()) { <br> int id =
skillSet.<strong>getInt </strong> (<span class="codeString">"ID"</span>);<br> String shortName =
skillSet.<strong>getString</strong> (<span class="codeString">"SHORT_NAME"</span>);<br>Skill newSkill
= new SkillImpl (id, shortName);<br>linkedSkills.add (newSkill);<br> }
return linkedSkills;<br> }<br> finally {<br> connection.<strong>close</strong>(); // Ferme le statement et
le ResultSet <br> }