Get Database records instead of fake data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oaklander
    New Member
    • Aug 2007
    • 20

    Get Database records instead of fake data

    I have this part of a class that outputs fake data in my Tomcat 4.17 container.
    Now I want to substitute the fake data for real data that fetches the field value of lastname records from the Oracle database, but not sure how?
    Code:
    public static final int INIT_SIZE = 32;
    
    private String[] strs = null;
    
    public MyContentGenerator() {
    strs = new String[INIT_SIZE];
    for (int i=0; i<INIT_SIZE; i++) {
    //need to change this next line and put database part here
    String str = new String("FakeData"+i);
    strs[i] = str;
    }
    }
    
    public int getTotal() {
    return strs.length;
    }
    
    public ArrayList getContent(int stratIndex, int endIndex) {
    // there is no protecttion of out of bounds
    ArrayList result = new ArrayList();
    for (int i=stratIndex; i<endIndex && i<strs.length; i++) {
    result.add(strs[i]);
    }
    return result;
    }
    Is this in the right direction?
    Code:
    //Database connection to Oracle 9i here....
    Statement stmt = connection.createStatement();
    ResultSet results = stmt.executeQuery("SELECT * from user");
    
    public static final int INIT_SIZE = 32;
    
    private String[] strs = null;
    
    public MyContentGenerator() {
    strs = new String[INIT_SIZE];
    for (int i=0; i<INIT_SIZE; i++) 
    {
       while(results.next())
       {
         String str = results.getString("lastname");
      }
    strs[i] = str;
    }
    }
    
    public int getTotal() {
    return strs.length;
    }
    
    public ArrayList getContent(int stratIndex, int endIndex) {
    
    ArrayList result = new ArrayList();
    for (int i=stratIndex; i<endIndex && i<strs.length; i++) {
    result.add(strs[i]);
    }
    return result;
    }
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by oaklander
    I have this part of a class that outputs fake data in my Tomcat 4.17 container.
    Now I want to substitute the fake data for real data that fetches the field value of lastname records from the Oracle database, but not sure how?
    Code:
    public static final int INIT_SIZE = 32;
    
    private String[] strs = null;
    
    public MyContentGenerator() {
    strs = new String[INIT_SIZE];
    for (int i=0; i<INIT_SIZE; i++) {
    //need to change this next line and put database part here
    String str = new String("FakeData"+i);
    strs[i] = str;
    }
    }
    
    public int getTotal() {
    return strs.length;
    }
    
    public ArrayList getContent(int stratIndex, int endIndex) {
    // there is no protecttion of out of bounds
    ArrayList result = new ArrayList();
    for (int i=stratIndex; i<endIndex && i<strs.length; i++) {
    result.add(strs[i]);
    }
    return result;
    }
    Is this in the right direction?
    Code:
    //Database connection to Oracle 9i here....
    Statement stmt = connection.createStatement();
    ResultSet results = stmt.executeQuery("SELECT * from user");
    
    public static final int INIT_SIZE = 32;
    
    private String[] strs = null;
    
    public MyContentGenerator() {
    strs = new String[INIT_SIZE];
    for (int i=0; i<INIT_SIZE; i++) 
    {
       while(results.next())
       {
         String str = results.getString("lastname");
      }
    strs[i] = str;
    }
    }
    
    public int getTotal() {
    return strs.length;
    }
    
    public ArrayList getContent(int stratIndex, int endIndex) {
    
    ArrayList result = new ArrayList();
    for (int i=stratIndex; i<endIndex && i<strs.length; i++) {
    result.add(strs[i]);
    }
    return result;
    }
    But you tell me one thing what do you mean by Fake Data.
    Will you clarify this then it ll be more easier to solve your problem.

    Kind regards,
    Dmjpro.

    Comment

    • oaklander
      New Member
      • Aug 2007
      • 20

      #3
      Originally posted by dmjpro
      But you tell me one thing what do you mean by Fake Data.
      Will you clarify this then it ll be more easier to solve your problem.

      Kind regards,
      Dmjpro.
      Thanks, fake data for testing. I ended up using ArrayList add and get methods to get it to work.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by oaklander
        Thanks, fake data for testing. I ended up using ArrayList add and get methods to get it to work.
        Your code was basically going in the right direction.
        However, use an ArrayList to store the values rather than a large String[].
        Did you you test it?

        Edit: Also make sure you close all connections, statements,resu ltsets after using them

        Comment

        • oaklander
          New Member
          • Aug 2007
          • 20

          #5
          Originally posted by r035198x
          Your code was basically going in the right direction.
          However, use an ArrayList to store the values rather than a large String[].
          Did you you test it?
          Thanks for your response.
          I actually did use an ArrayList to store the values and I appreciate your input about the large String[] type which I didnt know was smaller than the ArrayList type.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by oaklander
            Thanks for your response.
            I actually did use an ArrayList to store the values and I appreciate your input about the large String[] type which I didnt know was smaller than the ArrayList type.
            So do you have it all working now?

            Comment

            • oaklander
              New Member
              • Aug 2007
              • 20

              #7
              Originally posted by r035198x
              So do you have it all working now?
              Yes, and I closed all connections which I didnt show in my example.

              Comment

              Working...