Hybrid View
-
12 Sep 2012 7:55 AM #1
Database Connectivity
Database Connectivity
Can anyone point me in the right direction on how to populate a grid or tree from an Oracle Database. I have created a connection class i can connect and query the database but do i use this result set to populate the grid or is there other steps such as RPCProxy or interfaces I should implement and use.
Kind Regards,
Douglas
-
12 Sep 2012 9:26 AM #2
I think GWT RPC is quite suitable already for passing data from the database to the GXT Grid data widget. I'm still reviewing GXT 3 but in Ext GWT 2 we created some sort of PageModel which represents a query result and put that into a Map and pass it into the Grid widget:
Code:@Override public void setGridData(List<Map<String, String>> data, int offset, int total) { List<ModelData> list = new ArrayList<ModelData>(); int count = data.size(); for (int i=0; i<count; i++) { ModelData model = new BaseModelData(); for (Entry<String, String> entry: data.get(i).entrySet()) { model.set(entry.getKey(), entry.getValue()); } list.add(model); } ListStore<ModelData> store = grid.getStore(); store.removeAll(); store.add(list); PagingLoadResult<Map<String, String>> result = new BasePagingLoadResult<Map<String,String>>(null, offset, total); LoadEvent event = new LoadEvent(null, null, result); List<Listener<? extends BaseEvent>> listeners = loader.getListeners(Loader.Load); count = listeners.size(); for (int i=0; i<count; i++) { LoadListener listener = (LoadListener)listeners.get(i); listener.loaderLoad(event); } grid.unmask(); }
-
12 Sep 2012 11:01 AM #3
Thanks for the sharp response.
I am new to GXT so i am not sure how it was structured in GXT2. Can you possibly send a snippet of your database code and how the data passes into the method you have shown above.
Thanks again,
Douglas
-
12 Sep 2012 4:57 PM #4
Its not very hard actually. A good design pattern should get you to the right track. Basically, we create separate layer for the DAO and Service layers, here try this one:
Just check the setters in the code and you should be able to replicate the PageModel, just make sure you put this on the "shared" package or a package you defined in your gwt.xmlCode:@Override public PageModel<Map<String, String>> query(String sql, final int offset, final int limit) throws QueryException { try { return jdbcTemplate.query(sql, new ResultSetExtractor<PageModel<Map<String, String>>>() { @Override public PageModel<Map<String, String>> extractData(ResultSet rs) throws SQLException, DataAccessException { PageModel<Map<String, String>> pageModel = new PageModel<Map<String,String>>(); List<Map<String, String>> list = new ArrayList<Map<String,String>>(); int rows = 0; // skip rows for (int i=0; i<offset&&rs.next(); i++) { rows++; } // get rows for (int i=0; i<limit&&rs.next(); i++) { Map<String, String> map = new HashMap<String, String>(); ResultSetMetaData metadata = rs.getMetaData(); int count = metadata.getColumnCount(); for (int j=1; j<=count; j++) { map.put(metadata.getColumnName(j), rs.getString(j)); } list.add(map); rows++; } // iterate remaining rows to get total rows while (rs.next()) rows++; pageModel.setOffset(offset); pageModel.setLimit(limit); pageModel.setData(list); pageModel.setTotal(rows); return pageModel; } }); } catch (DataAccessException e) { throw new QueryException(e); } }
After this, you should create a GWT RPC that access this query code then process the PageModel, get the data which is afield that you need to pass to theCode:List<Map<String, String>>
you can do another query if necessary...Code:setGridData
-
13 Sep 2012 6:06 AM #5
Thanks for providing that code. Still a little unsure of how your application is structured. I noticed when i tried to implement the PageModel you are using the Spring Framework do you have any examples for just using the basic grid, an RPC example and so on.
Thanks again,
Douglas
-
13 Sep 2012 10:05 AM #6
I just installed the Spring STS Eclipse plugin and then created a Spring MVC template app then added the GWT dependencies to it, tried if it compiles, then finally add the Sencha Ext GWT dependencies, including the GXT resources folder.
I'm not sure what approach is most suitable for you, but getting data from the Database is a different, I mean you can do a PageModel approach or ORM approach, whatever suites you best. If you're just testing out, may be you can create a "Test data" then create your column configs for the Grid and then bind a Store in that Grid maybe this can help you:
http://www.java2s.com/Code/Java/GWT/GridStoredatabindingExtGWT.htm
Cheers.


Reply With Quote