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;
		Connection connection=getConnection();
		boolean notexist = false;
		try
		{
			result=super.search(text);
		}
		catch(Exception e)
		{
			notexist = true;
		}
		if(!notexist)
		{
			return result;
		}
		else 
		{
			try 
			{
				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 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;
	}

}

@@ -337,32 +337,8 @@
 n %7B%0A
-%09%09String result = null;%0A
 %09%09Co
@@ -402,16 +402,40 @@
  false;%0A
+%09%09String result = null;%0A
 %09%09try%0A%09%09
@@ -445,17 +445,19 @@
 %09%09result
-=
+ = 
 super.se
@@ -1034,40 +1034,34 @@
 ;%0A%09%09
-throw new Exception(%22NOT FOUND%22)
+else%0A%09%09%09return %22Not Found%22
 ;%0A  

Update SQLDatabaseEngine.java