Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5.  
  6. class DbCreate {
  7.  
  8. public static void main(String args[]) {
  9.  
  10. String[] queries = {
  11. "DROP USER IF EXISTS 'z4c8'@'localhost';",
  12. "DROP DATABASE IF EXISTS z4c8base;",
  13. "CREATE DATABASE z4c8base DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;",
  14. "CREATE USER 'z4c8'@'localhost' IDENTIFIED BY 'password';",
  15. "GRANT ALL ON z4c8base.* TO 'z4c8'@'localhost' IDENTIFIED BY 'z4c9pass';",
  16. "USE z4c8base;",
  17. "CREATE TABLE aktorka (" +
  18. "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
  19. "nazwisko VARCHAR(50) NOT NULL, " +
  20. "rok_urodzenia INT)",
  21. "CREATE TABLE rezyser (" +
  22. "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
  23. "nazwisko VARCHAR(50) NOT NULL, " +
  24. "rok_urodzenia INT)",
  25. "CREATE TABLE film (" +
  26. "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
  27. "tytul VARCHAR(100), " +
  28. "rok_produkcji INT, " +
  29. "rezyser_id INT REFERENCES rezyser.id, " +
  30. "aktorka INT REFERENCES aktorka.id)"
  31. }; // cp ze skryptu
  32.  
  33. Connection connection;
  34. int numerator = 1;
  35.  
  36. System.out.print("connecting... ");
  37.  
  38. try {
  39. Class.forName("com.mysql.jdbc.Driver").newInstance(); // cp ze skryptu
  40. } catch (Exception e){
  41. e.printStackTrace();
  42. }
  43.  
  44. try {
  45. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", ""); // cp ze skryptu
  46. System.out.println("connected.");
  47. } catch (SQLException e) {
  48. throw new IllegalStateException("cannot connect", e);
  49. }
  50.  
  51. System.out.println("executing:");
  52.  
  53. try {
  54. Statement statement = connection.createStatement();
  55. for (String q : queries) {
  56. try {
  57. System.out.println(numerator + ": " + q);
  58. statement.executeUpdate(q);
  59. } catch (SQLException qe) {
  60. qe.printStackTrace();
  61. }
  62. numerator++;
  63.  
  64. }
  65.  
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69.  
  70. try {
  71. connection.close();
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. }
  75.  
  76. // System.out.println("All OK.");
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement