1

I have a question which probably have been asked a million times, but I am at the point where I need to determine something like this. Here is the question: Is there some way to write Java code that queries, updates, inserts,deletes, calls stored procs etc using one type of code regardless of the fact which database there might be behind the scene? Such as I write code put it on a system and it automatically detects that db is MS SQL Server and works, then in another case database may be Oracle but application doesnt need any code changes and automatically detects and works and so on so forth....

If this is not possible then what is the best approach to almost get to this level of abstraction...e.g Have a config file and only make changes in that file regard which db drive will be used ?

Thanks

6
  • There are some queries that differ between databases. If you use those queries, the answer is no. Commented Dec 4, 2013 at 17:59
  • My personal opinion, but why you want to have this level of abstraction? In real scenario it's very rare case to change DB from one provider to another Commented Dec 4, 2013 at 17:59
  • The short answer is no, not all databases use the exact same syntax. Commented Dec 4, 2013 at 17:59
  • 1
    hibernate.org? Commented Dec 4, 2013 at 17:59
  • Sorry, this is a conversation question and does not fit the StackOverflow format FAQ Commented Dec 4, 2013 at 18:00

2 Answers 2

1

I have recently been a convert to using QueryDSL: http://www.querydsl.com/

Using this to construct your queries, all you need to do is provide the proper "template" when you declare your query object and it provides the mapping for that particular database platform. I have not went as far as using it to call stored procedures.

Sign up to request clarification or add additional context in comments.

Comments

0

Go for HQL(Hibernate query language) which is object oriented. For this you need to use hibernate. This way you dont have to depend on syntax of particular db. In case you want to switch from one db to another, it will be smooth

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.