Feb 2011 12

The Hibernate Tools reverse engineering function creates fully qualified names for MySQL, following the “standard”(when there is no standard, it seems) naming of CATALOG.SCHEMA.TABLE.

It just so happens that MySQL uses DATABASE.TABLE directly and adding a third component(the catalog) will generate a syntax error. Remember, in MySQL the database is the schema.

Even though this is a documented trait of MySQL, the reverse engineering code in Hibernate tools, even if you tell it to use MysqlDialect or Mysql5Dialect, insists on adding a catalog name to the fully qualified table name. After 3 hours of trying to get the reverse engineered managed classes to work, I finally went brute force.

As some have noted in forums throughout the WWW, if you remove the catalog annotation from the class/table mapping code, the generated syntax is corrected.

<class name="com.zefonseca.testing.db.orm.People" table="people" catalog="testing">

Removing catalog="testing" from the mapping code does the job.

So, Perl’s our friend. After generating the mapped classes, I went into the base ORM package directory, and ran:

find . -exec perl -pi -e 's/ catalog="testing"//gmsi' {} \;

Every file on every subdirectory from where you run this will have catalog=”testing” removed. If you happen to have other files which match the string catalog=”testing” under those directories, they’ll get clobbered, so proceed with care.

I don’t know whether I’m using the wrong MySQL dialect in Hibernate, or what exactly is causing this. But, since the 3 piece table names are invalid in MySQL, there really oughta be no excuse for any of the dialects to generate them that way, even if the automatic code adds a catalog attribute.

Feb 2011 14

Below is a sample JDBC client for IBM’s DB2 Express-C database server. The examples out on the WWW seem to be outdated, and I just wanted a simple example to run queries against a test database, not learn every detail of enterprise architecture software in Java and DB2.

A few notes that may be helpful: The driver name to use against Express-C is com.ibm.db2.jcc.DB2Driver not COM.ibm.db2.jdbc.app.DB2Driver.

It is also important to get the connection URL right. Use jdbc:db2://localhost:50000/SEUBANCODEDADOS, not jdbc:db2:database. The latter will attempt to load a Level 2 JDBC driver that is not included in the JARs.

Adjusting CLASSPATH
On your instance installation path, you should find a java/ subdirectory. In my case the full path is /opt/db2/V9.7/java/. There you’ll find the files db2jcc.jar and db2jcc_license_cu.jar -include both in your CLASSPATH. The first JAR includes all needed client software, the second includes the Express-C license.

package db2;

import java.sql.*;

public class db2query {

	public static void main(String args[]) {
		try {
	        Class.forName("com.ibm.db2.jcc.DB2Driver");
			Connection conn = DriverManager.getConnection("jdbc:db2://localhost:50000/YOURDATABASE", "user", "password");
			Statement stmt = conn.createStatement();
			ResultSet rs   = stmt.executeQuery("SELECT COUNT(*) from schema.table");

			if (rs.next()) {
				System.out.println("Rows found:  " + rs.getString(1));
			}
			else {
				System.out.println("Zero rows found.");
			}

		} catch(Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

}

Note that the above code is for your reference only. In production code you’d separate the exceptions and deal with each error accordingly.

Mar 2011 15

Part of our series of Java code snippets, here’s a simple example of a class that plots a SIN() function on a JFrame’s graphic context. Adjust the parameters to the Math.sin() function as well as the amplitude and width multiplier.

import javax.swing.*;
import java.awt.*;

public class sin extends JFrame {

	private static final long serialVersionUID = 147L;

	public sin() {
		setTitle("SIN(X) Function Plot");
		setBounds(400, 200, 400, 400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public void paint(Graphics g) {
		drawSin(g);
	}

	public void drawSin(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		g2.setStroke(new BasicStroke(3));
		g2.setColor(Color.blue);
		g2.drawLine(0, 200, 400, 200);
		g2.setColor(Color.red);
		int initY = 200;
		int amplitude = 100;
		for ( int i=0; i < 400; i++) {
			drawPoint(g, (int)(i * 3), initY + (int)(amplitude * Math.sin(i / 6.28)) );
		}
	}

	public void drawPoint(Graphics g, int x, int y) {
		Graphics2D g2 = (Graphics2D)g;
		g2.drawLine(x, y, x, y);
	}

	public static void main(String args[]) {
		sin mf = new sin();
	}
}
Mar 2011 16

A short little Java program to test whether

1^3 + 2^3 + 3^3 + ..... + n^3 = (1 + 2 + 3 + .... + n)^2

Remains true for larger values of n.

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.Math;

public class cubesums extends JFrame {

	private static final long serialVersionUID = 1L;
	JLabel value;
	JTextField iterationsText;
	public cubesums() {
		getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
		iterationsText = new JTextField("10");
		getContentPane().add(iterationsText);

		value = new JLabel("N/A");
		getContentPane().add(value);
		JButton jb = new JButton("Calculate");
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				long ln  = 0;
				long ln3 = 0;
				long basesum=0;
				int iterations = Integer.parseInt(iterationsText.getText());
				for (int i=1; i<= iterations; i++) {
					ln += (long) Math.pow(i, 3);
					basesum += i;
				}
				ln3 = (long) Math.pow(basesum, 2);
				value.setText("SUM OF CUBES " + ln + " == SQUARE OF SUM " + ln3 + " FOR N == " + iterations);
				pack();
			}
		});
		getContentPane().add(jb);
		setBounds(400,400,400,100);
		pack();
		setTitle("Sum of cubes = Cube of sum");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new cubesums();
	}

}