Start learning with our library of video tutorials taught by experts. Get started

Java Database Integration with JDBC

Getting column and data type information


From:

Java Database Integration with JDBC

with David Gassner
Expand all | Collapse all
  1. 5m 23s
    1. Welcome
      53s
    2. What you should know before starting this course
      1m 20s
    3. Using the exercise files
      3m 10s
  2. 12m 2s
    1. Testing your Java development environment
      5m 14s
    2. Importing a MySQL database
      5m 1s
    3. Creating a database user in MySQL
      1m 47s
  3. 32m 47s
    1. What is JDBC?
      4m 26s
    2. Choosing a JDBC driver
      6m 46s
    3. Connecting to a MySQL database server
      8m 7s
    4. Connecting to a HyperSQL database file
      6m 23s
    5. Executing a static SQL statement
      7m 5s
  4. 17m 42s
    1. Connecting to multiple databases
      6m 24s
    2. Handling JDBC exceptions
      7m 7s
    3. Closing database resources in Java 7
      4m 11s
  5. 47m 25s
    1. Looping through result sets
      8m 23s
    2. Moving the cursor in scrollable result sets
      5m 51s
    3. Limiting the number of fetched rows
      6m 57s
    4. Filtering data with prepared statements
      6m 58s
    5. Calling stored procedures
      5m 48s
    6. Handling multiple values from stored procedures
      5m 54s
    7. Using generic getter methods in Java SE 7
      7m 34s
  6. 45m 23s
    1. Managing data entities with JavaBean classes
      5m 0s
    2. Retrieving a single row as a JavaBean object
      6m 5s
    3. Inserting rows with prepared statements
      8m 2s
    4. Updating rows with prepared statements
      5m 4s
    5. Deleting rows with prepared statements
      4m 9s
    6. Managing data with updatable result sets
      6m 6s
    7. Using a persistent database connection
      6m 43s
    8. Committing and rolling back transactions
      4m 14s
  7. 9m 35s
    1. Getting the DatabaseMetaData object
      3m 40s
    2. Getting column and data type information
      5m 55s
  8. 50s
    1. Next steps
      50s

Video: Getting column and data type information

I have previously described how to use the database metadata class to get a list of tables from a database. I'm working in a new version of that project named TablesAndColumns where I already have the code to get the list of tables. I'll test it and make sure that I'm successfully retrieving that information. Now I'm going to show you how to get more information from the metadata object, including the names and data types of the database columns. I'll start by re-factoring this code. Instead of immediately outputting the name of each table, I'm going to store the names of the tables in an array list.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Java Database Integration with JDBC
2h 51m Intermediate Nov 28, 2012

Viewers: in countries Watching now:

Whether you're building a web- or desktop-based application with Java SE or Java EE, many Java applications need to integrate data from a relational database. This course describes how to read and manage data from relational databases such as MySQL and SQL Server using the Java Database Connectivity (JDBC) API.

Author David Gassner explains how to choose a JDBC driver and connect to one or more databases. He also provides detailed instructions on reading, selecting, and updating data; calling stored procedures; managing data via JavaBean classes or with prepared statements; and working with metadata.

Topics include:
  • Importing a MySQL database
  • Connecting to databases
  • Handling JDBC exceptions
  • Looping through result sets
  • Limiting the number of fetched rows
  • Filtering data with prepared statements
  • Calling stored procedures
  • Inserting, updating, and deleting rows with prepared statements
  • Using a persistent database connection
  • Committing and rolling back transactions
Subjects:
Developer Databases Programming Languages
Software:
Java
Author:
David Gassner

Getting column and data type information

I have previously described how to use the database metadata class to get a list of tables from a database. I'm working in a new version of that project named TablesAndColumns where I already have the code to get the list of tables. I'll test it and make sure that I'm successfully retrieving that information. Now I'm going to show you how to get more information from the metadata object, including the names and data types of the database columns. I'll start by re-factoring this code. Instead of immediately outputting the name of each table, I'm going to store the names of the tables in an array list.

I'll start by declaring an array list at the top of the code, I will place my cursor before the try catch block, and I will declare my ArrayList, and I will set its data type to String, that is that's the data type of the items it will contain. And I will name it tables. I will instantiate it with the ArrayList class's no arguments constructor. Now I'll go down to the while loop, where I am looping through the result set of the tables. I'm still going to need the names of the tables, so I'll select that code and copy it to the clipboard, and then I'll delete that line.

Then I'll replace that code within the while loop with a call to the array list add method using tables.add, and I will paste in that code that was getting the table name already. So now I have a resizable array, an array list containing the names of the tables, and I can loop through that and get the columns for the tables one at a time. To contain that information, I'm going to need another result set. So I will move back up to where I'm declaring my local variables within the main method, and I'll declare another result set, this one called rsColumns, and I will initially set it to null.

Now I will move back to after the while loop, and I'm ready to process the tables. I will make some extra space. And to loop through the array list, I'll use a foreach code template. I'll type in foreach and press Ctrl+Space, and I'll choose that code template, and I will name the variable that's going to be generated each time through the loop, tableName. I'm going to be looping through the tables collection. So I will make that change as well. On each time through the table, I will first output the name of the table. I'm going to need three system output lines.

So I will type in System.out and press Ctrl+Space, and then I will duplicate that line a couple of times. For the first system output, I will output the name of the table prefixed with a label. I will type a label of Table: and a space, and I will append to that the tableName. The second output will be a string of dashes, that will separate the tableName from the columns, and the third one will just be a blank string, and I will output in the column names before that blank string, so I will make some extra space there and here is where we will put the code to get the list of the columns.

I have already declared my result set where I will contain the list of the columns. I named it rsColumns. So now I will get its reference. I will use rsColumns =, then just as I did for the tables, I will use a method of the metadata object. It will be metadata.getColumns. The getColumns method is very similar to the getTables method. It asks for four arguments. As with getTables, the catalog can be left to null because I have already designated the catalog or database I am working with in my connection string.

The schemaPattern can be set to a wildcard. The tableNamePattern should be the specific table that you are querying. So I'm going to pass in my variable tableName and the columnNamePattern can be a wildcard because I'm asking for all of the columns for the current table. So now my result set will contain all of the information I need about my columns. The result set will contain a bunch of columns, and you should take a look at the documentation for this method to see what the column names are, but the particular column names I'm interested in are called column_name and type_name.

I'm going to loop through the result set using a while loop with a condition. I will set my condition to a call to the rsColumn.next method. That will be rsColumns.next. Within the loop I will create a StringBuffer. I will name it buffer, and I will instantiate it with the no arguments constructor. I'll start by appending the name of the column, I'll use rsColumns.getString, and I will pass in the name of the column which will be COLUMN_NAME. I'll duplicate that line of code, and I'll change the second one from COLUMN_NAME to TYPE_NAME.

And in between, I will separate these bits of output with a colon and a space. Finally, I'll add some system output, and I'll output the value of the two string method of the buffer object. I will clean up the extra white space, and now I'm ready to test my application again. Let's review the code. I'm looping through the array list of tables. On each time through the loop I am outputting the name of the table and a separator string. Then I'm getting the list of columns just for that table and then I'm doing another loop, this time through the result set of columns.

I create a string buffer, and I append to that the name of the column, a separator and the name of the datatype and then I output that information. I'll move the cursor down to the finally block and do one last little bit of cleanup. I will close the rsColumns object, and now I'll run my code, and I get back exactly the list I am looking for. For each table, I'm displaying the name of the table and then after the separator the names and data types of each of the columns. As I have mentioned previously, look at the documentation for the getColumns method, you'll see a listing of all of the names of the columns that are returned in this result set.

You'll find that you can get the names of the columns, the lengths of the columns, and all of the other information you might need and look thoroughly at what else is available in the database metadata object, it will return information about stored procedures, functions, and pretty much everything that you have stored in your database.

There are currently no FAQs about Java Database Integration with JDBC.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Java Database Integration with JDBC.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked