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

Java Database Integration with JDBC
Illustration by Don Barnett

Connecting to a HyperSQL database file


From:

Java Database Integration with JDBC

with David Gassner

Video: Connecting to a HyperSQL database file

In order to provide a contrast between different database management systems, I'm going to be showing how to connect both to MySQL and to HyperSQL. HyperSQL is one of a number of 100% Java-based databases. Others include Apache Derby and H2. I have chosen a HyperSQL because its databases are defined in pure text files, which makes them very easy to use in a training environment. You can get the Drivers and all the documentation for HyperSQL from hsqldb.org. You'll see hyperSQL preferred to both by the name HyperSQL and HSQLDB. Just as with MySQL, you can download the Drivers for free, but I have provided the driver for HSQLdb as part of the free exercise files that accompany the course.
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

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

Connecting to a HyperSQL database file

In order to provide a contrast between different database management systems, I'm going to be showing how to connect both to MySQL and to HyperSQL. HyperSQL is one of a number of 100% Java-based databases. Others include Apache Derby and H2. I have chosen a HyperSQL because its databases are defined in pure text files, which makes them very easy to use in a training environment. You can get the Drivers and all the documentation for HyperSQL from hsqldb.org. You'll see hyperSQL preferred to both by the name HyperSQL and HSQLDB. Just as with MySQL, you can download the Drivers for free, but I have provided the driver for HSQLdb as part of the free exercise files that accompany the course.

I'll go to my libs folder, and I'll copy the file hsqlbd.jar to the clipboard, then I will return to Eclipse. I'm working with a copy of my first project, this one is called ConnectHSQLDB, and it's available in the exercise files, this starting version of the project is already set up for use with MySQL, and I'm going to make some changes so that it works with HSQLDB instead. I'll go to the libs folder and then I'll add the jar file and then just as I did with MySql, I'll add the Driver to the Build Path. Now I need a database.

With MySQL the database was defined in the server environment, with HSQLDB you define a database in a text file, a script, and you accompany that script with a properties file. And these are both pure text files, so you can edit them with any text editor. I have provided a starting sample database as part of the free exercise files. I'll go to the exercise files root, I'll go to the database folder, and I'll select and copy these two files explorecalifornia .properties and explorecalifornia.script.

Now I'll go back to Eclipse, and I'm going to create a new folder in the project, and I'll name that folder data, all lowercase. And then I'll paste my script files into place. I'm going to open the script file in a text editor, I'll right-click and select Open With > Text Editor, and then I'll expand the editor to full screen. This is a database script for HSQLDB, it has some configuration commands at the top, and then if you scroll down, you'll find commands that are defining the database.

Here there is a command to create a user named SA when you create a brand-new database for HSQLDB SA, or System Administrator is the default root, or system administrator ID, and down here there is a command to grant the DBA permissions to the SA user. Here there is a set of commands that are defining the structures of the databases. Just as with my MySQL database, their tables are named Admin, Explorers, Packages, States, and Tours. They have the same basic column names, Datatypes and Configurations, including Auto Incrementing Primary Keys on four of the five tables.

Scroll down further and you'll see the insert statements that are adding the data to the database. The other file that I pasted into place is called a properties file, I'll open that one also with a Text Editor, and I'll see that it just has some information about the nature of the database, which version of the HSQL database engine was used, when the database was created, the version number, and whether it's been modified. HSQLDB works at runtime by loading these files into memory when you first initialize the connection and then the database is maintained in memory.

You can run HSQLDB in either embedded mode or in server mode. I'll be using it an embedded mode so that the HSQLDB engine is running in the same Java process as my application. So now I have my HSQLDB driver, and I have my database, and I'm ready to connect to it. I'll go to the Main.class, and I'm going to change a few things. First of all, notice that my USERNAME and PASSWORD are still set to dbuser and dbpassword. I'll go back to my explorecalifornia.script file, and I'll add that user so that I can make exactly the same kind of call I did with MySQL.

I'll scroll up to the section where the users are being created, I'll copy and paste the create user command for SA, and I'll change the name of the new user from SA to dbuser. To make sure that the USERNAME and PASSWORD stay lowercase and match what I have in MySQL, I'll wrap them both in quotes. Now I'll add my new user to the DBA Permissions, I'll go down here and locate GRANT DBA to SA I once again copy and paste, I'll change the New Command so that I'm granting those permissions to dbuser, I'll save my changes to the script, and I'll go back to my Main.class.

Now in my Main.class, I do have to make a change to my CONN_STRING or URL. Just as with MySQL, you start the CONN_STRING with jdbc, but then instead of mysql, you type in hsqldb. Instead of localhost, you will be loading this database from the local disk so type in data the name of the folder, which is a subfolder of the project, and then you refer to your database by the name of the database as the first part of the two filenames explorecalifornia.script and .properties.

Don't include the file extensions ,only include the beginning part of the name. So now I have made my changes to the code, I'm using the same USERNAME and the same PASSWORD but a different connection string. I'll save my changes, and I'll run my application, and just as with MySQL, I see a message indicating that I have successfully connected. Now just like with MySQL, I'll make sure that I'm getting a message that's true. So I'll change my USERNAME to something that doesn't exist, and I'll try to connect again. And I get an exception named SQLInvalidAuthorizationSpecException, a different exception that I got with MySQL.

And this is an indication that when you're working with different databases, what's happening in the background with Java might differ in some very interesting ways. But I know that I connected successfully, so I'll return my code back to that state with the correct USERNAME, the correct PASSWORD, and the correct CONN_STRING. I'll connect again, and now I know that that code is working, and I'm able to successfully connect the HyperSQL or HSQLDB.

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

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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 Upgrade 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
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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.