reterning selected data from an access database in a java app

Posted on 2005-05-10
Last Modified: 2010-05-18
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");
    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();;
    while (f.isValid()) {
      char c = Window.nextCommand();
      if (c == 's') {
        String[] r = demo.allRunners();
        for (int i = 0; i < r.length; i++) {


thank you
Question by:henrym01
    1 Comment
    LVL 3

    Accepted Solution

    Sorry, but coding this is not done in 10 ten lines of code. You can try the JDBC Tutorial:

    To set up the connection, use:

    // 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( ) {
        System.out.println(r.getString("members <or what you call the field>"));

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


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    NotAlone Challenge 20 65
    scoresIncreasing challenge 10 48
    copyEndy  challenge 15 40
    sumHeights  challenge 17 50
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now