Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- class DbCreate {
- public static void main(String args[]) {
- String[] queries = {
- "DROP USER IF EXISTS 'z4c8'@'localhost';",
- "DROP DATABASE IF EXISTS z4c8base;",
- "CREATE DATABASE z4c8base DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;",
- "CREATE USER 'z4c8'@'localhost' IDENTIFIED BY 'password';",
- "GRANT ALL ON z4c8base.* TO 'z4c8'@'localhost' IDENTIFIED BY 'z4c9pass';",
- "USE z4c8base;",
- "CREATE TABLE aktorka (" +
- "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
- "nazwisko VARCHAR(50) NOT NULL, " +
- "rok_urodzenia INT)",
- "CREATE TABLE rezyser (" +
- "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
- "nazwisko VARCHAR(50) NOT NULL, " +
- "rok_urodzenia INT)",
- "CREATE TABLE film (" +
- "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
- "tytul VARCHAR(100), " +
- "rok_produkcji INT, " +
- "rezyser_id INT REFERENCES rezyser.id, " +
- "aktorka INT REFERENCES aktorka.id)"
- }; // cp ze skryptu
- Connection connection;
- int numerator = 1;
- System.out.print("connecting... ");
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance(); // cp ze skryptu
- } catch (Exception e){
- e.printStackTrace();
- }
- try {
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", ""); // cp ze skryptu
- System.out.println("connected.");
- } catch (SQLException e) {
- throw new IllegalStateException("cannot connect", e);
- }
- System.out.println("executing:");
- try {
- Statement statement = connection.createStatement();
- for (String q : queries) {
- try {
- System.out.println(numerator + ": " + q);
- statement.executeUpdate(q);
- } catch (SQLException qe) {
- qe.printStackTrace();
- }
- numerator++;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // System.out.println("All OK.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement