I need help to build java web application that stores data to local DB on offline mode and sync stored data to online server when it connects to internet/server.
Yes, there are several methods to accomplish this. A general solution is to use sync4j, a library and protocol to support multi-master synchronization of databases. Also, some database systems (mysql, postgresql) have synchronization schemes that you may find useful.
If you do not require multi-master synchronization, you can roll your own solution by defining which copy of the data (which server) has the master data for each table or data item. When the offline system has a chance to connect to the online system, they can exchange data items. Each server may be the "master reference" for some data.
For example, if you have a device that is collecting data, say temperature data, and that device intermittently connects to a hosted server somewhere to upload the temperature readings, the device is the "master" of temperature data. If the device also has configuration parameters or settings that you want to control from the hosted server, the hosted server is the "master" of that configuration and settings data. While the device is offline, it uses the last configuration data and settings it downloaded from the server and collects temperature data locally. When the device is able to connect to the hosted server, it uploads all the new temperature data and downloads any new configuration or settings. I have implemented such a system which has worked successfully for millions of uploads and hundreds of thousands of downloads.
Why roll your own solution if there are existing libraries that accomplish synchronization of databases? The main reason is performance. By uploading only the new data and downloading only the new configuration information (or whatever), you can minimize the traffic required to keep the two systems synchronized. You can also control the timing and priority of different data (which is most important to keep synchronized), insert transformations if needed, etc.
If the same data items can be modified by either system, so-called multi-master synchronization is required which is much more complicated and requires more communications back and forth to ensure consistency. If you need multi-master sync, I strongly recommend using a library like sync4j or the built-in capability of your database vendor. It is difficult to get this logic right and there is little opportunity for you to "do better" than the experts at this.