package com.example.bot.spring;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.*;
import java.net.URISyntaxException;
import java.net.URI;

@Slf4j
public class SQLDatabaseEngine extends DatabaseEngine {
	@Override
	public String search(String text) throws Exception {
		String result=null;
		boolean exist=true;
		try{result=super.search(text);
				}catch(Exception e){exist=false;}
		if(exist){return result;}
		else {
			try {
				Connection connection=getConnection();
				PreparedStatement stmt=connection.prepareStatement(
				"SELECT response FROM chatbotdb WHERE keyword like concat('%', ?, '%')");
				stmt.setString(1, text);
				ResultSet rs=stmt.executeQuery();
				rs.next();
				result=rs.getString(1);
				rs.close();
				stmt.close();
				connection.close();}
			catch (Exception e) {
			log.info("Exception while reading file: {}", e.toString());
		} 
		}
		if (result != null)
			return result;
		throw new Exception("NOT FOUND");
    }
	private final String FILENAME = "/static/database.txt";

	
	
	private Connection getConnection() throws URISyntaxException, SQLException {
		Connection connection;
		URI dbUri = new URI(System.getenv("DATABASE_URL"));

		String username = dbUri.getUserInfo().split(":")[0];
		String password = dbUri.getUserInfo().split(":")[1];
		String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath() +  "?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory";

		log.info("Username: {} Password: {}", username, password);
		log.info ("dbUrl: {}", dbUrl);
		
		connection = DriverManager.getConnection(dbUrl, username, password);

		return connection;
	}

}

@@ -352,15 +352,58 @@
 sult
-=
+ = 
 null;%0A
+%09%09Connection connection=getConnection();%0A
 %09%09bo
@@ -408,22 +408,31 @@
 boolean 
+already
 exist
-=
+ = 
 true;%0A%09%09
@@ -434,17 +434,24 @@
 e;%0A%09%09try
-%7B
+%0A%09%09%7B%0A%09%09%09
 result=s
@@ -471,19 +471,20 @@
 ext);%0A%09%09
+%7D%0A
 %09%09
-%7D
 catch(Ex
@@ -493,17 +493,31 @@
 ption e)
-%7B
+%0A%09%09%7B%0A%09%09%09already
 exist=fa
@@ -524,22 +524,39 @@
 lse;
+%0A%09%09
 %7D%0A%09%09if(
-exist)%7B
+alreadyexist)%0A%09%09%7B%0A%09%09%09
 retu
@@ -565,16 +565,19 @@
  result;
+%0A%09%09
 %7D%0A%09%09else
@@ -577,16 +577,19 @@
 %0A%09%09else 
+%0A%09%09
 %7B%0A%09%09%09try
@@ -593,52 +593,13 @@
 try 
-%7B%0A%09%09%09%09Connection connection=getConnection();
+%0A%09%09%09%7B
 %0A%09%09%09
@@ -988,16 +988,17 @@
 g());%0A%09%09
+%09
 %7D %0A%09%09%7D%0A%09
@@ -1082,67 +1082,8 @@
   %7D%0A
-%09private final String FILENAME = %22/static/database.txt%22;%0A%0A%09
 %0A%09%0A%09

Update SQLDatabaseEngine.java