reterning selected data from an access database in a java app

hello there

im trying to create a small java app which connects to a microsoft access database and then retrives certain data from it.  I'm a bit of a newb to java so i realy need some help.

At the minute my program returns a list of all the "members" in the "members" table. what i need is for the user to select a race by typing in the "race_name" and then have a list of all the results of that race (eg 1st, 2nd, 3rd, etc) be returned...the "race_result" table contains a column called "race_name" (there are 2 races and their name is repeated as all the results for those races are displayed (eg 1st, 2nd, 3rd, etc)...the results are in the "race_result" column.

i hope you can understand this poor description and provide me with the code to do this. below is the code i have made so far:

import database.*;      // the database package
import jewl.*;          // user interface stuff
import java.util.Date;  // the standard Java class for dates

public class Demo {
 
  private DBLink db = new DBLink("C:\\db1.mdb");

  public String[] allRunners () {
   
    DBQuery q = new DBQuery(db,"members");
   
    q.orderBy("Membership_ID");
   
    DBRow[] r = q.getData();
   
    String[] s = new String[r.length];
    for (int i = 0; i < r.length; i++) {
      String mID  = r[i].getString("Membership_ID");
      String finame = r[i].getString("First_name");
      String faname = r[i].getString("Family_name");
      s[i] = mID + " " + finame + " " +faname;
    }
    return s;
  }  

  /**
   *  A main program which provides a graphical user interface for the demo.
   */
  public static void main (String args[]) {
    //
    //  Create the user interface
    //
    Frame f = new Frame (500, 400, "Demo", 'x');
    Menu  m = new Menu (f, "Select");
    MenuItem m1 = new MenuItem (m, "Runners", 's');
    ListBox results = new ListBox (f, 5,5, -10, -10);
   
    Demo demo = new Demo();
    f.show();
    while (f.isValid()) {
      char c = Window.nextCommand();
      if (c == 's') {
       
        String[] r = demo.allRunners();
        results.clear();
        for (int i = 0; i < r.length; i++) {
          results.appendLine(r[i]);
        }
      }

        }
      }
    }

thank you
henrym01Asked:
Who is Participating?
 
Mig-OCommented:
Sorry, but coding this is not done in 10 ten lines of code. You can try the JDBC Tutorial: http://java.sun.com/docs/books/tutorial/jdbc/.

To set up the connection, use:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// set this to a MS Access DB you have on your machine
String filename = "C:/db1.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end
// now we can get the connection from the DriverManager
Connection con = DriverManager.getConnection( database ,"","");

Then you can do something like:

Statement s = con.createStatement()
RecordSet r = s.executeQuery("SELECT * from members");
while( r.next() ) {
    System.out.println(r.getString("members <or what you call the field>"));
}

Don't know if everythings spelled right, read the api please.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.