Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Fetch read database info instead of fake content

Thread view: 
teser3@hotmail.com - 28 Aug 2007 01:06 GMT
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[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?

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[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;
}
Manish Pandit - 28 Aug 2007 05:29 GMT
On Aug 27, 5:06 pm, "tes...@hotmail.com" <tes...@hotmail.com> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.