[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Problem Reading and Sorting a Database

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.io.*;
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 (dbRecordset.next())
                   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 www.ee-stuff.com to upload files for our experts to see and post the URL that's returned here>
  • 4
  • 3
1 Solution
LimpzAuthor Commented:
<removed by GranMod>
Mayank SAssociate Director - Product EngineeringCommented:
Your Pad () method effectively does not do anything - it will return the same String.

Is the DB connection part working fine?
where whave u called Pad() and Proper() functions in your main method ??

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LimpzAuthor Commented:
How come no one respond quickly to the "solution" to the  problem . Sorry i was asking for the solution not comment. best regards.....Limpz
LimpzAuthor Commented:
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
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
LimpzAuthor Commented:
Oka-y  what i mean was when i compile my program on sorting and reading database this one appears :

A19010.java:109: cannot find symbol
symbol  : variable stationName
location: class A19010
                     strOut = stationName.toUpperCase();
A19010.java:110: cannot find symbol
symbol  : variable city
location: class A19010
                        strOut = city.toUpperCase();
2 errors
Mayank SAssociate Director - Product EngineeringCommented:
That's because you need to define them, like:

private String stationName ;

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now