advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Data Mover Example
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 4.7/5 | Rate this item | 6 users have rated this item.
Email this articleEmail this article
 
How To Copy Database Data Using JDBC
Copying database data is a basic, yet endlessly complex task, because not all data copy operations are alike. But the sample database-copy classes you'll find here provide a basic template for nearly every type of copy you need to perform. 

advertisement
f you work with databases, you'll often need to copy data from one database to another. While that sounds like a straightforward task, it often involves more than just a straightforward copy. For example, you may need to move only part of the data, or to modify the data as it is copied. This article demonstrates a set of classes that perform basic database copies—moving tables from one database to another—using a simple application I've called a "data mover." While the example is only a starting point that shows how to copy an entire database, you'll see that you can modify the code to perform customized data copy and transformation operations, such as copying individual tables or specific table rows or columns based on your individual needs.


For novices, this article also serves as a good introduction to Java Database Connectivity (JDBC), by showing you how to perform the following JDBC tasks:

  • Open a connection to a database
  • Create and submit queries
  • Process ResultSets returned by queries
In addition to demonstrating the basic use of JDBC, this article also shows how to query a database and gather information about the database's structure. This allows the data mover utility to gather enough information about the database to be able to copy it. Here's the four-step process that the data mover follows:

  • Generate SQL CREATE TABLE statements to create target tables
  • Generate SQL to drop existing tables
  • Generate SQL to SELECT from the source tables
  • Generate SQL to INSERT into the target tables
Before I show you exactly how to program the data mover application, I will first show you how to use it to copy a database.

Using the DataMover to Copy a Database
Included with the downloadable code you'll find a simple utility called DataMoverUtility. This utility reads a configuration file and copies data from the source database to the target database.

WARNING: As written, this utility overwrites existing data in the target database. In other words, as the utility copies tables to the target database, any tables with the same name will be deleted.

To run the utility, you must first create a configuration file. This file will contain four lines of data. For example, to test the data mover I used the following configuration file.

   sourceDriver=com.mysql.jdbc.Driver
   sourceURL=jdbc:mysql://127.0.0.1/test?user=root
   targetDriver=com.mysql.jdbc.Driver
   targetURL=jdbc:mysql://127.0.0.1/test2?user=root

The preceding configuration file tells the data mover utility to copy from one MySQL database to another MySQL database. It assumes that both of these databases are on your local computer (127.0.0.1). For every database in JDBC you must specify a driver and a connection URL. The sourceDriver and sourceURL specify the driver and URL of the source database. Likewise, the targetDriver and targetURL specify that information for the target database.

When you run the data mover utility you should specify the configuration file as the first parameter, for example:

   java DataMoverUtility c:\export.txt 

Using MySQL with Java
Before you can compile and use JDBC applications you must install the driver for the database you would like to use. Normally this is just a matter of adding the driver's JAR file to the classpath of your project.

For this article I will assume that you are using MySQL; however, the code presented here is very compatible with most databases. Anything that might be specific to MySQL has been confined to the MySQL.java class file. To use other databases simply extend the Database class and add any code that is custom to that database.

MySQL uses the Connector/J driver, which you can download from www.mysql.org. The package includes a JAR file, which you should add to your classpath. After doing that, you're ready to connect to a MySQL database.

Data Mover Structure
The data mover consists of two main packages. The package, named com.heatonresearch.datamover contains the DataMover and DataMoverUtility classes. The DataMover class contains several reusable functions that copy tables from one database to another or serve as the starting point for more advanced operations. The DataMoverUtility class is an example of how to use the classes provided by this article.

The package named com.heatonresearch.datamover.db contains all the lower-level database classes. The three classes provided in this article are DatabaseException, Database, and MySQL. The DatabaseException class defines an exception that the data mover throws whenever the it encounters a database problem. The Database class provides all the base functions for database access. The MySQL class inherits from the Database class and implements the MySQL-specific code. You can extend this class library to work with other databases by creating new database classes that descend from the Database class, similar to the MySQL class.

Connecting to MySQL
Before performing any operations you must connect to the database. To connect to any database through JDBC you must know two things: the JDBC driver name, and the database URL.

The JDBC driver name is just a class name. For MySQL, the class is provided by the MySQL JAR file that you installed earlier. The URL specifies the name of the database, where it is, and what user to use. To open a connection to the database, you should use the connect method of the Database class. The following lines of code are from that method and illustrate how to connect to the database.

   try
   {
     Class.forName(driver).newInstance();
     connection = DriverManager.getConnection(url);
   } catch (InstantiationException e)
   {
     throw new DatabaseException(e);
   } catch (IllegalAccessException e)
   {
     throw new DatabaseException(e);
   } catch (ClassNotFoundException e)
   {
     throw new DatabaseException(e);
   } catch (SQLException e)
   {
     throw new DatabaseException(e);
   }

The first two lines of code process the driver and the URL. The remaining lines of code handle the numerous exceptions that can occur when opening a database. If any exceptions do occur, they are packaged as a DatabaseException and thrown.

  Next Page: Accessing Table and Catalog Information


Page 1: IntroductionPage 3: Generating a CREATE TABLE Statement
Page 2: Accessing Table and Catalog InformationPage 4: Copying the Data
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES