Join David Gassner for an in-depth discussion in this video Importing a MySQL database, part of Java Database Integration with JDBC.
- View Offline
To follow along with the exercises in this course, you'll need a local installation of MySQL and a copy of phpMyAdmin, a web-based management system that lets you manage a MySQL Database. You can select from a number of approaches to installing this software on your computer. I describe how to install this software in the course installing Apache, MySQL, and PHP, which is a completely free course available in the lynda.com online training library. In this course, I will describe how to install separate components on either Mac or Windows, or you can select from one of the many available software bundles which include Apache, MySQL, PHP, and phpMyAdmin.
These include WampServer for Windows, MAMP for Mac OS X, or XAMPP or BitNami, which are both available for both Windows and Mac. Throughout this course I'll be using MAMP, a software bundle specifically designed for use on Mac OS X. If you're using MAMP, there's one preferences thing you need to take care of. When you first install MAMP, you'll see that it's set ports of 8888 for Apache and 8889 for MySQL. It does this so you can run the copy of Apache included with the MAMP side by side with a copy of Apache included with the Mac.
But for this course, you'll need to run Apache and MySQL on their standard default ports. To do this, go to this Preferences dialog to the Ports tab and click Set to default Apache and MySQL ports, and you'll see that the my SQL port is set to the standard port of 3306. That's what Java and JDBC will expect. I'll click OK, wait for the servers to restart, and then click Open start page, and here I'll see that MySQL is running on the correct port of 3306.
If you're using any of the other configurations of Apache, MySQL, and PHP, this port should already be set for you. MAMP is the only software bundle that I'm aware of where you need to make this change. Now the next step, once you have confirmed that MySQL is running is to import a database that I'll be using throughout this course. I'll go to phpMyAdmin. If you're not sure where to find phpMyAdmin on your configuration, check the particular server bundle's documentation. Within phpMyAdmin, I'm going to create a database.
I'll go to the Databases tab, I'll click into the text box, and I'll type the name of the database I want to create, explorecalifornia. If you're following along, make sure that you spell the name of the database exactly this way, all lowercase and all one word, because all of the exercises throughout the course will expect it. Click Create, and now your database has been created. Now, I'm going to import an SQL script that's provided as part of the Free exercise files that accompany this course. I have copied by Free exercise files to the desktop, but you can find them wherever you installed them.
Within the Free exercise files there is a database folder, and within that folder there's an SQL file called exploreCalifornia.sql. This is a simple text file, and you can open it in any TextEditor. Within this SQL file there's a command to use explorecalifornia, the database that I have just created. And then the five tables that are a part of this database are dropped if they already exist. So if you want to refresh your database, you can just run this SQL script in the future, and it will delete all of the existing data and refresh the database table structure and content.
Little further down in the script, for each table there's a create statement that describes the structure of the database and a series of Insert statements that populate a table with data. There are tables named Admin, explorers, packages, tours, and states. And the states table has 50 rows. I'll close my TextEditor, and I'll return to phpMyAdmin. Now I'm ready to import my database. I'll click into the database and click the Import tab. I'll browse to select my SQL file.
I'll be sure to select the file with the file extension .sql. There are also files with extensions of .script and .properties, but those are for use with another Database Management System. I'll select the file and click Open, and then I'll scroll down to the bottom of the screen and click Go. If everything goes well, I'll see a message that the import has been successfully finished, and if I click into the database again I'll see my five tables have been created. For any of these tables, I can click into the name of the table. For example, I'll click into states, and I'll see the listing of all the data.
Just see the structure of the database table, including the names of the columns which is important to know when programming with JDBC. I'll click this Structure tab, and I can also click any of the other tabs to run SQL statements, Search, Insert data, Export, Import, and Execute other operations. So if you successfully followed through the steps, you'll have a working MySQL Server installation, and you'll have imported the sample database that I'll be using throughout this course.
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.
- 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