Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using real database instead of fake content 2

Status
Not open for further replies.

dreampolice

Technical User
Dec 20, 2006
85
US
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?
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 part only 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;
}
 
Your code won't work because the first execution in the for (int i=0; i<INIT_SIZE; i++) loop has already read all entries in the Resultset. The second run will therefore produce no results because results.next() == false. Furthermore String str is a local variable in the while loop, which means that the for loop cannot access it. There is also no need to read your results first in an String array and then copy the String array in an ArrayList.


private ArrayList contents = new ArrayList();

public MyContentGenerator() throws Exception {
// fill in the oracle drive name and connection string
Class.forName("......");
Connection con = DriverManager.getConnection(".....");

Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("select * from user");
while (rs.next()) {
contents.add(rs.getString("lastname"));
}
rs.close();
stat.close();
con.close();
}

public int getTotal() {
return contents.size();
}

public ArrayList getContent() {
return contents;
}
 
Addition to my previous message:

public ArrayList getContent(int start, int finish) {
ArrayList result = new ArrayList();
for(int i=start; i<finish && i < contents.size(); i++) {
result.add(contents.get(i));
}
return result;
}


Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top