Results 1 to 9 of 9

Hybrid View

  1. #1
    Member
    Join Date
    Jul 2010
    Location
    Vancouver
    Posts
    7

    simple multiple records from database

    I have got the following working using JDBC and MySQL

    http://www.lightstreamer.com/vb/show...p?t=231&page=2

    Somebody commented on the example:

    Quote:
    Originally Posted by gerard
    I don't see in the code why it just reads the "first record" - isn't it looping over the entire record set and sending them all.

    Because in this example we suppose that the first record maps a subscribed item. So, each change in this record is pushed to the clients. In real-world scenarios, you could have different mappings.

    I've looked through all the examples an example that i can only update a couple of records

    is there anywhere in the documentation that explains how to update a couple of records or explains mapping records and how to use them.

    thanks.
    Scott

  2. #2
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,013
    We have never tried to suggest a mapping between relational database concepts and lightstreamer concepts.

    As a simple rule:
    an item in MERGE mode corresponds to a record in a relational table (possibly obtained through a join) identified by a predetermined unique key,
    whereas an item in COMMAND mode corresponds to a whole table (possibly obtained through a join) which supports a unique key.

    However, this is only a logical mapping and all the conversion stuff has to be performed by the Data Adapter.

    Did I address the question?

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Vancouver
    Posts
    7
    Not really, i may not be asking the right question.

    Basically what i am trying to do is change the stock list demo in java from from the external feed simulator to a real database feed.

    it doesn't have to be as complicated as the demo just updating a couple of records from the db would be fine for now.

    table row 1 field one: update me with record one
    table row 2 field one: update me with record two

    I the database demo it shows how to update only one record using the subscribe function.

    what i need to know is how to update multiple records using the subscribe function.

    i may just be missing the concept of how subscribe works?

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Vancouver
    Posts
    7

    yes merge mode

    i went over the general concepts pdf again.

    I definitely want MERGE mode.

  5. #5
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,013
    I'm not sure if you still need an answer.

    So, for now, I just clarify that subscribe tells you when some client needs the updates related with a row; moreover, the supplied item name tells you which row.
    For this reason, you have to predetermine the item names in such a way that they refer to rows; the obvious way is using a unique key field associated to the rows.

    Once you have received the request through subscribe (and until you receive unsubscribe), it's up to you to observe the row and send updates upon changes (and the initial row contents as the snapshot, if needed).
    I would prefer not to suggest a way of doing that; just note that, as shown in the tutorial, that should be performed by your own threads.

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Vancouver
    Posts
    7

    modified tutorial code.

    thank you, i do understand the concept of subscribe

    Since the example shows that are are only subscribed to own item my question is where do you actually loop through the result set to subscribe to each item (with the unique key)

    if i change the if (rs.next()) to while (rs.next()) it loops through all the records in the table but it updates only the first.

    Can you show me where i would modify this code to subscribe to the unique items.

    Thank you very much
    Scott

    Code:
    import java.util.*;
    import java.io.File;
    import java.sql.*;
    import com.lightstreamer.interfaces.data.*;
    
    public class HelloWorldDataAdapter implements SmartDataProvider {
    
        private ItemEventListener listener;
        private volatile GreetingsThread gt;
        private Statement command;
    	private final HashMap subscribedItems = new HashMap();
    	/*
    	 * used for the IndexedItemEvent version only (currently commented out)
         */
        private static final String[] names = new String[]{"message", "date"};
    
        
    	
    	
    	public void init(Map params, File configDir) throws DataProviderException {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                String dbFile = "ls";
                String database = "jdbc:mysql://localhost:3306/ls";
                String url = "jdbc:mysql://localhost:3306/";
    		String dbName = "ls";
    			
    			Connection conn = DriverManager.getConnection(url+dbFile, "user", "pass");
                command = conn.createStatement();
            } catch (Exception ex) {
                System.out.println(ex);
                throw new DataProviderException(ex.getMessage());
            }
        }
    
        public boolean isSnapshotAvailable(String itemName) throws SubscriptionException {
            return false;
        }
    
        public void setListener(ItemEventListener listener) {
            this.listener = listener;
        }
    
        public void subscribe(String itemName, Object itemHandle, boolean needsIterator)
                throws SubscriptionException, FailureException {
            if (itemName.equals("greetings")) {
                gt = new GreetingsThread(itemHandle);
                gt.start();
            }
        }
        
        public void subscribe(String itemName, boolean needsIterator)
                    throws SubscriptionException, FailureException {
        }           
    
        public void unsubscribe(String itemName) throws SubscriptionException,
                FailureException {
            if (itemName.equals("greetings") && gt != null) {
                gt.go = false;
            }
        }
    
        class GreetingsThread extends Thread {
    
            private final Object itemHandle;
            public volatile boolean go = true;
    
            public GreetingsThread(Object itemHandle) {
                this.itemHandle = itemHandle;
            }
    
        
    	
    
    
    		public void run() {
                int c = 0;
                Random rand = new Random();
                
    			while(go) {
                    try {
    					ResultSet rs = command.executeQuery("select * from data");       
                        
    					
    					
    					
    					if (rs.next()) {
                            Map<String, String> data = new HashMap<String, String>();
                            data.put("message", rs.getString("message"));
                            data.put("timestamp", rs.getString("timestamp"));
                            listener.smartUpdate(itemHandle, data, false);
    
                        }
                    } catch (Exception ex) {
                        System.out.println(ex);
                    }                    
                    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
    
    }

 

 

Similar Threads

  1. Database Feed
    By mode_vigilante in forum Adapter SDKs
    Replies: 16
    Last Post: January 27th, 2012, 03:58 PM
  2. NonVisualTable - simple tutorial
    By Mone in forum Client SDKs
    Replies: 13
    Last Post: September 9th, 2010, 05:51 PM
  3. Simple Web Server
    By Isanderthul in forum General
    Replies: 1
    Last Post: July 15th, 2009, 09:05 AM
  4. Database datafeed
    By Sinead in forum Adapter SDKs
    Replies: 4
    Last Post: February 14th, 2007, 03:47 PM
  5. Simple Grid Demo Released
    By Alessandro in forum Client SDKs
    Replies: 0
    Last Post: November 24th, 2006, 03:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +1. The time now is 07:00 PM.