Wednesday, August 12, 2009

[SQL] Why 1 = 1 in SQL query ?

I saw that interesting thread on StackOverflow here

Sometimes, you need to build dynamically the WHERE statement in a SQL query.
So you will concatenate some key/value seperated with a "AND" operator

If you run the following:


import java.util.HashMap;

public class Test {

public static void main(String[] args){

StringBuffer query = new StringBuffer("SELECT * FROM users WHERE ");

HashMap mp = new HashMap();
mp.put("firstname", "John");
mp.put("lastname", "Doe");
mp.put("login", "john.doe");

for (String k: mp.keySet()) {
query.append(" AND " + k + " = \"" + mp.get(k) + "\"");
}

System.out.println(query.toString());
}
}


The above will return:
SELECT * FROM users WHERE AND lastname = "Doe" AND login = "john.doe" AND firstname = "John"

The WHERE statement has been generated successfully except that now, we would need to remove the first occurence of the AND operator.

The trick is to add the following statement at the beginning of the query:
1 = 1

With this additionnal above statement, we wont need to look for the first occurence of the AND operator and our query will work since 1=1 will always be evaluated to true



import java.util.HashMap;

public class Test {

public static void main(String[] args){

StringBuffer query = new StringBuffer("SELECT * FROM users WHERE ");

HashMap mp = new HashMap();
mp.put("firstname", "John");
mp.put("lastname", "Doe");
mp.put("login", "john.doe");

query.append("1=1"); // This can also be moved when declaring the query in the StringBuffer

for (String k: mp.keySet()) {
query.append(" AND " + k + " = \"" + mp.get(k) + "\"");
}

System.out.println(query.toString());
}
}



The result will return:

SELECT * FROM users WHERE 1=1 AND lastname = "Doe" AND login = "john.doe" AND firstname = "John"

Tuesday, August 11, 2009

[SpringMVC] Spring Tool Suite 2.1.0 includes a Spring MVC template project

I have just installed Spring Tool Suite 2.1.0.
Spring Tool Suite (STS) is a tool from SpringSource based on the Eclipse IDE.
It includes most of the tools (Maven, Tomcat ...) you would need to do some Spring development.

I have tried STS 2.0.2 and one of the reason I stay with Netbeans (which is also a great product) is that creating a Spring MVC project was easier in Netbeans than in STS.
With STS 2.0.2, I had spend long hours to find out how to make a Spring MVC template work with no success (using maven2 archtype).

With STS 2.1.0, it's now easy to create a Spring MVC project to base your development on.

To create a Spring MVC project, go to:

1) File -> New -> Spring Template Project
2) Select the kind of template you want (MVC, Batch, Webflow...) and click Next
3) Enter your project name and top-level package name and click Finish
4) Et Voila, a Spring MVC project ready to be used.

Also, the great thing is that the configuration of this Spring MVC project is based on annotation instead of XML-only file (on Netbeans). The controller is then a simple POJO with @Controller.

Have a try, I look forward to playing around with it

Monday, August 10, 2009

[Python] Easily make available some files through HTTP

At work, our desktop computers run on different OSes, mainly Linux and Windows.

To be able to share files between Linux and Windows, a samba server will need to be installed and configured on the Linux box.

Since I dont need any files to be permanently shared, I dont have samba configured. If I need to make some files accessible, I use the below script to create a simple python-powered HTTP server, so that the files I want to share are easily accessible for download through HTTP.


#!/usr/bin/python

import SimpleHTTPServer
import SocketServer

# minimal web server. serves files relative to the
# current directory.

#Replace the port number if the port 8000 is in use already
PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()



-Just save the below script (i.e. share_files.py) under the directory you want the World to have access to (i.e. $HOME/Pictures/)

-Go to $HOME/Pictures/ folder

-Give executable right to you script:
chmod u+x share_files.py

-Launch the script
./share_files.py

-Go to http://localhost:8000/
You should see the list of files you have under $HOME/Pictures/

To have the folder accessible for others, give away your IP address instead of localhost

Note:
The above script was found here