Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
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.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.