Advertisement
NepityNeps

api to db2

Dec 15th, 2023 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 9.69 KB | Software | 0 0
  1. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  2.         Dim apiKey As String = "580997entTbou3Z7n0EQfucEV6eTYZiU"
  3.         Dim latitude As String = "12.8797" ' Philippines' latitude
  4.        Dim longitude As String = "121.7740" ' Philippines' longitude
  5.  
  6.         Dim apiEndpoint As String = "https://api.tomorrow.io/v4/timelines?location=" & latitude & "," & longitude & "&fields=temperature,windSpeed&apikey=" & apiKey
  7.  
  8.         Try
  9.             Using client As New HttpClient()
  10.                 Dim response = client.GetAsync(apiEndpoint).Result
  11.  
  12.                 If response.IsSuccessStatusCode Then
  13.                     Dim responseData = response.Content.ReadAsStringAsync().Result
  14.                     Dim json = JObject.Parse(responseData)
  15.  
  16.                     Dim weatherForecasts As New List(Of WeatherForecast)()
  17.  
  18.                     ' Extract weather forecast data for 12 PM each day for the next 3 days
  19.                    Dim intervals = json.SelectToken("data.timelines[0].intervals")
  20.                     Dim filteredIntervals = intervals.Where(Function(interval) Date.Parse(interval.SelectToken("startTime").ToString()).TimeOfDay = TimeSpan.FromHours(12)).Take(5)
  21.  
  22.                     ' Database connection
  23.                    WeatherDBConn = New DB2Connection("server=localhost;database=CCS6DB;" + "uid=db2admin;password=db2admin;")
  24.                     WeatherDBConn.Open()
  25.  
  26.                     Dim deleteQuery As String = "DELETE FROM weatherdata "
  27.                     Dim cmdDelete As New DB2Command(deleteQuery, WeatherDBConn)
  28.                     Dim resetQuery As String = "ALTER TABLE weatherdata ALTER COLUMN ID RESTART WITH 1"
  29.                     Dim cmdReset As New DB2Command(resetQuery, WeatherDBConn)
  30.                     cmdReset.ExecuteNonQuery()
  31.                     cmdDelete.ExecuteNonQuery()
  32.  
  33.                     ' Insert new data into the database
  34.                    For Each interval In filteredIntervals
  35.                         Dim weatherForecast As New WeatherForecast()
  36.  
  37.                         ' Extract required data
  38.                        weatherForecast.Temperature = interval.SelectToken("values.temperature").ToObject(Of Double)()
  39.                         weatherForecast.WindSpeed = interval.SelectToken("values.windSpeed").ToObject(Of Double)()
  40.                         Dim valuesToken = interval.SelectToken("values")
  41.                         If valuesToken IsNot Nothing Then
  42.                             Dim weatherCodeToken = interval.SelectToken("weatherCode")
  43.                             If weatherCodeToken IsNot Nothing Then
  44.                                 weatherForecast.weatherCode = weatherCodeToken.ToObject(Of Integer)()
  45.                             Else
  46.                                 MsgBox("Weather code not found in JSON.")
  47.                             End If
  48.                         End If
  49.                         weatherForecast.DateAndTime = Date.Parse(interval.SelectToken("startTime").ToString())
  50.  
  51.  
  52.                         ' Add WeatherForecast object to the list
  53.                        weatherForecasts.Add(weatherForecast)
  54.  
  55.                         ' Insert the data into the DB2 database
  56.                        Dim insertQuery As String = "INSERT INTO WeatherData (Temperature, WindSpeed, ForecastDate, Weathertype) VALUES (?, ?, ?, ?)"
  57.                         Using insertCmd As New DB2Command(insertQuery, WeatherDBConn)
  58.                             insertCmd.Parameters.Add("@Temperature", DB2Type.Double).Value = weatherForecast.Temperature
  59.                             insertCmd.Parameters.Add("@WindSpeed", DB2Type.Double).Value = weatherForecast.WindSpeed
  60.                             insertCmd.Parameters.Add("@ForecastDate", DB2Type.Date).Value = weatherForecast.DateAndTime
  61.                             If weatherForecast.weatherCode = 1000 Then
  62.                                 Dim weathertype As String = "Sunny"
  63.                                 insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
  64.                                 insertCmd.ExecuteNonQuery()
  65.                             ElseIf weatherForecast.weatherCode = 1001 Then
  66.                                 Dim weathertype As String = "Cloudy"
  67.                                 insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
  68.                                 insertCmd.ExecuteNonQuery()
  69.                             ElseIf weatherForecast.weatherCode = 4001 Then
  70.                                 Dim weathertype As String = "Rainy"
  71.                                 insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
  72.                                 insertCmd.ExecuteNonQuery()
  73.                             Else
  74.                                 Dim weathertype As String = "unknown"
  75.                                 insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
  76.                                 insertCmd.ExecuteNonQuery()
  77.                             End If
  78.                         End Using
  79.                     Next
  80.  
  81.                     ' Use weather data (for demonstration)
  82.                    For Each forecast In weatherForecasts
  83.                         Dim displayData As String = "Temperature: " & forecast.Temperature & ", Wind Speed: " & forecast.WindSpeed & ", Date: " & forecast.DateAndTime & ", Weather Code: " & forecast.weatherCode
  84.                         MsgBox(displayData)
  85.                     Next
  86.                 Else
  87.                     MsgBox("Failed to fetch weather data. Status code: " & response.StatusCode)
  88.                 End If
  89.             End Using
  90.         Catch ex As Exception
  91.             MsgBox(ex.ToString())
  92.         Finally
  93.             WeatherDBConn.Close()
  94.         End Try
  95.     End Sub
  96.  
  97.  
  98.  
  99. Json -------------------------------------
  100.  
  101. {"timelines":{"minutely":[{"time":"2023-12-16T01:16:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1,"dewPoint":2.69,"freezingRainIntensity":0,"humidity":65,"precipitationProbability":0,"pressureSurfaceLevel":1021.67,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.88,"temperatureApparent":8.88,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.19,"windSpeed":3}},{"time":"2023-12-16T01:17:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.08,"dewPoint":2.65,"freezingRainIntensity":0,"humidity":65.1,"precipitationProbability":0,"pressureSurfaceLevel":1021.66,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.82,"temperatureApparent":8.82,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.17,"windSpeed":3}},{"time":"2023-12-16T01:18:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.16,"dewPoint":2.61,"freezingRainIntensity":0,"humidity":65.2,"precipitationProbability":0,"pressureSurfaceLevel":1021.64,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.76,"temperatureApparent":8.76,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.15,"windSpeed":3}},{"time":"2023-12-16T01:19:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.24,"dewPoint":2.58,"freezingRainIntensity":0,"humidity":65.31,"precipitationProbability":0,"pressureSurfaceLevel":1021.62,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.7,"temperatureApparent":8.7,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.14,"windSpeed":3}},{"time":"2023-12-16T01:20:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.32,"dewPoint":2.54,"freezingRainIntensity":0,"humidity":65.41,"precipitationProbability":0,"pressureSurfaceLevel":1021.61,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.65,"temperatureApparent":8.65,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.12,"windSpeed":3}},{"time":"2023-12-16T01:21:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.39,"dewPoint":2.5,"freezingRainIntensity":0,"humidity":65.51,"precipitationProbability":0,"pressureSurfaceLevel":1021.59,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.59,"temperatureApparent":8.59,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.1,"windSpeed":3}},{"time":"2023-12-16T01:22:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.47,"dewPoint":2.46,"freezingRainIntensity":0,"humidity":65.61,"precipitationProbability":0,"pressureSurfaceLevel":1021.58,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.53,"temperatureApparent":8.53,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.09,"windSpeed":2.99}},{"time":"2023-12-16T01:23:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.55,"dewPoint":2.43,"freezingRainIntensity":0,"humidity":65.71,"precipitationProbability":0,"pressureSurfaceLevel":1021.56,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.48,"temperatureApparent":8.48,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.07,"windSpeed":2.99}},{"time":"2023-12-16T01:24:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.63,"dewPoint":2.39,"freezingRainIntensity":0,"humidity":65.82,"precipitationProbability":0,"pressureSurfaceLevel":1021.54,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.42,"temperatureApparent":8.42,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.05,"windSpeed":2.99}},
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement