<< Filtering characters from a Reader | Home | Horrible JDBC Code and What's Wrong With It >>

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 SQL in a prepared statement, is it going to work the same way in $OTHER_DATABASE?"

The answer is ... no, not in general, not if you rely on any non-portable SQL. And given the state of SQL in the various implementations, writing portable SQL can be a challenge, depending on your datatypes.

PreparedStatements are translators for your database; they basically allow you to parameterize your SQL, making it safe from SQL injection. It translates fields in the query for you, into a format that's presumably safe for your database... but does nothing else to your query.

Therefore, if you use any feature in your SQL that's not portable to every other database out there, your PreparedStatement won't work on any database that doesn't support whatever syntax you're using.

If you need portability in your relational data storage, then you should consider Hibernate or some other JPA implementation; they are designed to figure out the database you're using, and customize their interactions accordingly.

They also tend to come packed with neat features like caches and - occasionally - distributed caches, so your access speed has the potential be dramatically faster. Note: potential. It's easy to design an object/relational model that crushes your performance. (There's no magic bullet here.)

Of course, you could also avoid using relational databases in the first place, which obviously gets rid of relational database portability issues... (To be fair, this is also why you use JPA.)




Add a comment Send a TrackBack