|
| 1 | +package wrappers; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.Console; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStreamReader; |
| 7 | + |
| 8 | +public class Client { |
| 9 | + static boolean DEBUG = true; |
| 10 | + static final Console c = System.console(); |
| 11 | + static JdbcInterface jdi = null; |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + int rc, n, ncol, i = 0, fsize = 0; |
| 15 | + boolean scrollable = false; |
| 16 | + String s; |
| 17 | + String[] parms = new String[4]; |
| 18 | + |
| 19 | + if (args.length > 0) |
| 20 | + try { |
| 21 | + i = Integer.parseInt(args[i]); |
| 22 | + } catch (NumberFormatException e) { |
| 23 | + i = 0; |
| 24 | + } // end try/catch |
| 25 | + |
| 26 | + switch (i) { |
| 27 | + case 1: |
| 28 | + jdi = new ApacheInterface(DEBUG); |
| 29 | + break; |
| 30 | + case 2: |
| 31 | + jdi = new MysqlInterface(DEBUG); |
| 32 | + break; |
| 33 | + case 3: |
| 34 | + jdi = new MariadbInterface(DEBUG); |
| 35 | + break; |
| 36 | + case 4: |
| 37 | + jdi = new OracleInterface(DEBUG); |
| 38 | + break; |
| 39 | + case 5: |
| 40 | + jdi = new PostgresqlInterface(DEBUG); |
| 41 | + break; |
| 42 | + default: |
| 43 | + jdi = new JdbcInterface(DEBUG); |
| 44 | + } // endswitch i |
| 45 | + |
| 46 | + parms[0] = getLine("Driver: ", false); |
| 47 | + parms[1] = getLine("URL: ", false); |
| 48 | + parms[2] = getLine("User: ", false); |
| 49 | + parms[3] = getLine("Password: ", true); |
| 50 | + s = getLine("Fsize: ", false); |
| 51 | + fsize = (s != null) ? Integer.parseInt(s) : 0; |
| 52 | + s = getLine("Scrollable: ", false); |
| 53 | + scrollable = (s != null) ? s.toLowerCase().charAt(0) != 'n' : false; |
| 54 | + |
| 55 | + rc = jdi.JdbcConnect(parms, fsize, scrollable); |
| 56 | + |
| 57 | + if (rc == 0) { |
| 58 | + String query; |
| 59 | + System.out.println("Successfully connected to " + parms[1]); |
| 60 | + |
| 61 | + while ((query = getLine("Query: ", false)) != null) { |
| 62 | + n = jdi.Execute(query); |
| 63 | + System.out.println("Returned n = " + n); |
| 64 | + |
| 65 | + if ((ncol = jdi.GetResult()) > 0) |
| 66 | + PrintResult(ncol); |
| 67 | + else |
| 68 | + System.out.println("Affected rows = " + n); |
| 69 | + |
| 70 | + } // endwhile |
| 71 | + |
| 72 | + rc = jdi.JdbcDisconnect(); |
| 73 | + System.out.println("Disconnect returned " + rc); |
| 74 | + } else |
| 75 | + System.out.println(jdi.GetErrmsg() + " rc=" + rc); |
| 76 | + |
| 77 | + } // end of main |
| 78 | + |
| 79 | + private static void PrintResult(int ncol) { |
| 80 | + // Get result set meta data |
| 81 | + int i; |
| 82 | + String columnName; |
| 83 | + |
| 84 | + // Get the column names; column indices start from 1 |
| 85 | + for (i = 1; i <= ncol; i++) { |
| 86 | + columnName = jdi.ColumnName(i); |
| 87 | + |
| 88 | + if (columnName == null) |
| 89 | + return; |
| 90 | + |
| 91 | + // Get the name of the column's table name |
| 92 | + //String tableName = rsmd.getTableName(i); |
| 93 | + |
| 94 | + if (i > 1) |
| 95 | + System.out.print("\t"); |
| 96 | + |
| 97 | + System.out.print(columnName); |
| 98 | + } // endfor i |
| 99 | + |
| 100 | + System.out.println(); |
| 101 | + |
| 102 | + // Loop through the result set |
| 103 | + while (jdi.ReadNext() > 0) { |
| 104 | + for (i = 1; i <= ncol; i++) { |
| 105 | + if (i > 1) |
| 106 | + System.out.print("\t"); |
| 107 | + |
| 108 | + if (DEBUG) |
| 109 | + System.out.print("(" + jdi.ColumnType(i, null) + ")"); |
| 110 | + |
| 111 | + switch (jdi.ColumnType(i, null)) { |
| 112 | + case java.sql.Types.VARCHAR: |
| 113 | + case java.sql.Types.LONGVARCHAR: |
| 114 | + case java.sql.Types.CHAR: |
| 115 | + System.out.print(jdi.StringField(i, null)); |
| 116 | + break; |
| 117 | + case java.sql.Types.INTEGER: |
| 118 | + System.out.print(jdi.IntField(i, null)); |
| 119 | + break; |
| 120 | + case java.sql.Types.BIGINT: |
| 121 | + System.out.print(jdi.BigintField(i, null)); |
| 122 | + break; |
| 123 | + case java.sql.Types.TIMESTAMP: |
| 124 | + System.out.print(jdi.TimestampField(i, null)); |
| 125 | + break; |
| 126 | + case java.sql.Types.TIME: |
| 127 | + System.out.print(jdi.TimeField(i, null)); |
| 128 | + break; |
| 129 | + case java.sql.Types.DATE: |
| 130 | + System.out.print(jdi.DateField(i, null)); |
| 131 | + break; |
| 132 | + case java.sql.Types.SMALLINT: |
| 133 | + System.out.print(jdi.IntField(i, null)); |
| 134 | + break; |
| 135 | + case java.sql.Types.DOUBLE: |
| 136 | + case java.sql.Types.REAL: |
| 137 | + case java.sql.Types.FLOAT: |
| 138 | + case java.sql.Types.DECIMAL: |
| 139 | + System.out.print(jdi.DoubleField(i, null)); |
| 140 | + break; |
| 141 | + case java.sql.Types.BOOLEAN: |
| 142 | + System.out.print(jdi.BooleanField(i, null)); |
| 143 | + default: |
| 144 | + break; |
| 145 | + } // endswitch Type |
| 146 | + |
| 147 | + } // endfor i |
| 148 | + |
| 149 | + System.out.println(); |
| 150 | + } // end while rs |
| 151 | + |
| 152 | + } // end of PrintResult |
| 153 | + |
| 154 | + // ================================================================== |
| 155 | + private static String getLine(String p, boolean b) { |
| 156 | + String response; |
| 157 | + |
| 158 | + if (c != null) { |
| 159 | + // Standard console mode |
| 160 | + if (b) { |
| 161 | + response = new String(c.readPassword(p)); |
| 162 | + } else |
| 163 | + response = c.readLine(p); |
| 164 | + |
| 165 | + } else { |
| 166 | + // For instance when testing from Eclipse |
| 167 | + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
| 168 | + |
| 169 | + System.out.print(p); |
| 170 | + |
| 171 | + try { |
| 172 | + // Cannot suppress echo for password entry |
| 173 | + response = in.readLine(); |
| 174 | + } catch (IOException e) { |
| 175 | + response = ""; |
| 176 | + } // end of try/catch |
| 177 | + |
| 178 | + } // endif c |
| 179 | + |
| 180 | + return (response.isEmpty()) ? null : response; |
| 181 | + } // end of getLine |
| 182 | + |
| 183 | +} // end of class Client |
0 commit comments