Advertisement
farseenabdulsalam

Suggestion requested

Jan 11th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. /*
  2.  * Could you please help me to achieve the following functionality
  3.  *
  4.  *
  5.  */
  6.  
  7. package testapp;
  8.  
  9. import com.j256.ormlite.dao.Dao;
  10. import com.j256.ormlite.dao.DaoManager;
  11. import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
  12. import com.j256.ormlite.table.TableUtils;
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.SQLException;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19.  
  20. public class TestApp {
  21.  
  22.     public static void main(String[] args) {
  23.         try {
  24.        
  25.         //This example uses ORMLite
  26.         //A solution with the help of any ORM will be fine
  27.             JdbcPooledConnectionSource jdbcPooledConnectionSource = new JdbcPooledConnectionSource("jdbc:h2:mem:fzn");
  28.            
  29.         //ConversionPreset is my Entity POJO class
  30.             Dao<ConversionPreset, Integer> conversionPresetDao = DaoManager.createDao(jdbcPooledConnectionSource, ConversionPreset.class);
  31.             TableUtils.createTable(jdbcPooledConnectionSource, ConversionPreset.class);
  32.            
  33.             //Create an entity c1
  34.             ConversionPreset c1 = new ConversionPreset(null, "TITLE", "ACODEC", "BITRATE", "ARATE", "VCODEC", "VBITRATE", "VRATE", "CONTAINER");
  35.         //Persist or save in database
  36.             conversionPresetDao.createOrUpdate(c1);
  37.  
  38.  
  39.             //Retrieve the same from database
  40.             ConversionPreset c2 = conversionPresetDao.queryForAll().get(0);
  41.         //Change any field
  42.             c2.setABitrate("ABR_CHANGED");
  43.             conversionPresetDao.createOrUpdate(c2);
  44.        
  45.         //But the following test reveals that the same field of
  46.         //c1 is not changed. But I need the change to be automatically reflected
  47.             System.out.println(c1.getABitrate());
  48.  
  49.         //The reason I am needing this is for developing a small Download Manager
  50.         //cum Converter. There are two threads in the app. UI thread and Downloader Thread
  51.             //If the changes were reflected automatically, the downloader thread can be completely
  52.         //de coupled from th UI. So that the downloader thread doesn't need to access the methods
  53.             //of UI thread. Instead it will write to the entities and persist in database, then the UI
  54.         //controls bound to the underlying javafx properties automatically reads them.
  55.             //Feel free to suggest a better solution
  56.                    
  57.         } catch (SQLException ex) {
  58.             Logger.getLogger(TestApp.class.getName()).log(Level.SEVERE, null, ex);
  59.         }
  60.      
  61.      
  62.     }
  63.    
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement