The database I will use is BB two on IBM Cloud. We will connect to the database using our JDBC. For this the libraries we will require from our our our Java, please note the case DBI and our JDBC. Now I show you the DB two database instance. I already opened it up for you. Now first we see the table Select star from TV prizes.
Let's see the data. So here you can see the data. And let's just order it by order by observation date. vs date in the descending order. Now let's run it. You see we have data tails ready stuff.
May 2018, the first thing to do is to invoke the libraries. We already showed you how to do this in the previous part. Now, we just I have copied the code for you, I just highlight the code and I run it there you see the libraries I invoke. To read the data from database, we will require the database user ID and password. So I have stored it in a CSV file, I already showed you how to read CSV files. So I just copied the code I highlighted and I run it now the passwords have been read.
I'm providing you the code you will require for connecting to the database. Now the values of the variables will differ from the database which you are using. This these are the values which are required for connecting to my instance of dB two on IBM Cloud, however, these will differ depending on which database you're using, whether it's Oracle Informix or Sybase, etc. And also, the hostname will be different as well as your user ID passwords will definitely be different, but the construct of the connection code is going to be the same. So, once you provide the values appropriately, you can use this port for connecting to your database. Next we will see how we can use this code to connect to the database instance which I am using right now.
So, as I told you, I am copying this code, I have copied it here now I run it. So you see the code has run successfully. Now let us check whether the connection object has been created properly. For that, I just say co nn the connection object and you see it's a JDBC connection. Now that we have connected to the database, let us fetch some data and start working on the data. So here I provided the code what that is required for fetching data from the database.
First of all, we are required to specify the query which we are going to use for fetching The data. So here I am fetching the data from TV prices. And I'm altering it by opps observation date in descending order. Once the query has been formed, we already have the connection object that we saw previously. So we need to use a function. dB set query give the connection object and the query.
Once the Connect once this command has been executed, then we can use the object RS to fetch the data to be when the fetch function, if you specify minus one it fetches all the rows, or you can replace it with a specific number as to how many number of rows that you want to fetch. The fetch data is stored in a data frame, which we call this df. Now we run this code. So I highlight it and I run it. So now you see the data has been fetched. there we'll see the data we shall see the structure of the data which is the RDF.
So there you see there are nine 72 observations in seven variables and notice the variable names. They are all in capitals as for the database schema. Now we say head, df and see the top of the data. So there you have the top of the data