RazorHead
RazorHead

Reputation: 1520

Java program using different relational databases

I have somewhat a general question. Is it possible to have a java program that works with the type of database that the user specifies in let's say the application's setting. Meaning, depending on the server the app runs on, and the type of database server runs, the application must adjust itself to run its queries.
Do I have to specify different queries for different type of databases?

Upvotes: 0

Views: 67

Answers (1)

Justin Cave
Justin Cave

Reputation: 231691

Is it possible? Sure.

In general, if you really need database independence, you would write your application leveraging a database-independent framework to handle persisting data and retrieving it from the database in question. Something like Hibernate, for example, knows how to construct queries that work against Oracle and how to generate a different query that does the same thing against SQL Server, MySQL, etc.

Of course, building a completely database-independent application introduces additional issues. For example, different databases handle locking differently-- a simple SELECT in SQL Server will lock a row while it won't do so in Oracle. That may lead your application to behave differently depending on the database. Additionally, completely database-independent applications generally do not scale as well as applications that leverage a particular database's unique functionality-- a framework like Hibernate won't generate the SQL that is most efficient for your particular application against any particular database. It generates generally reasonable SQL against each database, of course, but a human that knows the business requirements and the specific database can almost certainly do better.

Upvotes: 2

Related Questions