Archive for December, 2007

Ubuntu: mouse pointer is randomly disappearing

Thursday, December 20th, 2007
Problem:
Lately you have noticed that your mouse pointer disappears occasionally, or flickers while you move it along the screen, in a fairly random fashion. You may have also noticed that this is mostly happening while Firefox is in the process of loading a page. Sometimes you may also get stuck with an animated mouse pointer, which is fixed only by restarting the X windows system.

Cause:
A bug in the Enhanced Zoom Desktop plugin of CompizFusion.

Solution:
Go to System → Preferences → Advanced Desktop Effects Settings. Click on Enhanced Zoom Desktop and then select the Mouse Behavior tab. Make sure that you have not checked the options Scale the mouse pointer and Hide original mouse pointer. (Yes, you will have to live without those two cool features if you want to get rid of the missing mouse pointer problem). Close the settings manager and restart X.

How to retrieve INSERT-generated IDs with JDBC.

Monday, December 17th, 2007
The answer is very simple. You just have to call the getGeneratedKeys() method of your statement object, right after its execution.

The getGeneratedKeys() method will return a ResultSet object that contains any keys that was generated by the execution of your statement.

Let's see a simple example. Assume the following MySQL table:

mysql> show fields from Person;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100)     | NO   |     |         |                |
+-------+------------------+------+-----+---------+----------------+


This is the code that will insert a row into this table. We do not provide a value for id, because it is an AUTO_INCREMENT field; MySQL will automatically assign a unique value for this field.

String sql = "INSERT INTO Person (name) VALUES ( ? )";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,"Giannis");
statement.execute();

ResultSet rs = statement.getGeneratedKeys();
rs.next();
int personId = rs.getInt(1);
rs.close();

statement.close();


After the execution of the statement, we call getGeneratedKeys() to get the ResultSet to get the value that automatically was generated for the id field. We call next() to read the first line of the result set, and then we read the first column, which we expect to be an integer.

How to get the files of a package on Ubuntu

Saturday, December 8th, 2007
To get the list of the contents of an installed package on Ubuntu, try this command:

dpkg -L package-name

or alternatively:

dpkg --listfiles package-name

For the reverse action, that is, to find which package a file belongs to, try this command:

dpkg -S file-pattern

or alternatively:

dpkg --search file-pattern

Replace file-pattern with part of the filename you're searching for, or even the full path.

For example, we will find out which package contains the file /usr/share/java/servlet-api-2.4.jar:

giannis@giannis-desktop:~$ dpkg -S /usr/share/java/servlet-api-2.4.jar
libservlet2.4-java: /usr/share/java/servlet-api-2.4.jar


The package we're looking for, is: libservlet2.4-java

Get stack trace information in Java programs.

Friday, December 7th, 2007
Sometimes it could be useful to get the current stack trace while a Java program is being executed.

As of my limited knowledge, I don't know a direct way to get the stack trace, but sometimes the solution is right in front of your eyes.

This code will print the stack trace at any point of your proram:

StackTraceElement[] elements = new Throwable().getStackTrace();
for (int i = 0; i < elements.length; i++)
System.out.println(elements[i]);


Or using for-each:

for (StackTraceElement element : new Throwable().getStackTrace())
System.out.println(element);


Or even simpler you can just call printStackTrace() if you only want to print the stack trace in standard error:

new Throwable().printStackTrace();

Note that we are not actualy throw a Throwable. We only create a Throwable object which contains the information we want.