Fetch read database info instead of fake content

Discussion in 'Java' started by teser3@hotmail.com, Aug 28, 2007.

  1. Guest

    I have this part of a java program that outputs fake data.
    Now I want to substitute that for database that fetches something
    called lastname records from the databse, but not sure how?

    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++) {
    String str = new String("FakeData"+i);
    strs = 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);
    }
    return result;
    }

    Is this in the right direction?

    Class.forName("org.gjt.mm.mysql.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://
    localhost/mydbhere?user=jones&password=thepasswordhere");
    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 = 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);
    }
    return result;
    }
     
    , Aug 28, 2007
    #1
    1. Advertising

  2. On Aug 27, 5:06 pm, "" <> wrote:
    > I have this part of a java program that outputs fake data.
    > Now I want to substitute that for database that fetches something
    > called lastname records from the databse, but not sure how?
    >


    Per my understanding, you answered your own question.

    Is the code not working right, or are you getting any exceptions ?

    (There can be some things that can be refactored, like combining the
    strs[] initialization, replacing "select *" with "select firstname",
    externalizing the database connection string and driver, etc.)

    -cheers,
    Manish
     
    Manish Pandit, Aug 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pitmairen
    Replies:
    4
    Views:
    535
    Bruno Desthuilliers
    Mar 6, 2006
  2. Noel Mosa

    to fake read() function

    Noel Mosa, Sep 11, 2006, in forum: C Programming
    Replies:
    2
    Views:
    281
    those who know me have no need of my name
    Oct 2, 2006
  3. Fetch session info

    , Jun 24, 2008, in forum: Java
    Replies:
    0
    Views:
    321
  4. prabesh shrestha

    fetch content from google results

    prabesh shrestha, Sep 28, 2008, in forum: Java
    Replies:
    33
    Views:
    2,270
  5. Kenshin
    Replies:
    1
    Views:
    239
    Jeff Cochran
    Jun 25, 2004
Loading...

Share This Page