We have a server-side application (in JAVA, communicates via web services) that accesses a database. If we wish to sell just the application to a customer, so that he can use his own choice of database (RDBMS for now), how can I design the application to work with any database developed as per some spec ? I have considered JPA for now but still not sure about all its features. Imagine if Facebook were to sell their core logic, independent of database so that buyer can implement any database and plug this to facebook logic.
JPA is a specification - you would want to leverage an implementation of it. There are a large number of Object Relational Mappings (ORMs) that are database agnostic most of which provide JPA implementation - Hibernate, SpringData, EclipseLink (used to be TopLink), Apache Cayenne.
If you want to work directly at the SQL layer rather than through a JPA then you should write your SQL the ANSI SQL specification, which most databases support. If you go this route I would recommend encapsulating your SQL and providing a mechanism for DB specific overrides of the SQL, to provide the option for tuning SQL for a specific DB and leveraging DB specific syntax and features.