Java Reference
In-Depth Information
LISTING 18.1
The Full Text of CoalReporter.java
1: import java.sql.*;
2:
3: public class CoalReporter {
4: public static void main(String[] arguments) {
5: String data = “jdbc:odbc:WorldEnergy”;
6: try {
7: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
8: Connection conn = DriverManager.getConnection(
9: data, “”, “”);
10: Statement st = conn.createStatement();
11: ResultSet rec = st.executeQuery(
12: “SELECT * “ +
13: “FROM Coal “ +
14: “WHERE “ +
15: “(Country='” + arguments[0] + “') “ +
16: “ORDER BY Year”);
17: System.out.println(“FIPS\tCOUNTRY\t\tYEAR\t” +
18: “ANTHRACITE PRODUCTION”);
19: while(rec.next()) {
20: System.out.println(rec.getString(1) + “\t”
21: + rec.getString(2) + “\t\t”
22: + rec.getString(3) + “\t”
23: + rec.getString(4));
24: }
25: st.close();
26: } catch (SQLException s) {
27: System.out.println(“SQL Error: “ + s.toString() + “ “
28: + s.getErrorCode() + “ “ + s.getSQLState());
29: } catch (Exception e) {
30: System.out.println(“Error: “ + e.toString()
31: + e.getMessage());
32: }
33: }
34: }
This program must be run with a single argument specifying the Country field in the
database from which to pull records, as in this example for the JDK:
java CoalReporter Poland
If the application were run with an argument of Poland , the output from the sample data-
base would be the following:
FIPS COUNTRY YEAR ANTHRACITE PRODUCTION
PL Poland 1990 0.0
PL Poland 1991 0.0
PL Poland 1992 0.0
PL Poland 1993 174.165194805424
 
Search WWH ::




Custom Search