forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchLesson.java
More file actions
37 lines (33 loc) · 1.49 KB
/
Copy pathBatchLesson.java
File metadata and controls
37 lines (33 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package jdbc;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import java.io.IOException;
import java.sql.*;
/**
* Created by max on 2/8/17.
*/
public class BatchLesson {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XMLStreamException, TransformerException, XPathExpressionException, SQLException, ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/Lessons";
String username = "root";
String password = "1";
Class.forName("com.mysql.jdbc.Driver");
try(Connection conn = DriverManager.getConnection(url, username, password);
Statement stat = conn.createStatement()) {
conn.setAutoCommit(false);
stat.addBatch("drop table IF EXISTS Books");
stat.addBatch("CREATE TABLE IF NOT EXISTS Books (id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id))");
stat.addBatch("INSERT INTO Books (name) VALUES ('Inferno')");
stat.addBatch("INSERT INTO Books (name) VALUES ('DaVinchi Code')");
stat.addBatch("INSERT INTO Books (name) VALUES ('Solomon key')");
if(stat.executeBatch().length == 5) {
conn.commit();
} else {
conn.rollback();
}
}
}
}