1 11 package com.nilostep.xlsql.ui; 12 import java.sql.*; 13 import java.io.*; 14 15 18 public class ixql { 19 public static void main(String [] args) { 20 Connection conn = null; try { 22 String driver = null, url = null, user = "", password = ""; 23 24 for(int n = 0; n < args.length; n++) { 26 if (args[n].equals("-d")) driver = args[++n]; 27 else if (args[n].equals("-u")) user = args[++n]; 28 else if (args[n].equals("-p")) password = args[++n]; 29 else if (url == null) url = args[n]; 30 else throw new IllegalArgumentException ("Unknown argument."); 31 } 32 33 if (url == null) 35 throw new IllegalArgumentException ("No database specified"); 36 37 if (driver != null) Class.forName(driver); 41 42 conn = DriverManager.getConnection(url, user, password); 47 48 Statement s = conn.createStatement(); 50 51 BufferedReader in = 53 new BufferedReader(new InputStreamReader(System.in)); 54 55 while(true) { 57 System.out.print("sql> "); System.out.flush(); String sql = in.readLine(); 61 if ((sql == null) || sql.equals("quit")) break; 63 64 if (sql.length() == 0) continue; 66 67 try { 69 boolean status = s.execute(sql); 74 75 do { 78 if (status) { ResultSet rs = s.getResultSet(); printResultsTable(rs, System.out); } 82 else { 83 int numUpdates = s.getUpdateCount(); 88 System.out.println("Ok. " + numUpdates + 89 " rows affected."); 90 } 91 92 status = s.getMoreResults(); 95 } while(status || s.getUpdateCount() != -1); 96 } 97 catch (SQLException e) { 101 System.err.println("SQLException: " + e.getMessage()+ ":" + 102 e.getSQLState()); 103 } 104 finally { SQLWarning w; 108 for(w=conn.getWarnings(); w != null; w=w.getNextWarning()) 109 System.err.println("WARNING: " + w.getMessage() + 110 ":" + w.getSQLState()); 111 } 112 } 113 } 114 catch (Exception e) { 117 System.err.println(e); 118 if (e instanceof SQLException) 119 System.err.println("SQL State: " + 120 ((SQLException)e).getSQLState()); 121 System.err.println("Usage: java ExecuteSQL [-d <driver>] " + 122 "[-u <user>] [-p <password>] <database URL>"); 123 } 124 125 finally { 131 try { conn.close(); } catch (Exception e) {} 132 } 133 } 134 135 140 static void printResultsTable(ResultSet rs, OutputStream output) 141 throws SQLException 142 { 143 PrintWriter out = new PrintWriter(output); 145 146 ResultSetMetaData metadata = rs.getMetaData(); 148 149 int numcols = metadata.getColumnCount(); String [] labels = new String [numcols]; int[] colwidths = new int[numcols]; int[] colpos = new int[numcols]; int linewidth; 156 linewidth = 1; for(int i = 0; i < numcols; i++) { colpos[i] = linewidth; labels[i] = metadata.getColumnLabel(i+1); int size = metadata.getColumnDisplaySize(i+1); 166 if (size == -1) size = 30; if (size > 500) size = 30; int labelsize = labels[i].length(); 169 if (labelsize > size) size = labelsize; 170 colwidths[i] = size + 1; linewidth += colwidths[i] + 2; } 173 174 StringBuffer divider = new StringBuffer (linewidth); 178 StringBuffer blankline = new StringBuffer (linewidth); 179 for(int i = 0; i < linewidth; i++) { 180 divider.insert(i, '-'); 181 blankline.insert(i, " "); 182 } 183 for(int i=0; i<numcols; i++) divider.setCharAt(colpos[i]-1,'+'); 185 divider.setCharAt(linewidth-1, '+'); 186 187 out.println(divider); 189 190 StringBuffer line = new StringBuffer (blankline.toString()); 194 line.setCharAt(0, '|'); 195 for(int i = 0; i < numcols; i++) { 196 int pos = colpos[i] + 1 + (colwidths[i]-labels[i].length())/2; 197 overwrite(line, pos, labels[i]); 198 overwrite(line, colpos[i] + colwidths[i], " |"); 199 } 200 201 out.println(line); 203 out.println(divider); 204 205 while(rs.next()) { 210 line = new StringBuffer (blankline.toString()); 211 line.setCharAt(0, '|'); 212 for(int i = 0; i < numcols; i++) { 213 Object value = rs.getObject(i+1); 214 if (value != null) 215 overwrite(line, colpos[i] + 1, value.toString().trim()); 216 overwrite(line, colpos[i] + colwidths[i], " |"); 217 } 218 out.println(line); 219 } 220 221 out.println(divider); 223 out.flush(); 224 } 225 226 227 static void overwrite(StringBuffer b, int pos, String s) { 228 int slen = s.length(); int blen = b.length(); if (pos+slen > blen) slen = blen-pos; for(int i = 0; i < slen; i++) b.setCharAt(pos+i, s.charAt(i)); 233 } 234 } 235 | Popular Tags |