|
|
min = first value; while ( there are more values ) { if ( next value < min ) min = next value; } |
/* ========================================= Read in query ========================================= */ Scanner in = new Scanner(System.in); System.out.print("Enter an SQL query: "); String query = in.nextLine( ); // Read in a query .... (code for JDBC initialization omitted for brevity) .... try { ResultSet rset = stmt.executeQuery(query); // Execute the query /* ================================ Check if rset is empty ================================ */ if ( rset.next( ) == false ) { System.out.println("Empty rset set. Done."); System.exit(1); } min = rset.getString(1); // First value while ( rset.next( ) ) { if ( rset.getString(1).compareTo(min) < 0 ) min = rset.getString(1); // Next value is smaller } /* =============================== Print the minimum found =============================== */ System.out.println("Min " + meta.getColumnName(1) + " = " + min); } catch (Exception e) { System.out.println(e.getMessage()); // Print the error message } |
Program: /home/cs377001/demo/JDBC/UseMetaData1.java
Compile: javac -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData1.java
Run: java -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData1
Example:
java -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData1
Enter an SQL query: select fname from employee
Attribute values of the first attribute fname:
John Frankl Alicia Jennif Ramesh Joyce Ahmad James
Min fname = Ahmad
However:
java -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData1
Enter an SQL query: select salary from employee
Attribute values of the first attribute salary:
9999.00 40000.00 25000.00 43000.00 38000.00 25000.00 25000.00 55000.00
Min salary = 25000.00
|
Problem:
|
We must write a separate program to find the minimum for decimal/double typed attributes:
Program: /home/cs377001/demo/JDBC/UseMetaData2.java /* ================================ Check if rset is empty ================================ */ if ( rset.next( ) == false ) { System.out.println("Empty rset set. Done."); System.exit(1); } double min = rset.getDouble(1); while ( rset.next( ) ) { if ( rset.getDouble(1) < min ) min = rset.getDouble(1); } |
|
Program: /home/cs377001/demo/JDBC/UseMetaData2.java Compile: javac -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData3.java Run: java -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData3 Example: java -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData3 Enter an SQL query: select fname from employee Attribute values of the first attribute fname: John Frankl Alicia Jennif Ramesh Joyce Ahmad James Min fname = Ahmad Furthermore: java -cp ".:/home/cs377001/lib/mysql-connector-java.jar" UseMetaData3 Enter an SQL query: select salary from employee Attribute values of the first attribute salary: 9999.00 40000.00 25000.00 43000.00 38000.00 25000.00 25000.00 55000.00 Min salary = 9999.00 |