Fetch read database info instead of fake content

T

teser3

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;
}
 
M

Manish Pandit

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top