Geek question (very)

What's Hot
2»

Comments

  • Phil_aka_PipPhil_aka_Pip Frets: 9794
    Myranda said:
    next week is Assembly... then next month C... 
    You'll have fun! Enjoy :)
    "Working" software has only unobserved bugs. (Parroty Error: Pieces of Nine! Pieces of Nine!)
    Seriously: If you value it, take/fetch it yourself
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • joeyowenjoeyowen Frets: 4025
    Firstly, is there a reason String isn't declared globally and then referenced?  And why is 44 declared again?

    (feel free to ignore me btw, and good luck learner the other languages! It's the best thing I ever did :) )
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • MyrandaMyranda Frets: 2940
    Myranda said:
    next week is Assembly... then next month C... 
    You'll have fun! Enjoy :)
    The Assembly is to make a toy game on a dev board... 

    the C is for robotics (and I love robotics so that's cool)
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • MyrandaMyranda Frets: 2940
    joeyowen said:
    Firstly, is there a reason String isn't declared globally and then referenced?  And why is 44 declared again?

    (feel free to ignore me btw, and good luck learner the other languages! It's the best thing I ever did :) )
    The extra "String" declaration is a copy and paste error.

    the String could be declared anywhere... but I don't want to write out the keys for all database entries in every class for every text field that needs it... I need a way to extract the key... so I can make it list the keys, so I can get all the information for all the fields in a less clunky way.
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • GIJoeGIJoe Frets: 213
    I'm not sure of your database structure, but could you not do something like this:

    SELECT key, name, price, quantity FROM table;

    That way, there is only one call the whole lot - much more efficient,

    OR, if the data is in different tables:

    SELECT A.key, B.name, b.price, b.quantity
    FROM table1 A
    LEFT JOIN table2 B ON (B.key = A.key);

    "Nobody is really researching robot jokes"

    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • MyrandaMyranda Frets: 2940
    I'm probably not explaining very well...

    All the SQL commands are being handled in the database handler... in that I can do all the SQL statments I want and need... calling from other classes though I am using the primary key to call each line... but I have no code to call the keys from the database to the other classes... except by writing the key...

    I mean curruntly I'd be making it 

    key = "11";
    debug.append (StockData.getKey(key)) ;

    if I did that I'd be able to get the key, but as you can see it would be redundant
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • digitalscreamdigitalscream Frets: 26647
    edited March 2016
    OK, stop and think for a sec - 

    String key ="00";
    debug.append(" " + newline);
    debug.append(" " + StockData.getName(key));
    debug.append(" " + StockData.getPrice(key)); 
    debug.append(" " + StockData.getQuantity(key));

    With this bit of code, you're running a query every single time you just want to get a field from a row you've already pulled back; that's incredibly inefficient and slow to run. What you want to do is create a class called StockItem (for example), which holds three attributes - Name, Price and Quantity. Then, in your database handler, have a method called getStockItem, which returns an object of class StockItem containing all three fields.

    For ease-of-access, put a static method in StockItem called get(), which talks to the database handler and returns a StockItem object.

    Then the above code becomes something like this:

    StockItem stockItem = StockItem.get(11);
    debug.append(" " + newline);
    debug.append(" " + stockItem.name);
    debug.append(" " + stockItem.price; 
    debug.append(" " + stockItem.quantity);

    That should make things a lot neater if you just want a single record from the database.

    As for getting all the StockItems from the database, I'd say you want something in your database handler like this:

    public static String getAllStockItems() {
            try {

                ResultSet res = stmt.executeQuery("SELECT * FROM StockDB");
                ArrayList stockItems = new ArrayList();
                while (res.next()) { // there is a result
                    StockItem s = new StockItem();
                    s.name = res.getString(2);
                    s.price = res.getString(3);
                    s.quantity = res.getString(4);
    stockItems.add(s);
                }
                return stockItems;
            } catch (SQLException e) {
                System.out.println(e);
                return null;
            }
        }

    ...and a wrapper method in your StockItem class that calls it. That solves the "code organisation" issue. Then your output code looks something like this:

    ArrayList stockItemArray = StockItem.getAllStockItems();
    // Iterates over the ArrayList, in case you haven't come across this syntax before
    for (StockItem stockItem : stockItemArray) {
      debug.append(" " + newline);
      debug.append(" " + stockItem.name);
      debug.append(" " + stockItem.price; 
      debug.append(" " + stockItem.quantity);
    }

    That will print out everything in the stockItemArray, which in turn was the result of pulling everything back from the StockDB table in the database.

    Again, I haven't tested any of this and it's all done from my memory of using Java years ago. It may or may not help - I don't know what the constraints on the assignment were.
    <space for hire>
    0reaction image LOL 0reaction image Wow! 1reaction image Wisdom
  • digitalscreamdigitalscream Frets: 26647
    The point of the above, by the way, is to avoid the necessity of knowing what all the keys are in advance of getting the records from the database - after all, the only way to find them is to query the database (Catch-22). In order to have that list, you'd have to get all the records from the database, so why bother running multiple queries when you can do it all in one go?
    <space for hire>
    0reaction image LOL 0reaction image Wow! 1reaction image Wisdom
  • DarnWeightDarnWeight Frets: 2566
    Yeah, as in the two posts above from @digitalscream, keeping the data access layer/handler code as a completely separate class, and defining classes for the objects you're pulling from the DB is definitely the proper OO way of going about it.  Your stockItem object's getName function should be returning a simple Property value (object.Name) from your object.  If you're being pernickity (or full-on OO) the name should be a privately scoped variable of the stockItem, and should only publicly exposed by get and set methods of the Name property...seems like extra code for no real benefit, but it allows for read-only properties and the like.

    It would be fairly easy to overload the getAllStockItems function so that you had another version that takes a key as parameter string...that way you could return all items, or just one item (if you have the key value), using pretty much the same function call.

    Once you have an array/arrayList of stockItems you can then start doing niftier things like iterating through them and retrieving, for example, all items between price A and price B.

    Love OO myself, but stuck working in a pure scripting environment at the moment, so don't get much scope to do things "the right way" any more.
    New fangled trading feedback link right here!
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • MyrandaMyranda Frets: 2940
    OK, stop and think for a sec - 

    String key ="00";
    debug.append(" " + newline);
    debug.append(" " + StockData.getName(key));
    debug.append(" " + StockData.getPrice(key)); 
    debug.append(" " + StockData.getQuantity(key));

    With this bit of code, you're running a query every single time you just want to get a field from a row you've already pulled back; that's incredibly inefficient and slow to run. What you want to do is create a class called StockItem (for example), which holds three attributes - Name, Price and Quantity. Then, in your database handler, have a method called getStockItem, which returns an object of class StockItem containing all three fields.

    For ease-of-access, put a static method in StockItem called get(), which talks to the database handler and returns a StockItem object.

    Then the above code becomes something like this:

    StockItem stockItem = StockItem.get(11);
    debug.append(" " + newline);
    debug.append(" " + stockItem.name);
    debug.append(" " + stockItem.price; 
    debug.append(" " + stockItem.quantity);

    That should make things a lot neater if you just want a single record from the database.

    As for getting all the StockItems from the database, I'd say you want something in your database handler like this:

    public static String getAllStockItems() {
            try {

                ResultSet res = stmt.executeQuery("SELECT * FROM StockDB");
                ArrayList stockItems = new ArrayList();
                while (res.next()) { // there is a result
                    StockItem s = new StockItem();
                    s.name = res.getString(2);
                    s.price = res.getString(3);
                    s.quantity = res.getString(4);
    stockItems.add(s);
                }
                return stockItems;
            } catch (SQLException e) {
                System.out.println(e);
                return null;
            }
        }

    ...and a wrapper method in your StockItem class that calls it. That solves the "code organisation" issue. Then your output code looks something like this:

    ArrayList stockItemArray = StockItem.getAllStockItems();
    // Iterates over the ArrayList, in case you haven't come across this syntax before
    for (StockItem stockItem : stockItemArray) {
      debug.append(" " + newline);
      debug.append(" " + stockItem.name);
      debug.append(" " + stockItem.price; 
      debug.append(" " + stockItem.quantity);
    }

    That will print out everything in the stockItemArray, which in turn was the result of pulling everything back from the StockDB table in the database.

    Again, I haven't tested any of this and it's all done from my memory of using Java years ago. It may or may not help - I don't know what the constraints on the assignment were.
    The array I need... is just the stockID labels... so I can re-use the ID to call the various methods over and over... or something like it.

    In my CheckStock class I've made a spreadsheet like set of text boxes to break up and display the information better... but that means 5 text fields, one each for the four data sets, and one for a total value of the stock (so price x quantity)... so I need the data from the database separately. 

    In the PurchaseStock and UpdateItem classes I want to have a drop-down list for the items in the database... which means I want all the keys so I can list the whole lot as descriptions in the drop down...

    Improbably not explaining well :( 
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • MyrandaMyranda Frets: 2940
    Part of the problem is that the lectures on database stuff was given by one of the most boring monotone people ever... so very little sank in.
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • digitalscreamdigitalscream Frets: 26647
    edited March 2016
    Myranda said:
    The array I need... is just the stockID labels... so I can re-use the ID to call the various methods over and over... or something like it.

    In my CheckStock class I've made a spreadsheet like set of text boxes to break up and display the information better... but that means 5 text fields, one each for the four data sets, and one for a total value of the stock (so price x quantity)... so I need the data from the database separately. 

    In the PurchaseStock and UpdateItem classes I want to have a drop-down list for the items in the database... which means I want all the keys so I can list the whole lot as descriptions in the drop down...

    Improbably not explaining well :( 
    OK, fair enough - in that case, you do exactly as I said above. Implement those methods, create the StockItem class, and then use getAllStockItems() to return an array you can use to fill your dropdown.

    There is an omission in my description - as well as the name/price/quantity attributes, the StockItem class also needs to implement the key attribute (ie the primary key...which, according to modern-day conventions, would be called Id or StockItemId). Once you've got an ArrayList of StockItems, then you can use it to do whatever you want.

    The point is that you need to get that array and iterate over it instead of requiring prior knowledge of all the possible primary key values.

    The other key part (no pun intended) is that thinking in object-oriented terms, you have to let go of the idea of "I'm manipulating data" and embrace the "I'm using objects pre-filled with everything to represent my data" approach. Yes, it means that you're going to be pulling back more data than you strictly need for the job you're doing at the time, but your code is so much easier to understand and more predictable to use that it's worth a few CPU cycles. Not only that, but the system will spend a lot less time running queries, which - overall - results in a lot less CPU time in the vast majority of applications.
    <space for hire>
    0reaction image LOL 0reaction image Wow! 1reaction image Wisdom
  • HeartfeltdawnHeartfeltdawn Frets: 22190
    To counteract the resoundingly sensible/geeky/boring discussions (delete as applicable) occurring within this thread, I am now going to put up a picture of a big pair of tits. 




    0reaction image LOL 0reaction image Wow! 1reaction image Wisdom
  • MyrandaMyranda Frets: 2940
    OK...  so in CheckStock I did this:

                    ResultSet keyList = StockData.getID();
                    while (keyList.next())
                    {
                        debug.append(keyList.getString(1));
                    }

    Which calls 

    public static ResultSet getID() 
    {
            try
    {
                ResultSet keyList = stmt.executeQuery("SELECT stockID FROM StockDB");
                //System.out.println(IDResult.getString(1));
                return keyList;
    }
    catch (SQLException e)
    {
    System.out.println(e);
    return null;
    }

    which has given me all the primary keys in the class I need them in, in the form of a ResultSet list... which after a break for food I should be able to use to iterate through, using the positions as the key used in my other code... meaning the database tells every other class how big it is and what the ID numbers are, so no matter how many entities were in the database it will all work... I think

    As long as I can get the ResultSet rules well enough to turn them into the key each time ...

    Yay.

    All I have to do now, is implement it all, sort out the UI, write a 2000 word report on it, and submit it in 18 hours.
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • thomasross20thomasross20 Frets: 4437
    chrispy108;1008514" said:
    Bit rich from a man who asks us how to buy jeans and t-shirts!

    Afraid I'm not geek enough for this Myranda! Good luck
    Jesus nothing goes undetected on this forum!

    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
  • thomasross20thomasross20 Frets: 4437
    I guess there a lot of Kodors on here.
    0reaction image LOL 0reaction image Wow! 0reaction image Wisdom
Sign In or Register to comment.