1 50 51 package org.openlaszlo.iv.flash.url; 52 53 import java.io.*; 54 import java.util.*; 55 import java.net.*; 56 import java.sql.*; 57 import org.openlaszlo.iv.flash.util.*; 58 import org.openlaszlo.iv.flash.api.*; 59 60 76 public class JDBCUrl extends IVUrl { 77 78 private String driver; 79 private String url; 80 private String userid; 81 private String password; 82 private String query; 83 88 89 public JDBCUrl( String surl ) throws IVException { 90 parse( surl ); 91 driver = getParameter("driver"); 92 url = getParameter("url"); 93 userid = getParameter("userid"); 94 password = getParameter("password"); 95 query = getParameter("query"); 96 101 if( driver == null || url == null || query == null ) { 102 throw new IVException(Resource.INVALURL, new Object [] {surl}); 103 } 104 } 105 106 public String getName() { 107 return url+"@"+userid+"/"+query; 108 } 109 110 public InputStream getInputStream() throws IOException { 111 return arrayToStream(getData(false)); 112 } 113 114 public boolean hasDataReady() { 115 return true; 116 } 117 118 public String [][] getData() throws IOException { 119 return getData(true); 120 } 121 122 private String [][] getData( boolean processEscapes ) throws IOException { 123 try { 124 Class.forName(driver); 125 } catch( Exception e ) { 126 throw new IOException("driver not found"); 127 } 128 129 String [][] data = null; 130 131 Connection conn = null; 132 try { 133 Log.logRB(Resource.SQLQUERY, new Object [] {driver, url, userid, password, query}); 134 if( userid == null ) { 135 conn = DriverManager.getConnection(url); 136 } else { 137 conn = DriverManager.getConnection(url, userid, password); 138 } 139 Statement stmt = conn.createStatement(); 140 141 ResultSet rs = stmt.executeQuery(query); 142 ResultSetMetaData meta = rs.getMetaData(); 143 144 int numCols = meta.getColumnCount(); 145 String [] header = new String [numCols]; 146 for( int i=0; i<numCols; i++ ) { 147 header[i] = meta.getColumnLabel(i+1); 148 } 149 150 IVVector lines = new IVVector(); 151 while( rs.next() ) { 152 String [] line = new String [numCols]; 153 for( int i=0; i<numCols; i++ ) { 154 Object o = rs.getObject(i+1); 155 String s = res2string(o); 156 line[i] = processEscapes? Util.processEscapes(s): s; 157 } 158 lines.addElement(line); 159 } 160 161 data = new String [lines.size()+1][]; 162 data[0] = header; 163 for( int i=0; i<lines.size(); i++ ) { 164 data[i+1] = (String []) lines.elementAt(i); 165 } 166 167 return data; 168 } catch( Exception e ) { 169 throw new IOException( e.getMessage() ); 170 } finally { 171 try { 172 if( conn != null ) 173 conn.close(); 174 } catch( SQLException ee ) { 175 throw new IOException(ee.getMessage()); 176 } 177 } 178 } 179 180 private String res2string( Object o ) { 181 if( o == null ) return ""; 182 if( o instanceof byte[] ) return new String ((byte[])o); 183 return o.toString(); 184 } 185 } 186 | Popular Tags |