Wednesday, January 23, 2008

Database dump with Java

I need to update a database that is created by PHP. The problem is that I am not a PHP coder, but a Java coder, and I need to use some other Java libraries to get the job done. So how can find out exactly which tables to update and how? It would take me weeks to search the PHP code, and I still wouldn't be sure if I got it right.

The first step is to install a clean application on my computer. There is no user data in the database, so if I perform commands like creating a user etc in the web application, I can look at what changed in the database. I'm sure that could be done in MySQL, but I'm not an expert on that either. When the only tool you have is a hammer, everything looks like a nail. So, I'll use Java for that to.

So, I wrote a small Java application that produces exactly the output that I need. It reads metadata from the database to find all tables and columns, lists that metadata and the content of all the rows.

Here it is:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class DumpDatabase {
private Connection conn;
private PrintWriter out;

public static void main(String[] args) {
try {
DumpDatabase app = new DumpDatabase();
String timestamp = new SimpleDateFormat("MMdd-hhmmss")
.format(new Date());
String fileName = "dumpdatabase-" + timestamp + ".txt";
System.out.println("Creating " + fileName);
} catch(Exception e) {

public DumpDatabase() throws IOException,
ClassNotFoundException, SQLException {
// Get database connection from
// Or hard-code your own JDBC connection if desired.
InputStream in = getClass().getResourceAsStream(
Properties properties = new Properties();
String driver = properties.getProperty(
String url = properties.getProperty(
String user = properties.getProperty(
String password = properties.getProperty(

conn = DriverManager.getConnection(url, user, password);

public void dumpDatabase(String fileName)
throws FileNotFoundException, SQLException {
out = new PrintWriter(fileName);

public void listAll() throws SQLException {
DatabaseMetaData metadata = conn.getMetaData();
String[] types = { "TABLE" };
ResultSet rs = metadata.getTables(
null, null, null, types);
while( {
String tableName = rs.getString("TABLE_NAME");

private void listTable(String tableName) throws SQLException {
PreparedStatement statement = conn.prepareStatement(
"select * from " + tableName + " a");
out.println("----" + tableName + "----");
int rowNo = 0;
ResultSet rs = statement.executeQuery();
while( {
if (rowNo == 0)

private void printTableColumns(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
for(int i = 0; i < metaData.getColumnCount(); i++) {
int col = i + 1;
out.println(metaData.getColumnName(col) + " "
+ metaData.getColumnTypeName(col) + " " + "("
+ metaData.getPrecision(col) + ")");

private void printResultRow(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
for(int i = 0; i < metaData.getColumnCount(); i++) {
String column = metaData.getColumnName(i + 1);
try {
String value = rs.getString(column);
if (value != null && !value.equals("null")
&& !value.equals("") && !value.equals("0"))
out.print(column + ": " + value + ", ");
catch(SQLException e) {
out.print(column + ": " + e.getMessage());

The application reads the JDBC properties from an file, for example like this:

It creates a file like dumpdatabase-01-23-10-30-38.txt:
user_id BIGINT (20)
username VARCHAR (255)
password VARCHAR (255)
first_name VARCHAR (255)
last_name VARCHAR (255)
email VARCHAR (255)


Then, I create a user and run the program again to get a new dump dumpdatabase-01-23-10-32-05.txt:
user_id BIGINT (20)
username VARCHAR (255)
password VARCHAR (255)
first_name VARCHAR (255)
last_name VARCHAR (255)
email VARCHAR (255)

user_id: 1, username: albert, password: pw, first_name: albert,
last_name: albertson, email:,

Of course, in the real application the dumps are thousands of lines, but with Eclipse "Compare with each other", it' still easy to see what happened, so I know exactly what my Java code needs to do.


M.M.M.R.B. Mudannayake said...

Hi "The Pessimistic Programmer" ,

I used your program for one of my assignments, but i got following result... :(

Creating dumpdatabase-0408-091230.txt
at DumpDatabase.listAll(
at DumpDatabase.dumpDatabase(
at DumpDatabase.main(
Java Result: 1

I think this NullPointer is raised @ reading metadata line.. I don't have any knowledge on this area.. Can you help out??

Lars said...


I cannot really help much, but it seems like conn is null. This is set by:
conn = DriverManager.getConnection(url, user, password);

So the url, user or password parameters must be wrong. Please look for correct values in the documentation for your database or JDBC driver.


M.M.M.R.B. Mudannayake said...

hi Lars,

Thanx for the fast reply.. :) And i think you are correct.. It can be "conn". I ll look in to it and try to figure out.. If i do i ll post the reply for sure.. :)

Actually I have hard coded those values (url, username, password) for JDBC. Those values are tested and working.

Thanx again for the wonderful program and prompt reply... Keep up the good work.. :)

Marco Collautti said...

Sooooo thank you!!!
It works!!
How can i restore the database from the dump?

Isuru Ranawaka said...

I'm using a very simple code in one of my applications. I would like your comments on my code.