package app; /**
 * Created by Petr Flégl on 12. 7. 2016.
 */

import java.sql.*;

public class DbConnector {
    Connection conn = null;
    Statement stmt = null;

    public void connect(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://localhost/predictorDB","predictor","predictor");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e ){
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public String getResponse(String query){
        String consoleRet="";
        ResultSet res;
        try {
            if(conn!=null)
                stmt=conn.createStatement();
            if(stmt!=null) {
                res = stmt.executeQuery(query);
                ResultSetMetaData rsmd = res.getMetaData();
                while (res.next()) {
                    for (int i = 1; i <= rsmd.getColumnCount(); i++)
                        consoleRet += res.getString(i) + " | ";
                    consoleRet+="<br>";
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return consoleRet;
    }

}
