Работа с базами данных – JDBC Введение в JDBC
JDBC JDBC (Java DataBase Connectivity) – технология доступа к базам данных для Java Состоит из: API (java.sql and javax.sql) Driver Manager Test Suite + JDBC-ODBC Bridge
JDBC API java.sql.Connection java.sql.Statement (Prepared + Callable) java.sql.ResultSet java.sql.*
Driver Class.forName("com.my.MyDriver"); // mydriver Connection conn = DriverManager.getConnection( "jdbc:mydriver:myDatabase", username,password); // Class.forName("com.mysql.jdbc.Driver"); // "jdbc:mysql://localhost:3306/db_name"
Driver
SQL LectorIDFirstNameLastNameDateOfStartCount 1MaxTyukh EugeneBochkov MaxMashnitsky AndrewGrigoruk
SQL Connection conn = DriverManager.getConnection( "jdbc:myDriver:myDatabase", username, password); Statement stmt = conn.createStatement(); String q = "SELECT FirstName, LastName, Count FROM Lessions WHERE Count > 4"; ResultSet rs = stmt.executeQuery(q); while (rs.next()) { String f = rs.getString("FirstName"); String l = rs.getFloat("LastName"); int c = rs.getInt("Count"); System.out.println(f + l + " : " + c); } conn.close();
SQL FirstNameLastNameCount MaxMashnitsky5 AndrewGrigoruk6 SELECT FirstName, LastName, Count FROM Lessions WHERE Count > 4;
SQL INSERT INTO Lessions (FirstName, LastName, DateOfStart, Count) VALUES ('Ivan', 'Petrov', ' ', 2); LectorIDFirstNameLastNameDateOfStartCount 1MaxTyukh EugeneBochkov MaxMashnitsky AndrewGrigoruk IvanPetrov
SQL Data Manipulation Language (DML) – SELECT, INSERT, DELETE, UPDATE Data Definition Language (DDL) – CREATE, DROP, ALTER Data Control Language (DCL) – GRANT, REVOKE, DENY Transaction Control Language (TCL) – COMMIT, ROLLBACK, SAVEPOINT
Преимущества JDBC Изоляция разработки (отсутствие необходимости знать особенностей БД) Повторное использование кода Использование простого клиента (драйвер) Простота конфигурации (URL + properties)
Q&A