filmov
tv
Java JDBC Tutorial – Part 9: JDBC ResultSet MetaData with MySQL
Показать описание
---
---
In this video tutorial we will learn how to retrieve JDBC result set metadata with MySQL.
---
Transcript
Time - 00:00
Time - 00:30
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
Комментарии