Dumping Tables

Question:
Using JDBC, how can I turn a database table into a series of SQL INSERT statements that can recreate the table?

Answer:
Dumping a database table into a set of insert statements is just amatter of selecting all of its rows and constructing insert statementsbased on their column values.

You can determine the number of columnsin a table by accessing the ResultSetMetaData for a ResultSet withgetMetaData() and then calling ResultSetMetaData.getColumnCount().Getting the values of each row is just a matter of calling getString()with the column number as an argument. The following programdemonstrates the approach:

import java.sql.*;/*** * This program accepts a table name, a JDBC driver name, and a database * connection URL as arguments.  The program connections to the database * and reads all the rows in the table, printing SQL INSERT statements * on standard output representing the data. ***/public final class dump {  public static String DUMP_QUERY = "SELECT * FROM ";  public static final void main(String[] args) {    Connection connection;    Statement statement;    ResultSet result;    ResultSetMetaData metaData;    String table, driver, url;    StringBuffer buffer = new StringBuffer(8192);    int columns;    if(args.length < 3) {      System.err.println("Usage: dump  ");      return;    }    table  = args[0];    driver = args[1];    url    = args[2];    System.setProperty("jdbc.drivers", driver);    try {      connection = DriverManager.getConnection(url);      statement  = connection.createStatement();      result = statement.executeQuery(DUMP_QUERY + table);      metaData = result.getMetaData();       columns = metaData.getColumnCount();      while(result.next()) {        buffer.setLength(0);        buffer.append("INSERT INTO ");        buffer.append(table);        buffer.append(" VALUES (");        for(int i = 1; i <= columns; i++) {          String str;          str = result.getString(i);          if(str == null)            buffer.append("NULL");          else {            buffer.append(''');            buffer.append(str);            buffer.append(''');          }          if(i < columns)            buffer.append(", ");          else            buffer.append(");");        }        System.out.println(buffer.toString());      }    } catch(SQLException e) {      e.printStackTrace();      return;    }  }}
		
				
				
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.

Sitemap