Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SP4;
- import java.sql.*;
- public class POSTConnection2 {
- public Connection connection;
- public void connect(int portNo, String userName, String password) {
- // Establishing a PostgreSQL database connection
- String databaseUrl = "jdbc:postgresql://localhost:" + 5432 + "/postgres";
- try {
- connection = DriverManager.getConnection(databaseUrl, "postgres", "102030");
- System.out.println("Connection established to: " + databaseUrl);
- }
- catch (Exception exception) {
- System.out.println("Connection failed");
- exception.printStackTrace();
- }
- }
- public Object[][] retrieveThermo() {
- String sql = "select outflowtemp, inflowtemp from sep.thermosp where DateTime =(select MAX(DateTime) FROM sep.thermosp)";
- int ThermoDegree = 0;
- ResultSet ThermoSet = null;
- try {
- Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- ThermoSet = statement.executeQuery(sql);
- ThermoSet.last();
- ThermoDegree = ThermoSet.getRow();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Object[][] columnnames = new Object[ThermoDegree][3];
- try {
- ThermoSet.first();
- for (int i=0;i<ThermoDegree;i++) {
- columnnames[i][0] = ThermoSet.getString(1);
- columnnames[i][1] = ThermoSet.getString(2);
- ThermoSet.next();
- }
- String header[] = { "", "Incoming temperature", "Outcoming temperature"};
- } catch (SQLException e) {
- System.out.println("Error");
- e.printStackTrace();
- }
- return columnnames;
- }
- public Object[][] retrieveForecast() {
- String sql = "select * from public.forecast";
- int forecaster = 0;
- ResultSet ForecastSet = null;
- try {
- Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- ForecastSet = statement.executeQuery(sql);
- ForecastSet.last();
- forecaster = ForecastSet.getRow();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Object[][] WeatherForecast = new Object[forecaster][3];
- try {
- ForecastSet.first();
- for (int i=0;i<forecaster;i++) {
- WeatherForecast[i][0] = ForecastSet.getString(1);
- WeatherForecast[i][1] = ForecastSet.getString(2);
- WeatherForecast[i][2] = ForecastSet.getString(3);
- ForecastSet.next();
- }
- } catch (SQLException e) {
- System.out.println("Error");
- e.printStackTrace();
- }
- return WeatherForecast;
- }
- public Object[][] retrievePastWeather() {
- String sql = "select * from sep.pastweather";
- int PastWeather = 0;
- ResultSet PastWeatherSet = null;
- try {
- Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- PastWeatherSet = statement.executeQuery(sql);
- PastWeatherSet.last();
- PastWeather = PastWeatherSet.getRow();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Object[][]HistoricalWeather= new Object[PastWeather][3];
- try {
- PastWeatherSet.first();
- for (int i=0;i<PastWeather;i++) {
- HistoricalWeather[i][1] = PastWeatherSet.getString(1);
- HistoricalWeather[i][2] = PastWeatherSet.getString(2);
- HistoricalWeather[i][2] = PastWeatherSet.getString(3);
- PastWeatherSet.next();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return HistoricalWeather;
- }
- public Object[][] retrievePastElectrical() {
- String sql = "select watt from sep.ElectPanel";
- int Elect = 0;
- ResultSet ElectSet = null;
- try {
- Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- ElectSet = statement.executeQuery(sql);
- ElectSet.last();
- Elect = ElectSet.getRow();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Object[][]HistoricalElect = new Object[Elect][3];
- try {
- ElectSet.first();
- for (int i=0;i<Elect;i++) {
- HistoricalElect[i][1] = ElectSet.getString(1);
- ElectSet.next();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return HistoricalElect;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement