Geek Smithology

November 28, 2005

PreparedStatements and collection inserts

Filed under: Craft of Dev by Nathan @ 12:14 am

Remember when Java Servlets first came on the scene? Heavyweight beasts like Perl were going to be swept aside by the compiled code running in a multithreaded servlet container. But then mod perl came along and leveled the playing field. To this day, LAMP (Or LARP -> Linux, Apache, Ruby and PostgreSQL) is both viable and powerful for non-enterprise class applications.

PreparedStatements are like mod perl for SQL. For awhile, prevailing wisdom among the relational cognoscenti was that all SQL access should be through calling stored procedures, with performance being among the arguments. I’m not a big fan of stored procedures. They are not the ultimate evil to be avoided at all costs, but I find designs that avoid them cleaner. PreparedStatements are precompiled SQL statements, meaning that they are more more efficient when executing the same bit of code multiple times.

Yet, you see a lot of code where the PreparedStatements are treated as a parameterized Statement, not really seeing the true benefit. A quick example of the wrong way to do it.

1
2
3
4
5
6
7
8
private static final SQL = "INSERT INTO foo(id, bar) VALUES(?,?";

for (Foo foo: fooList) {
    PreparedStatement ps = connection.prepareStatement(SQL);
    ps.setInt(1, foo.getId());
    ps.setString(2, foo.getBar());
    ps.executeUpdate();
}

The problem here is that the statement is being prepared each time through the loop — this is not only unnecessary, but inefficient. The whole point of parameterized queries is that you can reuse them with different parameters:

1
2
3
4
5
6
7
8
private static final SQL = "INSERT INTO foo(id, bar) VALUES(?,?)";

PreparedStatement ps = connection.prepareStatement(SQL);
for (Foo foo: fooList) {
    ps.setInt(1, foo.getId());
    ps.setString(2, foo.getBar());
    ps.executeUpdate();
}

Seems obvious when you see it. Obvious enough that you should always do it right from now on.

November 19, 2005

MP3otW – Ironclad by Sleater-Kinney

Filed under: MP3 of the Week by Nathan @ 4:38 pm

There are fewer musical crimes more egregious than the sentimental over-produced pap put out by such luminaries as New Kids on the Block and Backstreet Boys. Manufactured to do nothing more than manipulate your emotions through lazy electronic hooks and sappy intelligence insulting vocals, boy bands are at best. But sex, even in the form of vague pubescent crushes involving the buying of posters and magazines with the word “Teen” in the title, sells. At least in the 60′s girls were swooning over John, Paul, and George[1] who were brilliant musicians in addition to being “cute”.

There were faux punk bands affecting rebellion, many of which were no more relevent or less manufactured than those they professed to hate, but the real rebellion came from the ladies with the Riot Grrrl movement. The academics called it “feminist resistance to male domination in society and especially to the abuse and harassment of women,” and there was a new energy. You wouldn’t dare objectify the girls of Bikini Kill or Bratmobile, and listening to true feminist lyrics exposes the “Girl Power” of the Spice Girls as the tripe it is (or was, as the case may be.)

One of the bands that formed in the mid-90′s to kick some ass was Sleater-Kinney. A little rough around the edges, they were one of many cures for the Britney Blues, and you probably haven’t heard of them[2]. But enough rambling. The song is Ironclad, from their fifth disc, All Hands on the Bad One. You know you love it.

[1] Let’s face it – nobody swooned because of Ringo
[2] Just like Metallica, by the time they hit the mainstream, the rabid fans will be waxing poetic about the old days before the band sold out.

November 14, 2005

Review – Mr. and Mrs. Smith

Filed under: Sight by Nathan @ 10:39 pm

My wife and I went to see Mr. and Mrs. Smith the other day (yeah, I know. Ha, ha, whatever.) Near the end this crazy thought popped into my head about just how the movie could redeem itself. The titular characters, having been shot up something fierce, crawl under cover to regroup. All that the writers needed to do was have them jump up and be shot to death, rather than the standard montage where they can’t miss and a strike team of crack government soldiers can’t hit. I haven’t been so disappointed by a character not dying at the end of a movie since Tom Cruise lived through The Last Samurai.

John and Jane Smith (why didn’t they just call it Mr. and Mrs. Doe?) are your typical Solondz-lite couple: bored with their lives, bored with each other, bored with themselves. But beneath the thin veneer of suburban bliss flows a current of intrigue: turns out our friends and neighbours are actually assasins for rival top secret spy agencies. If you think that’s spoiling the plot, you aren’t really in the target audience for this one. Circumstances collide and they each become the other’s next assignment. Ironically, trying to kill each other succeeds where marriage counseling did not, and in a violent epiphany, they realize they are still in love. Together, they will defeat the evil empire.

Pretty cut and dry, boilerplate action here. The chemistry between Brad Pitt and Angelina Jolie (and most of the audience) is palpable, but sometimes when you put two really good looking people on the screen, they are so enamored with themselves that it can get distracting. It doesn’t devolve to the level of Runaway Bride[1], but sometimes you get the feeling we’re supposed to be entertained just because the stars are on screen.

And who’s to say…maybe we are.

four star

[1] You know, the movie where Julia Roberts and Richard Gere spent more time mugging for the camera than acting. “Pretty Woman was good. Remember Pretty Woman? That other move we did?”

November 12, 2005

MP3otW – Caprice No. 2 by Niccolo Paganini

Filed under: MP3 of the Week by Nathan @ 5:43 pm

Before Marilyn Mason’s Antichrist Superstar in 1996.
Before Iron Maiden’s Number of the Beast[1] in 1982.
Before Johnny Rotten ushered in a new era screaming “I am an antichrist” in 1977.
Before John Lennon let us know the Beatles were more popular than Jesus in 1966.

Hell, a full century and quarter before Jerry Lee Lewis was accused by Reverend Falwell of playing the “Devil’s Music”, violinist Niccolo Paganini was playing the violin with such passion, force, and virtuosity that people in the audience would faint.

There were rumours that one of his violin strings was fashioned from his wife’s intestines, and of course there was no way someone could develop such technique without entering the proverbial Faustian pact. Such was his mastery of the violin that inspired the incomparable Franz Liszt to do the same for the piano (and we shall hear from him one day, believe me.)

In 1820, Niccolo’s 24 Caprices for Violin were published, to this day considered far too difficult for the repertoire – these pieces are for virtuosos only. Without further ado, here is the second caprice from that incomparable collection. And yes, that is a solo violin (played by the excellent James Ehns, master of the fiddle.)

[1] Yeah, I know the song. I know it’s about a dream. I know it’s not pro-Satan. But did your parents know that?

November 11, 2005

Eskimo Bill Says… (6 in a series)

Filed under: Eskimo Bill by Nathan @ 5:48 pm

(on being unable to discern the origin of a disconcerting stain in the back of the rental van) …We’ll go with coffee.

November 8, 2005

IDEA Tricks – Open multiple files using wildcards (1 in a series)

Filed under: Craft of Dev,IDEA Tips by Nathan @ 8:40 pm

If you have an n-tiered architecture, sometimes you have to modify every tier to add some functionality. If you are using a naming convention that standardizes names for the components of your component, IDEA makes it really easy to open up every file.

Let’s say you have the following files, likely located in several different packages:

EntityDAO
EntityDAOOracle
EntityDAOFactory
EntityBean
EntityAction
EntityForm

To open all of these in one shot, CTRL-N and type Entity* to get the autocomplete list. Then press Shift+down arrow, highlight all the files, and press Enter

Voila! All your files are open.

November 2, 2005

Egad Hawkman! Paratroopers have captured Cynicalman!

Filed under: Grab Bag by Nathan @ 10:33 pm

Well I’ll be a monkey’s second cousin twice removed – it’s been so long since I posted I’m feeling like shelfoo. Long story short: I took myself off the grid for a little while to spend some time with the family and to start weaning myself from my crippling TonyHawk-OnlinePoker-Bloglines addiction. I call it detox, my wife calls it “getting a life.” Tomato, Tomahto, eh? Whether we call the whole thing off or not, I owe an update…

It started last Sunday (Sunday! Sunday!) as the project team flew to Surrey (they sold us the whole seat, but we only needed the edge) to deploy our mad pimpin’ (and totally ph4t) software to production. We proceeded to work something like 22 of the next 24 hours, including a lovely stretch at about 2:30 in the morning when Eskimo Bill and I were frantically verifying an Axis patch I coded so that we could sleep.

That wasn’t the worst part, though. The worst part was when Oracle melted down. You know how people always tell you Oracle never melts down? Well, as Sean Connery would tell you, Never Say Never (Again). Although maybe he should’ve at least muttered a subtle “no” when they asked him to debase himself for First Knight. I mean c’mon. I liked it the first time when it was called Excalibur. And didn’t suck. Like Oracle. That night.

Monday, I was exhausted, spent and probably hallucinating, but Eskimo Bill managed to talk me into going for beers and then checking out the new Doom movie. (To be clear, talking me into going for beers and then checking out the new Doom movie is slightly easier than asking Hulk Hogan to rip off his shirt and do a leg drop.) It would be one in the morning by the time I slept.

Tuesday was supposed to be a nice gentle slope: a half day then back to the hotel before going out to our release celebration. I thought I might catch a cup o’ java with the Luesinkian Menace, but it was not to be. There was work to do, and depite raving half dead screeds to the contrary, we did not skip dinner to go home and sleep (that would’ve been the grown-up thing to do) but instead went to the Keg. We drank a lot, ate some food, and did the whole party thing. I’d love to tell you that afterward we went back to the hotel for some much needed rest and not that we went to Earl’s and drank eight shooters, but I’m an honourable man.

We zombied our way through Wednesday and flew home. I wasn’t really myself again until Monday, when it took me about 3 hours to fix a bug that the undead version of me (along with help from the undead Popowich and the undead Tran) failed to track down during a fair bit of Tuesday and a lot of Wednesday. For what it’s worth, despite the inevitable deployment headaches, the client heaped accolades upon us and there was much rejoicing.

That brings us to Halloween. Most honourable number one and number two sons went out trick or treating with Marie, while I dressed up especially for the occasion (or in my every day dress, as Mr. Kuz would put it) as the headless hippie. Here’s a picture. Just in case you don’t know which one is me, I’m not the bat (that’s Gabriel) or the mouse (that’s Xander.) The costume was effective enough that a few kids wouldn’t come up the stairs when I opened the door, but Marie assures me that has little to do with the costume.

Tuesday was the staff meeting. We skipped out early to hit the CAMUG meeting, where we hooked up with the Ront. Followers of that little known English dialect known as Rontglish will be happy to know he has coined another word: presentators.[1]

Speaking of the Ront (aka Fronto, aka Beer-out-of-Rag, aka Skinhead Newfie), he has started up his own little blog, The Rontologist. He likes to rant about horrible code (who doesn’t?) and he’s in the process of taking the Hibernate kool-aid so I can get the skinny without getting wet. So far the word is not that great my friends.

On Wednesday, it took me an hour and a freakin’ half to get to work because for some reason when the first snow falls it seems half the city forgets how to drive. After work I came home, hung out, and then sat on the computer. I opened up my blog, and hit the “Write” button and started typing…

Well, I’ll be a…

[1] Don’t get me Ron, I think Fronto’s a great dev and even better friend, he just doesn’t know how talk good sometimes.

Powered by WordPress