Khanh_huu
asked on
Java - how to compare user's input with Microsoft Access database
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.od bc.JdbcOdb cDriver");
con = DriverManager.getConnectio n("jdbc:od bc:project ");
}
catch(java.lang.ClassNotFo undExcepti on e)
{
System.err.print("ClassNot FoundExcep tion: ");
System.err.println(e.getMe ssage());
}
catch(SQLException ex)
{
System.err.print("SQLExcep tion: ");
System.err.println(ex.getM essage());
}
} //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();
}
}
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.od
con = DriverManager.getConnectio
}
catch(java.lang.ClassNotFo
{
System.err.print("ClassNot
System.err.println(e.getMe
}
catch(SQLException ex)
{
System.err.print("SQLExcep
System.err.println(ex.getM
}
} //end of public jdbc_test()
// Read input from the keyboard
public void readScan() throws Exception
{
System.out.println("Please
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,
}
}
}
// 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[
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>Not much help,...
You could answer to our post.
About the accepting, it's little strange.
orangehead911 tol something,
Giant2 told: Agree with orangehead911...
stmani2005 told: i agree with giant...
Why accepting this last one?
You could answer to our post.
About the accepting, it's little strange.
orangehead911 tol something,
Giant2 told: Agree with orangehead911...
stmani2005 told: i agree with giant...
Why accepting this last one?
ASKER
I do apologize, I'm new to this; therefore, I didn't know I can split up the points.
;-D
ASKER