Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Sunday, May 01, 2016

Lifesaver Snippets

Universal JDBC CLI Client

I work on the Unix/Linux boxes running Java application servers regularly. One of the problems I always face is that access to the server is restricted, yet occasionally I have the need to access the most protected resources of all - the database. Without direct access to the database server, all my GUI database client tools are useless. What is available to me is only the JVM in an SSH session.

I find that, while I can't install any database tools on the servers without going through a lot of bureaucratic red tapes, I can usually make use of what is already available on the server to do my work. With only h2, the JDBC driver and some shell script, I am able to connect to the database and get the data I want. Here's how.

First, put all the jar files into the same folder. You should at least have h2 and the JDBC driver in the folder. Create a script h2shell:

#!/bin/bash

CP=.
for j in *.jar; do
  CP=$CP:$j
done

java -Xmx512m -classpath $CP org.h2.tools.Shell "$@"

Make the script executable:

chmod +x h2shell

Next, create a helper script with the database connection properties. Let's call the script mydb:

#!/bin/bash

./h2shell -url url -user username -password password

Make the script executable too:

chmod +x mydb

You can now connect to the DB by running mydb:

./mydb

telnet substitute

Due to the security policy of one of my clients, they remove the telnet program from all of their servers. telnet is one of common tools to test network connectivity. I found a crude alternative by writing some Python scripts.

import telnetlib

t = telnetlib.Telnet('some.host', port)
print t.read_eager()
t.write(message)
print t.read_all()
t.close()

Besides telnetlib, Python has a lot of other wonderful modules such as urllib, socket.

Wednesday, January 07, 2009

Convert MDB file in Linux

I have some old, standalone Java projects which used Access MDB file as database. As I have always wanted to use an installation-free database for my application, I was looking for ways to convert the data in the MDB file to HSQLDB. I choose HSQLDB because it's simple to use and cross platform.

I started off researching with unixODBC but ended up installing odbcinst. Eventually I stumbled upon mdbtools. That solves my problem. After installating mdbtools on my Ubuntu laptop, I exported the schema

mdb-schema data.mdb > schema.sql

I edited the schema to HSQLDB syntax and created the new database in HSQLDB.

Then, I only need to do

mdb-export -I data.mdb table | sed -e "s/)$/);/" > table.sql

for each of the tables in the MDB file. The sed command is to append a ';' as mdb-export doesn't end the insert statements with ';'.

I then edit the INSERT statements and run them in HSQLDB.

This is simpler than most of the other approaches I have read on the web.