Advertisement
Guest User

POSTConnection2

a guest
May 15th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. package SP4;
  2.  
  3. import java.sql.*;
  4.  
  5.  
  6.  
  7.                
  8. public class POSTConnection2 {
  9. public Connection connection;
  10.    
  11.     public void connect(int portNo, String userName, String password) {
  12.         // Establishing a PostgreSQL database connection
  13.         String databaseUrl = "jdbc:postgresql://localhost:" + 5432 + "/postgres";
  14.  
  15.         try {
  16.             connection = DriverManager.getConnection(databaseUrl, "postgres", "102030");
  17.             System.out.println("Connection established to: " + databaseUrl);
  18.         }
  19.         catch (Exception exception) {
  20.             System.out.println("Connection failed");
  21.             exception.printStackTrace();
  22.         }
  23.     }
  24.  
  25.  
  26.     public Object[][] retrieveThermo() {
  27.         String sql = "select outflowtemp, inflowtemp from sep.thermosp where DateTime =(select MAX(DateTime) FROM sep.thermosp)";
  28.         int ThermoDegree = 0;
  29.         ResultSet ThermoSet = null;
  30.         try {
  31.             Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  32.             ThermoSet = statement.executeQuery(sql);
  33.             ThermoSet.last();
  34.             ThermoDegree = ThermoSet.getRow();
  35.         } catch (SQLException e) {
  36.            
  37.             e.printStackTrace();
  38.            
  39.         }
  40.        
  41.        
  42.         Object[][] columnnames = new Object[ThermoDegree][3];
  43.         try {
  44.             ThermoSet.first();
  45.             for (int i=0;i<ThermoDegree;i++) {
  46.                 columnnames[i][0] = ThermoSet.getString(1);
  47.                 columnnames[i][1] = ThermoSet.getString(2);
  48.  
  49.                 ThermoSet.next();
  50.             }
  51.            
  52.             String header[] = { "", "Incoming temperature", "Outcoming temperature"};
  53.  
  54.         } catch (SQLException e) {
  55.             System.out.println("Error");
  56.             e.printStackTrace();
  57.         }
  58.        
  59.        
  60.         return columnnames;
  61.        
  62.     }
  63.    
  64.    
  65.  
  66.  
  67. public Object[][] retrieveForecast() {
  68.     String sql = "select * from public.forecast";
  69.  
  70.    
  71.    
  72.     int forecaster = 0;
  73.     ResultSet ForecastSet = null;
  74.     try {
  75.         Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  76.         ForecastSet = statement.executeQuery(sql);
  77.         ForecastSet.last();
  78.         forecaster = ForecastSet.getRow();
  79.        
  80.     } catch (SQLException e) {
  81.         e.printStackTrace();
  82.     }
  83.    
  84.     Object[][] WeatherForecast = new Object[forecaster][3];
  85.     try {
  86.         ForecastSet.first();
  87.         for (int i=0;i<forecaster;i++) {
  88.             WeatherForecast[i][0] = ForecastSet.getString(1);
  89.             WeatherForecast[i][1] = ForecastSet.getString(2);
  90.             WeatherForecast[i][2] = ForecastSet.getString(3);
  91.  
  92.             ForecastSet.next();
  93.         }
  94.        
  95.        
  96.  
  97.     } catch (SQLException e) {
  98.         System.out.println("Error");
  99.         e.printStackTrace();
  100.     }
  101.    
  102.     return WeatherForecast;
  103. }
  104. public Object[][] retrievePastWeather() {
  105.     String sql = "select * from sep.pastweather";
  106.    
  107.     int PastWeather = 0;
  108.     ResultSet PastWeatherSet = null;
  109.     try {
  110.         Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  111.         PastWeatherSet = statement.executeQuery(sql);
  112.         PastWeatherSet.last();
  113.         PastWeather = PastWeatherSet.getRow();
  114.        
  115.     } catch (SQLException e) {
  116.         e.printStackTrace();
  117.     }
  118.    
  119.     Object[][]HistoricalWeather= new Object[PastWeather][3];
  120.     try {
  121.         PastWeatherSet.first();
  122.         for (int i=0;i<PastWeather;i++) {
  123.             HistoricalWeather[i][1] = PastWeatherSet.getString(1);
  124.             HistoricalWeather[i][2] = PastWeatherSet.getString(2);
  125.             HistoricalWeather[i][2] = PastWeatherSet.getString(3);
  126.  
  127.            
  128.            
  129.             PastWeatherSet.next();
  130.     }
  131.        
  132.    
  133.  
  134.     } catch (SQLException e) {
  135.         e.printStackTrace();
  136.     }
  137.    
  138.     return HistoricalWeather;
  139. }
  140.  
  141.  
  142.  
  143. public Object[][] retrievePastElectrical() {
  144.     String sql = "select watt from sep.ElectPanel";
  145.    
  146.     int Elect = 0;
  147.     ResultSet ElectSet = null;
  148.     try {
  149.         Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  150.         ElectSet = statement.executeQuery(sql);
  151.         ElectSet.last();
  152.         Elect = ElectSet.getRow();
  153.        
  154.     } catch (SQLException e) {
  155.         e.printStackTrace();
  156.     }
  157.    
  158.     Object[][]HistoricalElect = new Object[Elect][3];
  159.     try {
  160.         ElectSet.first();
  161.         for (int i=0;i<Elect;i++) {
  162.             HistoricalElect[i][1] = ElectSet.getString(1);
  163.            
  164.             ElectSet.next();
  165.     }
  166.        
  167.        
  168.  
  169.     } catch (SQLException e) {
  170.         e.printStackTrace();
  171.     }
  172.    
  173.     return HistoricalElect;
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement