Link to home
Start Free TrialLog in
Avatar of Limpz
Limpz

asked on

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");
        
        try
        {
         // Generic SUN driver for databases
               Class.forName("sun.jdbc.odbc.JdbcDriver");
            
            /**
            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");
            }
            else
            {
          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.print(dbRecordset.getString("state").toUpperCase());
                  outFile.println(" ");
                  outFile.close();
              }// end of while
                                 dbRecordset.close();
             }// end of else
             Status("Closing the database");
             dbConn.close();
             dbCmdText = null;
            } // end of try
              catch (Exception myExceptionVariable)
              {
                Status(myExceptionVariable.toString());
              } // 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.
             <hr>
             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>
Avatar of Limpz
Limpz

ASKER

<removed by GranMod>
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
where whave u called Pad() and Proper() functions in your main method ??

-KuTtZ
Avatar of Limpz

ASKER

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

ASKER

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
>> 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.
Avatar of Limpz

ASKER

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
.
That's because you need to define them, like:

private String stationName ;