· 9 years ago · Nov 24, 2016, 06:44 AM
1package eg.edu.alexu.csd.oop.XML_DBMS;
2import java.io.BufferedWriter;
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.nio.file.Files;
8import java.nio.file.Path;
9
10public class DBMSFunctions {
11/**
12 * creates one database folder
13 * @param databaseName
14 */
15 public void createDatabase(String databaseName) {
16 File dir = new File(databaseName);
17 dir.mkdir();
18 }
19 /**
20 * create table in the required database
21 * @param databaseName
22 * @param tableName
23 * @throws FileNotFoundException
24 */
25
26 public void createTable(String databaseName,String tableName)
27 throws FileNotFoundException {
28 File f = new File(databaseName);
29 if (f.exists() && f.isDirectory()) {
30 String fileName = tableName +".xml";
31 String absoluteFilePath = databaseName + File.separator + fileName;
32 try {
33 File file = new File(absoluteFilePath);
34 if (file.createNewFile()) {
35 System.out.println("File is created!");
36 } else {
37 System.out.println("File already exists!");
38 }
39
40 } catch (IOException e) {
41 e.printStackTrace();
42 }
43 } else {
44 throw new FileNotFoundException ();
45 }
46
47 }
48 /**
49 * delete the required database
50 * @param databaseName
51 */
52
53 public void dropDatabase(String databaseName) {
54 File f = new File(databaseName);
55 if (f.exists() && f.isDirectory()) {
56 String[]entries = f.list();
57 for(String s: entries){
58 File currentFile = new File(f.getPath(),s);
59 currentFile.delete();
60 }
61 f.delete();
62 } else {
63 System.out.println("database not found");
64 }
65
66 }
67 /**
68 * delete the required table from the chosen database
69 * @param databaseName
70 * @param tableName
71 * @throws FileNotFoundException
72 */
73 public void dropTable(String databaseName,String tableName)
74 throws FileNotFoundException {
75
76 File f1 = new File(databaseName);
77 if (f1.exists() && f1.isDirectory()) {
78 String fileName = tableName;
79 String absoluteFilePath = databaseName + File.separator + fileName+".xml";
80 File f2 = new File(absoluteFilePath);
81 if (f2.exists()) {
82 f2.delete();
83 } else {
84 throw new FileNotFoundException ();
85 }
86 } else {
87 System.out.println("database not found");
88 }
89 }
90 void insertIntoTable(String databaseName,String tableName) {
91
92 }
93 //testing main
94 public static void main(String[]args) throws FileNotFoundException{
95 DBMSFunctions y= new DBMSFunctions();
96 y.createDatabase("d1");
97 y.createTable("d1", "t1");
98 y.createTable("d1", "t2");
99 // y.dropDatabase("d1");
100 y.dropTable("d1", "t2");
101 }
102
103}