Работа с базами данных – JDBC Основы JDBC
description
Transcript of Работа с базами данных – JDBC Основы JDBC
Работа с базами данных – JDBCОсновы JDBC
DDL + DCL• CREATE DATABASE test_db;• CREATE USER 'test_db_user'@'%' IDENTIFIED BY
'test_db_password';
• GRANT ALL ON test_db.* TO 'test_db_user'@'%';• REVOKE ALL ON test_db.* FROM 'test_db_user'@'%';
• DROP USER test_db_user;• DROP DATABASE test_db;
DDL• CREATE TABLE test_db.lectors (
id INT NOT NULL AUTO_INCREMENT, first_name VARCHAR(50) NOT NULL DEFAULT "", last_name VARCHAR(50) NOT NULL DEFAULT "", PRIMARY KEY (id)) ENGINE=INNODB;
• CREATE TABLE test_db.lessions ( id INT NOT NULL AUTO_INCREMENT, topic VARCHAR(500) NOT NULL DEFAULT "", date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, lector_id INT NOT NULL DEFAULT 0, PRIMARY KEY (id)) ENGINE=INNODB;
DML• INSERT INTO test_db.lectors
(first_name, last_name)VALUES ('Max', 'Tyukh');
• INSERT INTO test_db.lectors (first_name, last_name)VALUES ('Max', 'Mashnitsky'), ('Eugene', 'Bochkov'), ('Andrew', 'Grigoruk');
DMLid first_name last_name1 Max Tyukh2 Max Mashnitsky3 Eugene Bochkov4 Andrew Grigoruk
DML• INSERT INTO test_db.lessions
(topic, date, lector_id)VALUES ('Intro', '2013-04-23 16:00:00', 1), ('Basic 1', '2013-04-25 16:00:00', 2), ('Basic 2', '2013-04-30 16:00:00', 2), ('Basic 3', '2013-05-07 16:00:00', 2), ('Basic 4', '2013-05-14 16:00:00', 2), ('Basic 5', '2013-05-16 16:00:00', 2))
DMLid topic date lector_id1 Intro 2013-04-23
16:00:001
2 Basic 1 2013-04-25 16:00:00
2
3 Basic 2 2013-04-30 16:00:00
2
4 Basic 3 2013-05-07 16:00:00
2
5 Basic 4 2013-05-14 16:00:00
2
6 Basic 5 2013-05-16 16:00:00
2
DMLConnection conn = DriverManager.getConnection( "jdbc:myDriver://localhost:3306/test_db", "test_db_user", "test_db_password");String q = "INSERT INTO test_db.lessions (topic, lector_id) VALUES (?, ?)";PreparedStatement stmt = conn.prepareStatement(q);for (int i = 1; i <= 4; i++) { stmt.setString(1, "Core " + i); stmt.setInt(2, 3); // ~~ get lector id from DB stmt.addBatch();}stmt.executeBatch();conn.close();
DMLid topic date lector_id1 Intro 2013-04-23
16:00:001
2 Basic 1 2013-04-25 16:00:00
2
3 Basic 2 2013-04-30 16:00:00
2
4 Basic 3 2013-05-07 16:00:00
2
5 Basic 4 2013-05-14 16:00:00
2
6 Basic 5 2013-05-16 16:00:00
2
7 Core 1 2013-06-11 16:00:00
3
8 Core 2 2013-06-11 16:00:01
3
9 Core 3 2013-06-11 16:00:02
3
10 Core 4 2013-06-11 16:00:03
3
DMLCREATE DEFINER = 'test_db_user'@'%'PROCEDURE test_db.AddLesson (IN _topic VARCHAR(500),IN _date TIMESTAMP,IN _lector_id INT)BEGIN INSERT INTO test_db.lessions (topic, date, lector_id) VALUES (_topic, _date, _lector_id);END
DMLConnection conn = DriverManager.getConnection( "jdbc:myDriver://localhost:3306/test_db", "test_db_user", "test_db_password");String q = "Call AddLession(?, ?)";CallableStatement stmt = conn.prepareCall(q);
stmt.setString(1, "Hibernate 1");stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));stmt.setInt(3, 4);
stmt.execute();conn.close();
DMLid topic date lector_id1 Intro 2013-04-23
16:00:001
2 Basic 1 2013-04-25 16:00:00
2
3 Basic 2 2013-04-30 16:00:00
2
4 Basic 3 2013-05-07 16:00:00
2
5 Basic 4 2013-05-14 16:00:00
2
6 Basic 5 2013-05-16 16:00:00
2
7 Core 1 2013-06-11 16:00:00
3
8 Core 2 2013-06-11 16:00:01
3
9 Core 3 2013-06-11 16:00:02
3
10 Core 4 2013-06-11 16:00:03
3
11 Hibernate 1 2013-06-11 16:00:04
4
Q&A