Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

package Executors;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.sql.Connection;
import java.sql.DriverManager;

public class Database {


private static String Con_;
private static String databaseName;
private static String User_;
private static String Password_;
private static String tableName;
private static java.sql.Connection conn;
private static String tableCreatorS;
private static String databaseNameHolderPath;
private static String previousDatabase;

public final synchronized static java.sql.Connection getConn() throws


Exception{
if (conn == null || conn.isClosed())
getConnection();
return conn;
}

public final static void setDatabase(String con,String user,String


password,String database,String tablename,String path,String holderpath) {
Con_ = con;
User_ = user;
Password_ = password;
databaseName = database;
tableName = tablename;
tableCreatorS = java.nio.file.Paths.get(Installer.getProgramPath(),
path).toString();
databaseNameHolderPath =
java.nio.file.Paths.get(Installer.getProgramPath(),holderpath).toString();
java.io.File nameHolderFile = new java.io.File(databaseNameHolderPath);
java.io.File tableCreatorFile = new java.io.File(tableCreatorS);
if(!nameHolderFile.exists()) {
Installer.deleteDirectory(null);
throw new RuntimeException("Error in locating holder file");
}
if(!tableCreatorFile.exists()) {
Installer.deleteDirectory(null);
throw new RuntimeException("Error in locating creator file");
}
}

public static final boolean hasDatabaseChanged() throws Exception {


previousDatabase =
Files.readAllLines(Paths.get(databaseNameHolderPath)).get(0);
if(!previousDatabase.equalsIgnoreCase(databaseName)) {
createRemoveDatabase();
return true;
}
return false;
}
public static final void createRemoveDatabase() throws Exception{
createDB();
createTable();
}

private static final void getConnection() throws Exception {


try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn =
java.sql.DriverManager.getConnection(Con_+databaseName,User_,Password_);
} catch (ClassNotFoundException e) {
Setup.Error(e);
}
}

private static final void createDB() throws Exception {


String createDatabase = "CREATE DATABASE IF NOT EXISTS " +
databaseName;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection tempConn =
DriverManager.getConnection(Con_,User_,Password_);
String deleteDatabase = "DROP DATABASE IF EXISTS " + previousDatabase;
tempConn.createStatement().executeUpdate(deleteDatabase);
Files.write(Paths.get(databaseNameHolderPath),
databaseName.getBytes() , StandardOpenOption.TRUNCATE_EXISTING);
tempConn.createStatement().executeUpdate(createDatabase);
tempConn.close();
}

private static final void createTable() throws Exception {


Path tableCreator = Paths.get(tableCreatorS);
java.util.List<String> lines = Files.readAllLines(tableCreator);
lines.add(1," IF NOT EXISTS " + tableName + " ");
StringBuilder b = new StringBuilder();
for(String s : lines)
{
String tempS = s.replaceAll("\n","");
tempS = s.replaceAll("\t","");
b.append(tempS);
}
getConn().createStatement().executeUpdate(b.toString());
}
}

You might also like