Archive for Programming

What is the difference between an inner join and outer join?

This is another one of those stupid questions asked during technical interviews that don’t really give an indication of whether you are actually qualified for the job.

Inner join

Say you have one table of CUSTOMERS and one table of ORDERS. Each row in the ORDERS table has a reference (foreign key reference) to a customer id which represents what customer placed that order. If you want to run a query that lists the orders along with the names of the customers who ordered them (since a customer id number itself it pretty useless), you will want to execute a join query:

SELECT CUSTOMERS.NAME, ORDERS.NAME
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID

Outer join

If for some reason, you wanted the query results to return all customer name regardless of whether they placed an order, you can use one of two types of OUTER JOINS, in this case, a LEFT JOIN:

SELECT CUSTOMERS.NAME, ORDERS.NAME
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID

Leave a Comment

What is Model-View-Controller?

One interview question that I’ve been asked repeatedly is “Describe the model-view-controller (MVC) pattern”. I guess that’s technically a demand, not a question.

Anyway, when you’re building software, if you really wanted to, you could put together in one place all the code that deals with 1) deciding what to show the user (View), 2) figuring out what to do with user input (Controller), and 3) doing what the application decided to do in step 2 and in the process, storing and retrieving necessary data (Model). But unless the software is really, really simple (only a little more than “hello world”-simple), trust me, you do not want to do that.

Why not? Because as the software’s codebase becomes larger and more complex, it is likely that you may want to reuse certain components. Like you may want to retrieve some set of data from a number of different places. The code is much more extendable and maintainable when you use MVC.

Typical flow is:
1) The view show the user something and give him/her a means to input/respond to that something
2) The controller figures out what to do with the user input and asks the model to do what it decided to do
3) The model does what the controller told it to do along with getting and modifying relevant data in the process, then passes resulting data to the controller
4) The controller passes the resulting data to the view
5) The view shows the user the results

Fortunately, in the Java world, there are many free open source frameworks that make writing Model-View-Controller applications pretty easy. At my current company, we use a combination of Struts (Controller layer), Tiles/JSP (View layer), Spring (Model layer – application logic), and Hibernate (Model layer – data abstraction). I’m not sure why we do that. Although we only use Spring Framework for its Inversion of Control container, it supports the whole MVC stack.

Next post, I will talk about Struts.

Comments (2)

Open source RSS parsing API

There’s a package called ROME that will do most of the work of parsing various syndication feeds for you. Pretty neat.

https://rome.dev.java.net/

There’s also a Jakarta Commons project called Feedparser that looks similar but it’s listed under their “dormant” projects.

http://jakarta.apache.org/commons/sandbox/feedparser/

Leave a Comment

Why the programming posts?

I’ve started posting little blurbs about computer programming because it’s a good way for me to take notes for myself. Also, programmers often use Google to search for programming solutions and maybe my posts will be helpful to someone. For example, my post about setting no-cache for Struts gets a lot of hits.

I know most of you don’t care, but you’ll just have to deal. :p

Comments (1)

flatten=”true” in Apache Ant

When building a war file using an Ant build script, you need to set flatten="true" when copying the required libraries into WEB-INF/lib, otherwise the servlet engine will not be able to find the .jar files.

Example:

<copy todir="${project.war.home}/WEB-INF/lib" flatten="true" includeEmptyDirs="false">
<fileset dir="${project.lib.home}"/>
</copy>

Leave a Comment

Site-wide nocache in your Struts application

Instead of putting meta tags in every page to tell the browser not to cache, you can simply modify your Struts configuration as follows:

Add nocache="true" attribute to the <controller> element in struts-config.xml.

More details here: http://struts.apache.org/1.x/userGuide/configuration.html

Leave a Comment

Emacs for Windows

Ugh. I don’t know why this had to be so complicated.

Here is the best version of Emacs for Windows:

http://ntemacs.sourceforge.net/

There’s an annoying splash screen that displays on start up all the time. You can disable this by setting inhibit-splash-screen to on.

Leave a Comment