If you've seen an interesting question or news item related to Java, feel free to send it to me (along with the answer or whatever made it interesting) at joeo at enigmastation dot com.
How do I get a stack trace in a String? It comes up every now and then: someone wants a stack trace in a String. They're probably wrong; if they're trying to analyze the stack trace, there are mechanisms in the Throwable to do that without creating a String and parsing it, but just in case... ...
Horrible JDBC Code and What's Wrong With It Ah, Efficiency: Thou art a goddess who deserves worship, adulation, sacrifice, but thou art also a goddess who gets said worship, adulation, and sacrifice in all the wrong ways. Here's some JDBC code validating a login, trying to be good code (I ...
Are JDBC statements portable across databases? Ah, JDBC. Sometimes, people want to write JDBC code, because SQL is friendly and safe; then they discover that they actually don't want to deploy on MySQL after all, and they run into problems. The question is usually formed in this way: "If I write my ...
Filtering characters from a Reader It's an occasionally legitimate question: "How do I filter content from a Reader?" There're related questions for Streams, too. Here's an example of a simple Reader class that shows how to remove specific characters from a Reader.Read more...Tags : filter ...
How can I create a maintenance window for my web app? Every so often, web applications need maintenance. Yes, even web applications. It's okay - no need to reach for your security blanket. The problem that's come up is: What's a good way to redirect requests during a maintenance window to an error page? As ...
How can I automatically expire data from a database? Another fairly common question: how can I expire data from a database? This is a case of applying lipstick to a pig: there are ways, to be sure, but the real problem is that you're using a database - a permanent datastore - to handle data that's ...
My data structures have changed - how can I migrate my data model? Sometimes, people design data structures, persisted entities, only to find that - gasp! - their data structures are more fluid than they expected. When all of your data is transient - meaning that it can be destroyed safely - that's no big deal, because ...
How do I send mail with Java? You send mail in Java with one of two APIs, basically: JavaMail and Commons-Email. People who use JavaMail fit in one of three categories: they don't know about Commons-Email, they are doing bulk mail and know what they're doing, or they're gluttons for ...
Re: Horrible JDBC Code and What's Wrong With It I can agree with not wanting to introduce dependencies, but this is the way to do non-ORM database access in Java at the moment. It's not so ok to write twenty lines of raw JDBC, if you can do it in a couple of lines with JdbcTemplate. Even if you don't ...
Re: Horrible JDBC Code and What's Wrong With It Not terrible advice, but... Most of your points go straight at the problem definition, and not the code. The original code made obvious an assumption that all customer numbers were, well, numbers. Using a string when a number is mandated is pointless. ...
Re: Horrible JDBC Code and What's Wrong With It @RJones: It *is* example code, and isn't meant to be the end-all of login authentication. As far as not catching exceptions other than SQLException... are you expecting them to be thrown? I'm not. It'd be silly of me to try to catch exceptions that the ...
Re: Horrible JDBC Code and What's Wrong With It I wold say do not concatenate the prepared statement string - immutable strings. That is also a was of cpu effort. I'd say not all companies have numbers for customer id, therefore keeping it as a String still forces you to check it's not null/empty so ...
Re: Horrible JDBC Code and What's Wrong With It Even if your exception handling is not complete your code is flawed. You fail to catch any Exceptions outside SQLException. There should always be at least (and this includes examples) a standard Exception catch. For example you may wish to catch ...