package Database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DatabaseManager {

    private static DatabaseManager dbManager;
    private String drivername, databasename, username, password;
    private Connection conn = null;
    Statement stmt;

    private DatabaseManager(String props) {
        File file = new File(props);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            Properties prop = new Properties();
            prop.load(fis);
            //Enumeration enums = prop.propertyNames(); 
            drivername = (String) prop.get("Driver");
            databasename = (String) prop.get("URL");
            username = (String) prop.get("UserName");
            password = (String) prop.get("UserPass");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        try {
            Class.forName(drivername).newInstance();
            conn = DriverManager
                    .getConnection(databasename, username, password);
            stmt = conn.createStatement();

        } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
    
    public static Connection getTestConnection() {
        dbManager = new DatabaseManager("testdatabase.properties");
        return dbManager.conn;
    }

    public static Connection getConnection() {
        if (dbManager == null) {
            dbManager = new DatabaseManager("database.properties");
        }
        return dbManager.conn;
    }

    public ResultSet executeQuery(String sql) {
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static void close() {
        try {
            dbManager.conn.close();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        Connection conn = getConnection();
        int commitId;
        try {
            Statement stmt1 = conn.createStatement();
            Statement stmt2 = conn.createStatement();
            ResultSet r1 = stmt1.executeQuery("select id from scmlog");
            while (r1.next()) {
                commitId = r1.getInt(1);
                ResultSet r2 = stmt2
                        .executeQuery("select file_id from actions where commit_id =" + commitId);
                while (r2.next()) {
                    System.out.print(r2.getInt(1));
                }
            }
        } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }

    }



}
@@ -502,16 +502,37 @@
 nt stmt;
+%0A    String datafile;
 %0A%0A    pr
@@ -564,16 +564,92 @@
 ing 
-props) %7B
+file) %7B%0A        datafile = file;%0A    %7D%0A    %0A    public void createConnection()%7B%0A    
 %0A   
@@ -678,13 +678,16 @@
 ile(
-props
+datafile
 );%0A 
@@ -1655,24 +1655,26 @@
  password);%0A
+//
             
@@ -1813,32 +1813,1134 @@
    %7D%0A    %7D%0A    %0A
+    public void createTestConnection()%7B%0A        %0A        File file = new File(datafile);%0A        FileInputStream fis = null;%0A        try %7B%0A            fis = new FileInputStream(file);%0A            Properties prop = new Properties();%0A            prop.load(fis);%0A            //Enumeration enums = prop.propertyNames(); %0A            drivername = (String) prop.get(%22Driver%22);%0A            databasename = (String) prop.get(%22URL%22);%0A%0A        %7D catch (FileNotFoundException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D finally %7B%0A            if (fis != null)%0A                try %7B%0A                    fis.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A        %7D%0A%0A        try %7B%0A            Class.forName(drivername).newInstance();%0A            conn = DriverManager%0A                    .getConnection(databasename);%0A//            stmt = conn.createStatement();%0A%0A        %7D catch (Exception e) %7B%0A            System.out.println(e);%0A            System.exit(0);%0A        %7D%0A    %7D%0A    %0A    %0A
     public stati
@@ -3034,32 +3034,74 @@
 e.properties%22);%0A
+        dbManager.createTestConnection();%0A
         return d
@@ -3262,32 +3262,74 @@
 e.properties%22);%0A
+            dbManager.createConnection();%0A
         %7D%0A      

change DatabaseManager to create database connection to sqlite database