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