Are you using an IDE? I find that using the debug feature on an IDE can really help in this situation.
For instance, in most IDEs (I use NetBeans) it is helpful to set up a break point at your SQL statement. That way when you debug the application, you can walk through exactly what is happening when it runs. I find it very useful in this type of situation.