Java JDBC Tutorial – Part 9: JDBC ResultSet MetaData with MySQL

preview_player
Показать описание

---

---

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
Рекомендации по теме
Комментарии
Автор

Thank you for the useful demonstration in clearly spoken English.

bgiYouTubeChannel
Автор

thank you so much. i want to know, how i can to create a database if i want to read the metadata from an csv file. that means i have an csv file with the metadata and i want to read the metadata from the file and create a database or table. please help me. i need that for my college project.
thanks again

basselbakkar
Автор

As lucid as humanly possible. Thanks a ton.

mistery
Автор

Hello again =].
Thank you as always for the upload.

inadaizz
Автор

hi, i have a table of patients and i want to give to every one a unique ID, how can i do it by java, the id_pat in mysql DB is auto increment, but the id_textField don't get any auto value, i should insert it manually !

nagisafurukawa
Автор

how to change the result into tabular form? please reply I need this answer

medos
Автор

java.sql.SQLException: [Microsoft][SQL Server Native Client 11.0]Connection is busy with results for another command
help me to resolve this

mohandast
Автор

To convert 'isNullable' to a yes/no answer I did the following (within the for loop):
int result =
String answer = null;
if(result == 0)
{
answer = "no";
}
else
answer = "yes";
System.out.println("Is Nullable: " + answer );

If anyone has a better way to approach this, please let me know

EDIT: i used a ternary operator to shorten the if statement to:
answer = ((result == 0) ? "no":"yes");

clopes