Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1.  
  2. import java.io.DataOutput;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. /**
  9. * Created by Tomer Katzav (ID: 302175716) and Nir Mekin(ID: 301734158) on 21/11/2016.
  10. */
  11. public class TableCreator {
  12. static final String JDBC_Driver = "com.mysql.jdbc.Driver"; // jdbc driver and db url
  13. static final String DB_URL = "jdbc:mysql://localhost:3306/college";
  14. static final String UserName = "root"; // DB login information
  15. static final String Password = "Ktzv3404";
  16.  
  17. public static void main(String[] args) {
  18. Connection connection = null;
  19. Statement statement = null;
  20. try{
  21. Class.forName("com.mysql.jdbc.Driver");
  22. System.out.println("Connecting to the selected database... Please hold on");
  23. connection = DriverManager.getConnection(DB_URL, UserName, Password);
  24. System.out.println("You are now successfully connected to the database!");
  25. statement = connection.createStatement();
  26. statement.execute("CREATE TABLE Teachers " +
  27. "(ID INTEGER, " +
  28. "Name VARCHAR(25), " +
  29. "Age INTEGER, " +
  30. "Phone VARCHAR(25), " +
  31. "Adress VARCHAR(25), " +
  32. "BirthDate VARCHAR(25), " +
  33. "PRIMARY KEY ( ID ))");
  34. statement.execute("CREATE TABLE Courses " +
  35. "(CourseNum INTEGER, " +
  36. "Subject VARCHAR(25), " +
  37. "Semester VARCHAR(25), " +
  38. "Year VARCHAR(25), " +
  39. "Weekly_Hours INTEGER, " +
  40. "PRIMARY KEY ( CourseNum ))");
  41. statement.execute("CREATE TABLE Classes " +
  42. "(ClassNum INTEGER, " +
  43. "Building VARCHAR(25), " +
  44. "Floor INTEGER, " +
  45. "PRIMARY KEY ( ClassNum ))");
  46. statement.execute("CREATE TABLE Lecture " +
  47. "(ID INTEGER, " +
  48. "ClassNum INTEGER, " +
  49. "CourseNum INTEGER, " +
  50. "Day VARCHAR(25), " +
  51. "Hour VARCHAR(25), " +
  52. "FOREIGN KEY (ID) REFERENCES Teachers (ID), " +
  53. "FOREIGN KEY (CourseNum) REFERENCES Courses (CourseNum), " +
  54. "FOREIGN KEY (ClassNum) REFERENCES Classes (ClassNum))");
  55. System.out.println("The tables were created in the database successfully!");
  56. System.out.println("Now inserting the requested data into the tables and columns...");
  57. System.out.println("...\n...\n...\n");
  58. statement.executeUpdate("INSERT INTO `Classes`" +
  59. "VALUES (2105, 'Mitchell', 1)");
  60. statement.executeUpdate("INSERT INTO `Classes`" +
  61. "VALUES (2204, 'Mitchell', 2)");
  62. statement.executeUpdate("INSERT INTO `Classes`" +
  63. "VALUES (246, 'Fernik', 3)");
  64. statement.executeUpdate("INSERT INTO `Teachers`" +
  65. "VALUES (302175716, 'Tomer Katzav', 27, 0521112223, 'Poleg 1', '04/04/1989')");
  66. statement.executeUpdate("INSERT INTO `Teachers`" +
  67. "VALUES (301734158, 'Nir Mekin', 27, 0508939643, 'Levi 1', '07/05/1989')");
  68. statement.executeUpdate("INSERT INTO `Teachers`" +
  69. "VALUES (456102333, 'Moshe Levi', 40, 054101334, 'Sokolov 3', '30/02/1976')");
  70. statement.executeUpdate("INSERT INTO `Courses`" +
  71. "VALUES (31, 'Histroy', 'A', '2nd', 4)");
  72. statement.executeUpdate("INSERT INTO `Courses`" +
  73. "VALUES (6, 'Mathematics', 'B', '1st', 8)");
  74. statement.executeUpdate("INSERT INTO `Courses`" +
  75. "VALUES (50, 'Arts', 'Summer', '3rd', 2)");
  76. statement.executeUpdate("INSERT INTO `Lecture` " +
  77. "VALUES (302175716, 2105, 31, 'Monday', '08:00')");
  78. statement.executeUpdate("INSERT INTO `Lecture` " +
  79. "VALUES (301734158, 2204, 6, 'Wednesday', '12:00')");
  80. statement.executeUpdate("INSERT INTO `Lecture` " +
  81. "VALUES (456102333, 246, 50, 'Tuesday', '09:30')");
  82. System.out.println("Done!");
  83. }catch(SQLException se){
  84. System.out.println("SQL Exception");
  85. se.printStackTrace();
  86. }catch(Exception e){
  87. System.out.println("Exception");
  88. e.printStackTrace();
  89. }finally{
  90. try{
  91. if (statement != null)
  92. connection.close();
  93. }catch(SQLException se){
  94. System.out.println("SQL Exception while disconnecting");
  95. se.printStackTrace();
  96. }
  97. }
  98. System.out.println("The program will now end!");
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement