1: import java.sql.*;
2: import java.util.Properties;
4: class Test {
6: static String url = "jdbc:easysoft://demo.easysoft.com/pubs";
7: static String query = "tables";
8: static String user = "demo";
9: static String password = "easysoft";
10: static String logonuser = "";
11: static String logonpassword = "";
12: static int resultSetLimit = 50;
15: public static void usage() {
16: System.out.println("Usage: \n" +
17: " Test -url <Easysoft JDBC url>\n" +
18: " -user <user name>\n" +
19: " -password <passrord>\n" +
20: " -logonuser <server access user name>\n" +
21: " -logonpassword <server access password>\n" +
22: " -sql <SQL query or the string \"tables\">\n" +
23: " -l <limit on the number of records retrieved>\n"
24: );
25: }
26: public static void main (String args[]) {
27:
28:
29: if (args.length>0) {
30: for (int i = 0; i < args.length; i++) {
31: if (args[i].equals("")){
32: // skip
33: if (i + 1 < args.length) {
34: i++;
35: }
36: } else if (args[i].equals("-url")) {
37: if (i + 1 < args.length) {
38: i++;
39: url = args[i];
40: }
41: } else if (args[i].equals("-user")) {
42: if (i + 1 < args.length) {
43: i++;
44: user = args[i];
45: }
46: } else if (args[i].equals("-password")) {
47: if (i + 1 < args.length) {
48: i++;
49: password = args[i];
50: }
51: } else if (args[i].equals("-logonuser")) {
52: if (i + 1 < args.length) {
53: i++;
54: logonuser = args[i];
55: }
56: } else if (args[i].equals("-logonpassword")) {
57: if (i + 1 < args.length) {
58: i++;
59: logonpassword = args[i];
60: }
61: } else if (args[i].equals("-sql")) {
62: if (i + 1 < args.length) {
63: i++;
64: query = args[i];
65: }
66: } else if (args[i].equals("-l")) {
67: if (i + 1 < args.length) {
68: i++;
69: resultSetLimit = new Integer(args[i]).intValue();
70: }
71: } else if (args[i].equals("-h")) {
72: usage();
73: System.exit(0);
74: }
75: }
76: }
77: else {
78: usage();
79: System.exit(0);
80: }
81: try {
82: // Load the Easysoft JDBC=ODBC Bridge Driver
83: Class.forName ("easysoft.sql.jobDriver");
85: Connection con;
87: // Attempt to connect to a driver. Each one
88: // of the registered drivers will be loaded until
89: // one is found that can process this URL
90: // If additional connection properties need to be used,
91: // create a Properties object to pass them into the connect
92: if ( logonuser.length() > 0 || logonpassword.length() > 0 ) {
93: Properties p = new Properties();
95: p.put( "user", user );
96: p.put( "password", password );
98: if ( logonuser.length() > 0 ) {
99: p.put( "logonuser", logonuser );
100: }
102: if ( logonpassword.length() > 0 ) {
103: p.put( "logonpassword", logonpassword );
104: }
106: con = DriverManager.getConnection (url, p );
107: }
108: else {
109: con = DriverManager.getConnection (url,
110: user,
111: password );
112: }
114: // If we were unable to connect, an exception
115: // would have been thrown. So, if we get here,
116: // we are successfully connected to the URL
118: // Check for, and display and warnings generated
119: // by the connect.
120: checkForWarning (con.getWarnings ());
122: // Get the DatabaseMetaData object and display
123: // some information about the connection
124: DatabaseMetaData dmd = con.getMetaData ();
125: String info =
126: "JDBC Driver Name: : " + dmd.getDriverName() + "\n" +
127: "JDBC Driver Version: "
128: + dmd.getDriverVersion() + "\n" +
129: "URL: " + dmd.getURL() + "\n" +
130: "User Name: " + dmd.getUserName() + "\n" +
131: "Database Product Name: "
132: + dmd.getDatabaseProductName() + "\n" +
133: "Database Product Version: "
134: + dmd.getDatabaseProductVersion() + "\n";
135: System.out.println(info);
136: System.out.println("");
138: // Create a Statement object so we can submit
139: // SQL statements to the driver
140: Statement stmt = con.createStatement ();
142: // Submit a query, creating a ResultSet object
143: ResultSet rs = stmt.executeQuery (query);
145: // Display all columns and rows from the result set
146: showResultSet (rs);
148: // Close the result set
149: rs.close();
151: // Close the statement
152: stmt.close();
154: // Close the connection
155: con.close();
157: System.exit(1);
158: }
159: catch (SQLException ex) {
161: // A SQLException was generated. Catch it and
162: // display the error information. Note that there
163: // could be multiple error objects chained
164: // together
165: System.out.println ("\n*** SQLException caught ***\n");
167: while (ex != null) {
168: System.out.println ("SQLState: " +
169: ex.getSQLState ());
170: System.out.println ("Message: " +
171: ex.getMessage ());
172: System.out.println ("Vendor: " +
173: ex.getErrorCode ());
174: ex = ex.getNextException ();
175: System.out.println ("");
176: }
177: }
178: catch (java.lang.Exception ex) {
180: // Got some other type of exception. Dump it.
181: ex.printStackTrace ();
182: }
183: }
185: //-------------------------------------------------------------------
186: // checkForWarning
187: // Checks for and displays warnings. Returns true if a warning
188: // existed
189: //-------------------------------------------------------------------
191: private static boolean checkForWarning (SQLWarning warn)
192: throws SQLException
193: {
194: boolean rc = false;
196: // If a SQLWarning object was given, display the
197: // warning messages. Note that there could be
198: // multiple warnings chained together
199: if (warn != null) {
200: System.out.println ("\n *** Warning ***\n");
201: rc = true;
202: while (warn != null) {
203: System.out.println ("SQLState: " +
204: warn.getSQLState ());
205: System.out.println ("Message: " +
206: warn.getMessage ());
207: System.out.println ("Vendor: " +
208: warn.getErrorCode ());
209: System.out.println ("");
210: warn = warn.getNextWarning ();
211: }
212: }
213: return rc;
214: }
216: //-------------------------------------------------------------------
217: // showResultSet
218: // Displays all columns and rows in the given result set
219: //-------------------------------------------------------------------
221: private static void showResultSet (ResultSet rs)
222: throws SQLException
223: {
225: // Get the ResultSetMetaData.
226: ResultSetMetaData rsmd = rs.getMetaData ();
228: // Get the number of columns in the result set
229: int numCols = rsmd.getColumnCount ();
231: // Display column headings
232: for (int i=1; i<=numCols; i++) {
233: if (i > 1) System.out.print(",");
234: System.out.print(rsmd.getColumnLabel(i));
235: }
236: System.out.println("");
237:
238: // Display data, fetching until end of the result set or
239: // resultSetLimit is not reached
240: int rowCount = 0;
241: while (rs.next () && rowCount < resultSetLimit) {
243: // Loop through each column, getting the
244: // column data and displaying
245: rowCount++;
246: for (int i=1; i<=numCols; i++) {
247: if (i > 1) System.out.print(",");
248: System.out.print(rs.getString(i));
249: }
250: System.out.println("");
251: }
252: }
253: }