In this video we will learn how to use JDBC result set metadata.
Please SUBSCRIBE to this channel
Time – 00:00
Hi, this is Chad (Shod) with luv2code.com, welcome back to another tutorial on Java JDBC. In this video, we will learn how to use ResultSet Metadata. For this tutorial we will use the following table: employees. The table already has sample data for testing. I have a SQL script that creates the table and adds sample data. You can download it from the link below. The file is sql/table-setup.sgl.
Time – 00:30
ResultSet Metadata can give you information about your ResultSet so to get started with database Metadata you must first retrieve it from the ResultSet object. This is accomplished by calling ResultSet.getMetadata. Then once we have the ResultSet object, we can call methods on it to get the column names and types and also check to see if a column is auto-increment and also if a column is nullable and so on. There are a lot more methods available, see the Javadoc for details.
Time – 00:58
All right, so let’s go ahead and switch over to clips and see this in action. I have a very simple program called ResultSet demo and this program what I’ll do is I’ll first start off by getting a connection to the database, then I’ll create a statement and I’ll execute a query. In this example I have select ID, last name, first name, salary from employees, then once that query is executed I retrieve the ResultSet Metadata. Now that I have the Metadata, I can get information about it.
Time – 01:31
The first thing I’d like to do is get the actual column count and that’s the number of columns that we have in our query and we display it. Then I’d like to do a four loop and get information about each column. Remember in JDBC, all columns are one based so we start our four loop at the position one and then we move up to the column count.
Time – 01:50
The next line here I’ll print out the column name, then I’ll print out the column type name and so this will give me the database specific type name like varchar, INT or decimal. I’ll also check to see if a column is nullable, this will return a certain code if that value’s set accordingly. Then I’ll also check to see if a column has auto-increments set, this returns true or false. That’s pretty much it for the for loop.
Time – 02:27
Here’s the output of the application. I can see that we have four columns that were returned and this matches it with the query that we submitted. We have the information for the first column, ID, it’s an INT, it’s not nullable and auto-increments set to true. Last names are varchar, it is nullable, auto-increments set for false. A similar thing here for first name. Then finally, we have our salary, the column type is decimal, it’s nullable and auto-increments is set to false also. This looks pretty good.
Time – 02:58
What I’d like to do right now is move over to the MySQL tool and just verify some of this information. Here’s the actual database schema for the employees’ table. The first thing I’ll look at is the ID column and we can see that it’s of type INT, it’s non-nullable and also auto-increment and it’s the primary key for the table. We also see that we have last name, first name and those are varchars as was shown by the application and also salary we see it’s of type decimal so this really matches up with the information that was presented by the actual Java program.
Time – 03:31
For the ResultSet Metadata class, we have additional methods where you can get more details about it but this is enough information to get us started with ResultSet Metadata.
Time – 03:42
That wraps up our discussion on using ResultSet Metadata with JDBC. Please subscribe to our channel to view more videos on Java, click the thumbs up to like our video, also visit our website https://luv2code.com to download the Java source code used in this video.