troubleshooting Question

Java - how to compare user's input with Microsoft Access database

Avatar of Khanh_huu
Khanh_huu asked on
Java
8 Comments4 Solutions1791 ViewsLast Modified:
Hello,

I need your help with this Java program. I'm trying to read the input from the keyboard and compare it to the data queried from the MS Access database. I was able to query the database and display it; however, I'm having problems getting the user's input (from the keyboard) to compare it with the database. Goal: If the user's input matches with the database, then displays the result. If the user's input does not match, display "Sorry, No record found). Thanks in advance.

HERE IS MY PROGRAM:

//output.java

      import java.sql.*;
     
        public class jdbc_test  {

        Connection con;
        String query = "SELECT CustomerID FROM Customers";
        Statement stmt;
        ResultSet rs;
        String input;
      String [] values;
       
        public jdbc_test()
       {
      try {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           con = DriverManager.getConnection("jdbc:odbc:project");
          }
              catch(java.lang.ClassNotFoundException e)
             {
           System.err.print("ClassNotFoundException: ");
           System.err.println(e.getMessage());
          }

              catch(SQLException ex)
             {
          System.err.print("SQLException: ");
          System.err.println(ex.getMessage());
          }

       } //end of public jdbc_test()

          // Read input from the keyboard
          public void readScan()      throws Exception
              {
                     System.out.println("Please Scan Your Membership Card: ");                            
                     BufferedReader a = new BufferedReader (new InputStreamReader (System.in));                 
                          input = a.readLine();
          }
         
            // compare input with data queried
               public void compare() throws Exception
            {
                  for(int j=0; j<10; j++)
               {
               
               // display the data if input matches with data queried
                  if (input == values[j])
                   {
                        getValues();
                        result();
                   }
                  
                  else
                   {
                        System.out.println("SORRY, NO RECORD FOUND");
                   }
               }
            }                          
        
         // get data from Microsoft Access database
       public String [] getValues()
      {
       String [] myresponse = null;
     
      try
         {
         myresponse = new String[10];  // for now lets just keep 10 slots
           stmt = con.createStatement();                                          
           rs = stmt.executeQuery(query);

         int i = 0;
         while(rs.next())
         {
            myresponse[i] = rs.getString("CustomerID");
            i++;          
          }
           }
         catch( Exception e )
          {
            myresponse = null;
          }
         return myresponse;  
       }//end of getValues
        
        
          public void close()
       {
           try
           {
              con.close();
             }
      
           catch ( Exception e ) {};
       }
               
            // display results if input matches data queried
         public void result()            throws Exception
            {
        System.out.println("Here are the values:");
        jdbc_test connection = new jdbc_test();
       
        values = connection.getValues();
        for (int i = 0; i < values.length; i++)
            {
          System.out.println(values[i]);
         }
        connection.close();
        }
     }

       // functions calling
       public void functions()      throws Exception
            {
                  readScan();
                  compare();
                  close();
            }  
}  
                 
  public class output
      {
            
      public static void main(String args[]) throws Exception
        {                 
       
                jdbc_test func = new jdbc_test();
                func.functions();
                
               }
      }
ASKER CERTIFIED SOLUTION
Tommy Braas

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 4 Answers and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros