Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.DataBase;
- import java.io.File;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.apache.commons.io.FileUtils;
- public class Queries implements DataBaseInterface {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
- String DB_path;
- private static Connection getDBConnection() {
- Connection dbConnection = null;
- try {
- Class.forName(JDBC_DRIVER);
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- }
- try {
- dbConnection = DriverManager.getConnection(DB_URL);
- return dbConnection;
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return dbConnection;
- }
- @Override
- public void createTable(String tableName, String properties) {
- // TODO Auto-generated method stub
- Connection dbConnection = null;
- Statement statement = null;
- File tableDirectory = new File(DB_path + File.separator + tableName+".xml");
- try {
- dbConnection = getDBConnection();
- statement = dbConnection.createStatement();
- //System.out.println(createTableSQL);
- // execute the SQL stetement
- statement.execute(properties);
- System.out.println("Table \"dbuser\" is created!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- if (statement != null) {
- try {
- statement.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (dbConnection != null) {
- try {
- dbConnection.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public void createDatabase(String databaseName) {
- File file = new File("C:\\"+databaseName);
- if (!file.exists()) {
- if (file.mkdir()) {
- DB_path="C:\\"+databaseName;
- }} else {
- try {
- FileUtils.cleanDirectory(file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println("Data Base Created successfully");
- }
- public boolean executeStructureQuery(String query) throws SQLException {
- return false;
- }
- public void dropDatabase(String databaseName) {
- File file = new File("C:\\"+databaseName);
- try {
- FileUtils.deleteDirectory(file);
- System.out.println("Data Base Dropped successfully");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment