Guest User

Untitled

a guest
Nov 21st, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.DataBase;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import org.apache.commons.io.FileUtils;
  11. public class Queries implements DataBaseInterface {
  12. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  13. static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
  14.  
  15. String DB_path;
  16.  
  17.  
  18. private static Connection getDBConnection() {
  19.  
  20. Connection dbConnection = null;
  21.  
  22. try {
  23.  
  24. Class.forName(JDBC_DRIVER);
  25.  
  26. } catch (ClassNotFoundException e) {
  27.  
  28. System.out.println(e.getMessage());
  29.  
  30. }
  31.  
  32. try {
  33.  
  34. dbConnection = DriverManager.getConnection(DB_URL);
  35. return dbConnection;
  36.  
  37. } catch (SQLException e) {
  38.  
  39. System.out.println(e.getMessage());
  40.  
  41. }
  42.  
  43. return dbConnection;
  44.  
  45. }
  46. @Override
  47. public void createTable(String tableName, String properties) {
  48. // TODO Auto-generated method stub
  49. Connection dbConnection = null;
  50. Statement statement = null;
  51. File tableDirectory = new File(DB_path + File.separator + tableName+".xml");
  52. try {
  53. dbConnection = getDBConnection();
  54. statement = dbConnection.createStatement();
  55.  
  56. //System.out.println(createTableSQL);
  57. // execute the SQL stetement
  58. statement.execute(properties);
  59.  
  60. System.out.println("Table \"dbuser\" is created!");
  61.  
  62. } catch (SQLException e) {
  63.  
  64. System.out.println(e.getMessage());
  65.  
  66. } finally {
  67.  
  68. if (statement != null) {
  69. try {
  70. statement.close();
  71. } catch (SQLException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. }
  76.  
  77. if (dbConnection != null) {
  78. try {
  79. dbConnection.close();
  80. } catch (SQLException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. }
  85.  
  86. }
  87.  
  88.  
  89.  
  90. }
  91.  
  92. public void createDatabase(String databaseName) {
  93. File file = new File("C:\\"+databaseName);
  94. if (!file.exists()) {
  95. if (file.mkdir()) {
  96. DB_path="C:\\"+databaseName;
  97.  
  98. }} else {
  99. try {
  100. FileUtils.cleanDirectory(file);
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. System.out.println("Data Base Created successfully");
  106. }
  107.  
  108.  
  109.  
  110. public boolean executeStructureQuery(String query) throws SQLException {
  111.  
  112.  
  113.  
  114. return false;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. public void dropDatabase(String databaseName) {
  121. File file = new File("C:\\"+databaseName);
  122.  
  123. try {
  124. FileUtils.deleteDirectory(file);
  125. System.out.println("Data Base Dropped successfully");
  126.  
  127. } catch (IOException e) {
  128. // TODO Auto-generated catch block
  129. e.printStackTrace();
  130. }
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137. }
Add Comment
Please, Sign In to add comment