Problem  Reading and Sorting a Database

Posted on 2006-05-12
Last Modified: 2010-03-31
I am having trouble reading and sorting  my work ( It seems that i am having problem with the private String Pad and Proper functions( ) . Here is the code...) The database(weather.mdb) file can be read by access although i could not convert because  of the absence of  Microsoft access in my system. Attached also is the sample output  stations.txt and raw weather.mdb file....

import java.sql.*;
import java.util.Scanner;

      < h2 > A19010 </h2>
      Demonstrates the use of JAVA ODBC to
      connect to a database using SQL Commands.
      @programmed by: Aestipona
      @Java 190, 4/30/2006
      @Professor John Couture,Instructor
      public class A19010
       public static void main(String[]args)
        Status("Starting A19010");
        Connection dbConn; // Setup connection with the OS
        Statement dbCmdText; //Contains SQL commands
        String dbSQL; // Temp String for building SQL command
        ResultSet dbRecordset;
        Status("Attempting to establish a database connection");
         // Generic SUN driver for databases
            Befoer running this, we have to setup a connection
            to the database using the operating system utilities
            In this assignment,we are calling the connection "BMAC"
            for Black Mountain Aviation Center.
            dbConn = DriverManager.getConnection("jdbc:odbc:BMAC");
            // if you cannot find the database, hadle it here
            if(dbConn == null) // Is the Connection working ?
              Status("Connection to database failed");
          Status("Connection successful");
             // We are going to communicate with the database
             // Using a SQL command string
             dbCmdText = dbConn.createStatement();
             // Build the SQL string that we need
             dbSQL = "Select + from Station ";
             // Now run it
             Status("Executing SQL- " + dbSQL);
             dbRecordset = dbCmdText.executeQuery(dbSQL);
             // .next() reads the next record and reports
             // if it was successful
             while (
                   FileWriter fw = new FileWriter("stations.txt");
                  BufferedWriter bw = new BufferedWriter(fw);
                  PrintWriter outFile = new PrintWriter(bw);
                  // get a specified field
                  outFile.print (dbRecordset.getString("stationID").toUpperCase()+ "");
                  outFile.println(dbRecordset.getString("stationName")  );
                  outFile.print(dbRecordset.getString ("city") );
                  outFile.println(" ");
              }// end of while
             }// end of else
             Status("Closing the database");
             dbCmdText = null;
            } // end of try
              catch (Exception myExceptionVariable)
              } // end of catch
             Status("A19010 is ending");
}      // end of main
   /** The purpose of this routine is to display a message on the screen as program
          progresses during debugging---when the program is ready for production, you
             simply comment out this System.out line.
             Also putting "main: " in front of message, you can identify where the message
             came from in case you have Status() functions
             in many different classes.
            public  static void Status(String strMsg)
             System.out.println("Main:" + strMsg);

                  private  static String Proper(String strIn)
                   String strOut;
                   char charArray[] = strIn.toCharArray();
                   for (int i = 0; i < strIn.length(); i++)
                     strOut = stationName.toUpperCase();
                        strOut = city.toUpperCase();
                return strIn;

 private static String Pad( String s, int length)
   String strVar = "";                                        
   strVar = s + strVar;
   return strVar.substring(0,length);



Sample STATIONS.TXT file
YKM Yakima Air Terminal/Mcal Yakima      WA
SBA Santa Barbara Muni Airpo Santa Barba CA
SFO San Francisco Internatio San Francis CA
AST Astoria Regional Airport Astoria     OR
IMB Kimberly Station         Kimberly    OR
... etc (however, you need to sort by City)

Attached is the (weather.mdb) :
<removed by GranMod, please use to upload files for our experts to see and post the URL that's returned here>
Question by:Limpz

    Author Comment

    <removed by GranMod>
    LVL 30

    Accepted Solution

    Your Pad () method effectively does not do anything - it will return the same String.

    Is the DB connection part working fine?
    LVL 3

    Expert Comment

    where whave u called Pad() and Proper() functions in your main method ??


    Author Comment

    How come no one respond quickly to the "solution" to the  problem . Sorry i was asking for the solution not comment. best regards.....Limpz

    Author Comment

    How would the statement look like if  i try to  create   or should accept a connection string  that would create an ODBC Database, return  true if successful , false if not  .

                  Using this :     Public boolean open(String strConn) throws Exception

    best regards
    LVL 30

    Expert Comment

    >> How come no one respond quickly to the "solution" to the  problem . Sorry i was asking for the solution not comment

    I don't understand what you mean. Until and unless you clarify certain points, it is difficult for us to give the correct answer in the first shot itself.

    Author Comment

    Oka-y  what i mean was when i compile my program on sorting and reading database this one appears : cannot find symbol
    symbol  : variable stationName
    location: class A19010
                         strOut = stationName.toUpperCase();
                                        ^ cannot find symbol
    symbol  : variable city
    location: class A19010
                            strOut = city.toUpperCase();
    2 errors
    LVL 30

    Expert Comment

    That's because you need to define them, like:

    private String stationName ;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    This video teaches viewers about errors in exception handling.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    760 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

    10 Experts available now in Live!

    Get 1:1 Help Now