1 7 8 package javax.sql.rowset; 9 10 import java.sql.*; 11 import javax.sql.*; 12 import java.util.*; 13 import java.io.*; 14 import java.io.Serializable ; 15 16 import javax.sql.rowset.serial.*; 17 18 277 278 public abstract class BaseRowSet implements Serializable , Cloneable { 279 280 287 public static final int UNICODE_STREAM_PARAM = 0; 288 289 296 public static final int BINARY_STREAM_PARAM = 1; 297 298 305 public static final int ASCII_STREAM_PARAM = 2; 306 307 313 protected java.io.InputStream binaryStream; 314 315 321 protected java.io.InputStream unicodeStream; 322 323 329 protected java.io.InputStream asciiStream; 330 331 337 protected java.io.Reader charStream; 338 339 344 private String command; 345 346 356 private String URL; 357 358 364 private String dataSource; 365 366 372 private transient String username; 373 374 380 private transient String password; 381 382 390 private int rowSetType = ResultSet.TYPE_SCROLL_INSENSITIVE; 391 392 397 private boolean showDeleted = false; 399 407 private int queryTimeout = 0; 409 413 private int maxRows = 0; 415 419 private int maxFieldSize = 0; 421 428 private int concurrency = ResultSet.CONCUR_UPDATABLE; 429 430 436 private boolean readOnly; 437 438 445 private boolean escapeProcessing; 446 447 458 private int isolation; 459 460 472 private int fetchDir = ResultSet.FETCH_FORWARD; 474 481 private int fetchSize = 0; 483 489 private Map map; 490 491 496 private Vector listeners; 497 498 503 private Hashtable params; 505 511 public BaseRowSet() { 512 listeners = new Vector(); 514 } 515 516 525 protected void initParams() { 526 params = new Hashtable(); 527 } 528 529 533 555 public void addRowSetListener(RowSetListener listener) { 556 listeners.add(listener); 557 } 558 559 572 public void removeRowSetListener(RowSetListener listener) { 573 listeners.remove(listener); 574 } 575 576 579 private void checkforRowSetInterface() throws SQLException { 580 if ((this instanceof javax.sql.RowSet ) == false) { 581 throw new SQLException("The class extending abstract class BaseRowSet " + 582 "must implement javax.sql.RowSet or one of it's sub-interfaces."); 583 } 584 } 585 586 599 protected void notifyCursorMoved() throws SQLException { 600 checkforRowSetInterface(); 601 if (listeners.isEmpty() == false) { 602 RowSetEvent event = new RowSetEvent((RowSet)this); 603 for (Iterator i = listeners.iterator(); i.hasNext(); ) { 604 ((RowSetListener)i.next()).cursorMoved(event); 605 } 606 } 607 } 608 609 624 protected void notifyRowChanged() throws SQLException { 625 checkforRowSetInterface(); 626 if (listeners.isEmpty() == false) { 627 RowSetEvent event = new RowSetEvent((RowSet)this); 628 for (Iterator i = listeners.iterator(); i.hasNext(); ) { 629 ((RowSetListener)i.next()).rowChanged(event); 630 } 631 } 632 } 633 634 649 protected void notifyRowSetChanged() throws SQLException { 650 checkforRowSetInterface(); 651 if (listeners.isEmpty() == false) { 652 RowSetEvent event = new RowSetEvent((RowSet)this); 653 for (Iterator i = listeners.iterator(); i.hasNext(); ) { 654 ((RowSetListener)i.next()).rowSetChanged(event); 655 } 656 } 657 } 658 659 682 public String getCommand() { 683 return command; 684 } 685 686 702 public void setCommand(String cmd) throws SQLException { 703 707 if(cmd == null) { 708 command = null; 709 } else if (cmd.length() == 0) { 710 throw new SQLException("Invalid command string detected. " + 711 "Cannot be of length less than 0"); 712 } else { 713 if(params == null){ 715 throw new SQLException("Set initParams() before setCommand"); 716 } 717 params.clear(); 718 command = new String (cmd); 719 } 720 721 } 722 723 738 public String getUrl() throws SQLException { 739 return URL; 740 } 741 742 774 public void setUrl(String url) throws SQLException { 775 if(url == null) { 776 url = null; 777 } else if (url.length() < 1) { 778 throw new SQLException("Invalid url string detected. " + 779 "Cannot be of length less than 1"); 780 } else { 781 URL = new String (url); 782 } 783 784 dataSource = null; 785 786 } 787 788 805 public String getDataSourceName() { 806 return dataSource; 807 } 808 809 810 831 public void setDataSourceName(String name) throws SQLException { 832 833 if (name == null) { 834 dataSource = null; 835 } else if (name.equals("")) { 836 throw new SQLException("DataSource name cannot be empty string"); 837 } else { 838 dataSource = new String (name); 839 } 840 841 URL = null; 842 } 843 844 854 public String getUsername() { 855 return username; 856 } 857 858 868 public void setUsername(String name) { 869 if(name == null) 870 { 871 username = null; 872 } else { 873 username = new String (name); 874 } 875 } 876 877 887 public String getPassword() { 888 return password; 889 } 890 891 903 public void setPassword(String pass) { 904 if(pass == null) 905 { 906 password = null; 907 } else { 908 password = new String (pass); 909 } 910 } 911 912 928 public void setType(int type) throws SQLException { 929 930 if ((type != ResultSet.TYPE_FORWARD_ONLY) && 931 (type != ResultSet.TYPE_SCROLL_INSENSITIVE) && 932 (type != ResultSet.TYPE_SCROLL_SENSITIVE)) { 933 throw new SQLException("Invalid type of RowSet set. Must be either " + 934 "ResultSet.TYPE_FORWARD_ONLY or ResultSet.TYPE_SCROLL_INSENSITIVE " + 935 "or ResultSet.TYPE_SCROLL_SENSITIVE."); 936 } 937 this.rowSetType = type; 938 } 939 940 956 public int getType() throws SQLException { 957 return rowSetType; 958 } 959 960 976 public void setConcurrency(int concurrency) throws SQLException { 977 978 if((concurrency != ResultSet.CONCUR_READ_ONLY) && 979 (concurrency != ResultSet.CONCUR_UPDATABLE)) { 980 throw new SQLException("Invalid concurrency set. Must be either " + 981 "ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE."); 982 } 983 this.concurrency = concurrency; 984 } 985 986 998 public boolean isReadOnly() { 999 return readOnly; 1000 }; 1001 1002 1009 public void setReadOnly(boolean value) { 1010 readOnly = value; 1011 } 1012 1013 1036 public int getTransactionIsolation() { 1037 return isolation; 1038 }; 1039 1040 1064 public void setTransactionIsolation(int level) throws SQLException { 1065 if ((level != Connection.TRANSACTION_NONE) && 1066 (level != Connection.TRANSACTION_READ_COMMITTED) && 1067 (level != Connection.TRANSACTION_READ_UNCOMMITTED) && 1068 (level != Connection.TRANSACTION_REPEATABLE_READ) && 1069 (level != Connection.TRANSACTION_SERIALIZABLE)) 1070 { 1071 throw new SQLException("Invalid transaction isolation set. Must " + 1072 "be either " + 1073 "Connection.TRANSACTION_NONE or " + 1074 "Connection.TRANSACTION_READ_UNCOMMITTED or " + 1075 "Connection.TRANSACTION_READ_COMMITTED or " + 1076 "Connection.RRANSACTION_REPEATABLE_READ or " + 1077 "Connection.TRANSACTION_SERIALIZABLE"); 1078 } 1079 this.isolation = level; 1080 } 1081 1082 1100 public java.util.Map <String ,Class <?>> getTypeMap() { 1101 return map; 1102 } 1103 1104 1118 public void setTypeMap(java.util.Map <String ,Class <?>> map) { 1119 this.map = map; 1120 } 1121 1122 1136 public int getMaxFieldSize() throws SQLException { 1137 return maxFieldSize; 1138 } 1139 1140 1155 public void setMaxFieldSize(int max) throws SQLException { 1156 if (max < 0) { 1157 throw new SQLException("Invalid max field size set. Cannot be of " + 1158 "value: " + max); 1159 } 1160 maxFieldSize = max; 1161 } 1162 1163 1172 public int getMaxRows() throws SQLException { 1173 return maxRows; 1174 } 1175 1176 1189 public void setMaxRows(int max) throws SQLException { 1190 if (max < 0) { 1191 throw new SQLException("Invalid max row size set. Cannot be of " + 1192 "value: " + max); 1193 } else if (max < this.getFetchSize()) { 1194 throw new SQLException("Invalid max row size set. Cannot be less " + 1195 "than the fetchSize."); 1196 } 1197 this.maxRows = max; 1198 } 1199 1200 1215 public void setEscapeProcessing(boolean enable) throws SQLException { 1216 escapeProcessing = enable; 1217 } 1218 1219 1229 public int getQueryTimeout() throws SQLException { 1230 return queryTimeout; 1231 } 1232 1233 1243 public void setQueryTimeout(int seconds) throws SQLException { 1244 if (seconds < 0) { 1245 throw new SQLException("Invalid query timeout value set. Cannot be " + 1246 "of value: " + seconds); 1247 } 1248 this.queryTimeout = seconds; 1249 } 1250 1251 1267 public boolean getShowDeleted() throws SQLException { 1268 return showDeleted; 1269 } 1270 1271 1282 public void setShowDeleted(boolean value) throws SQLException { 1283 showDeleted = value; 1284 } 1285 1286 1296 public boolean getEscapeProcessing() throws SQLException { 1297 return escapeProcessing; 1298 } 1299 1300 1328 public void setFetchDirection(int direction) throws SQLException { 1329 1334 if (((getType() == ResultSet.TYPE_FORWARD_ONLY) && (direction != ResultSet.FETCH_FORWARD)) || 1335 ((direction != ResultSet.FETCH_FORWARD) && 1336 (direction != ResultSet.FETCH_REVERSE) && 1337 (direction != ResultSet.FETCH_UNKNOWN))) { 1338 throw new SQLException("Invalid Fetch Direction"); 1339 } 1340 fetchDir = direction; 1341 } 1342 1343 1354 public int getFetchDirection() throws SQLException { 1355 1356 1360 1362 1369 return (fetchDir); 1370 } 1371 1372 1414 public void setFetchSize(int rows) throws SQLException { 1415 if (getMaxRows() == 0 && rows >= 0) { 1419 fetchSize = rows; 1420 return; 1421 } 1422 if ((rows < 0) || (rows > getMaxRows())) { 1423 throw new SQLException("Invalid fetch size set. Cannot be of " + 1424 "value: " + rows); 1425 } 1426 fetchSize = rows; 1427 } 1428 1429 1439 public int getFetchSize() throws SQLException { 1440 return fetchSize; 1441 } 1442 1443 1460 public int getConcurrency() throws SQLException { 1461 return concurrency; 1462 } 1463 1464 1468 1479 private void checkParamIndex(int idx) throws SQLException { 1480 if ((idx < 1)) { 1481 throw new SQLException("Invalid Parameter Index"); 1482 } 1483 } 1484 1485 1489 1541 public void setNull(int parameterIndex, int sqlType) throws SQLException { 1542 Object nullVal[]; 1543 checkParamIndex(parameterIndex); 1544 1545 nullVal = new Object [2]; 1546 nullVal[0] = null; 1547 nullVal[1] = new Integer (sqlType); 1548 1549 if (params == null){ 1550 throw new SQLException("Set initParams() before setNull"); 1551 } 1552 1553 params.put(new Integer (parameterIndex - 1), nullVal); 1554 } 1555 1556 1620 public void setNull(int parameterIndex, int sqlType, String typeName) 1621 throws SQLException { 1622 1623 Object nullVal[]; 1624 checkParamIndex(parameterIndex); 1625 1626 nullVal = new Object [3]; 1627 nullVal[0] = null; 1628 nullVal[1] = new Integer (sqlType); 1629 nullVal[2] = new String (typeName); 1630 1631 if(params == null){ 1632 throw new SQLException("Set initParams() before setNull"); 1633 } 1634 1635 params.put(new Integer (parameterIndex - 1), nullVal); 1636 } 1637 1638 1639 1663 public void setBoolean(int parameterIndex, boolean x) throws SQLException { 1664 checkParamIndex(parameterIndex); 1665 1666 if(params == null){ 1667 throw new SQLException("Set initParams() before setNull"); 1668 } 1669 1670 params.put(new Integer (parameterIndex - 1), new Boolean (x)); 1671 } 1672 1673 1697 public void setByte(int parameterIndex, byte x) throws SQLException { 1698 checkParamIndex(parameterIndex); 1699 1700 if(params == null){ 1701 throw new SQLException("Set initParams() before setByte"); 1702 } 1703 1704 params.put(new Integer (parameterIndex - 1), new Byte (x)); 1705 } 1706 1707 1731 public void setShort(int parameterIndex, short x) throws SQLException { 1732 checkParamIndex(parameterIndex); 1733 1734 if(params == null){ 1735 throw new SQLException("Set initParams() before setShort"); 1736 } 1737 1738 params.put(new Integer (parameterIndex - 1), new Short (x)); 1739 } 1740 1741 1765 public void setInt(int parameterIndex, int x) throws SQLException { 1766 checkParamIndex(parameterIndex); 1767 if(params == null){ 1768 throw new SQLException("Set initParams() before setInt"); 1769 } 1770 params.put(new Integer (parameterIndex - 1), new Integer (x)); 1771 } 1772 1773 1797 public void setLong(int parameterIndex, long x) throws SQLException { 1798 checkParamIndex(parameterIndex); 1799 if(params == null){ 1800 throw new SQLException("Set initParams() before setLong"); 1801 } 1802 params.put(new Integer (parameterIndex - 1), new Long (x)); 1803 } 1804 1805 1829 public void setFloat(int parameterIndex, float x) throws SQLException { 1830 checkParamIndex(parameterIndex); 1831 if(params == null){ 1832 throw new SQLException("Set initParams() before setFloat"); 1833 } 1834 params.put(new Integer (parameterIndex - 1), new Float (x)); 1835 } 1836 1837 1861 public void setDouble(int parameterIndex, double x) throws SQLException { 1862 checkParamIndex(parameterIndex); 1863 if(params == null){ 1864 throw new SQLException("Set initParams() before setDouble"); 1865 } 1866 params.put(new Integer (parameterIndex - 1), new Double (x)); 1867 } 1868 1869 1893 public void setBigDecimal(int parameterIndex, java.math.BigDecimal x) throws SQLException { 1894 checkParamIndex(parameterIndex); 1895 if(params == null){ 1896 throw new SQLException("Set initParams() before setBigDecimal"); 1897 } 1898 params.put(new Integer (parameterIndex - 1), x); 1899 } 1900 1901 1927 public void setString(int parameterIndex, String x) throws SQLException { 1928 checkParamIndex(parameterIndex); 1929 if(params == null){ 1930 throw new SQLException("Set initParams() before setString"); 1931 } 1932 params.put(new Integer (parameterIndex - 1), x); 1933 } 1934 1935 1961 public void setBytes(int parameterIndex, byte x[]) throws SQLException { 1962 checkParamIndex(parameterIndex); 1963 if(params == null){ 1964 throw new SQLException("Set initParams() before setBytes"); 1965 } 1966 params.put(new Integer (parameterIndex - 1), x); 1967 } 1968 1969 2002 public void setDate(int parameterIndex, java.sql.Date x) throws SQLException { 2003 checkParamIndex(parameterIndex); 2004 2005 if(params == null){ 2006 throw new SQLException("Set initParams() before setDate"); 2007 } 2008 params.put(new Integer (parameterIndex - 1), x); 2009 } 2010 2011 2047 public void setTime(int parameterIndex, java.sql.Time x) throws SQLException { 2048 checkParamIndex(parameterIndex); 2049 if(params == null){ 2050 throw new SQLException("Set initParams() before setTime"); 2051 } 2052 2053 params.put(new Integer (parameterIndex - 1), x); 2054 } 2055 2056 2090 public void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException { 2091 checkParamIndex(parameterIndex); 2092 if(params == null){ 2093 throw new SQLException("Set initParams() before setTimestamp"); 2094 } 2095 2096 params.put(new Integer (parameterIndex - 1), x); 2097 } 2098 2099 2163 public void setAsciiStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException { 2164 Object asciiStream[]; 2165 checkParamIndex(parameterIndex); 2166 2167 asciiStream = new Object [3]; 2168 asciiStream[0] = x; 2169 asciiStream[1] = new Integer (length); 2170 asciiStream[2] = new Integer (ASCII_STREAM_PARAM); 2171 2172 if(params == null){ 2173 throw new SQLException("Set initParams() before setAsciiStream"); 2174 } 2175 2176 params.put(new Integer (parameterIndex - 1), asciiStream); 2177 } 2178 2179 2240 public void setBinaryStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException { 2241 Object binaryStream[]; 2242 checkParamIndex(parameterIndex); 2243 2244 binaryStream = new Object [3]; 2245 binaryStream[0] = x; 2246 binaryStream[1] = new Integer (length); 2247 binaryStream[2] = new Integer (BINARY_STREAM_PARAM); 2248 if(params == null){ 2249 throw new SQLException("Set initParams() before setBinaryStream"); 2250 } 2251 2252 params.put(new Integer (parameterIndex - 1), binaryStream); 2253 } 2254 2255 2256 2317 2318 public void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException { 2319 Object unicodeStream[]; 2320 checkParamIndex(parameterIndex); 2321 2322 unicodeStream = new Object [3]; 2323 unicodeStream[0] = x; 2324 unicodeStream[1] = new Integer (length); 2325 unicodeStream[2] = new Integer (UNICODE_STREAM_PARAM); 2326 if(params == null){ 2327 throw new SQLException("Set initParams() before setUnicodeStream"); 2328 } 2329 params.put(new Integer (parameterIndex - 1), unicodeStream); 2330 } 2331 2332 2397 public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { 2398 Object charStream[]; 2399 checkParamIndex(parameterIndex); 2400 2401 charStream = new Object [2]; 2402 charStream[0] = reader; 2403 charStream[1] = new Integer (length); 2404 if(params == null){ 2405 throw new SQLException("Set initParams() before setCharacterStream"); 2406 } 2407 params.put(new Integer (parameterIndex - 1), charStream); 2408 } 2409 2410 2483 public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException { 2484 Object obj[]; 2485 checkParamIndex(parameterIndex); 2486 2487 obj = new Object [3]; 2488 obj[0] = x; 2489 obj[1] = new Integer (targetSqlType); 2490 obj[2] = new Integer (scale); 2491 if(params == null){ 2492 throw new SQLException("Set initParams() before setObject"); 2493 } 2494 params.put(new Integer (parameterIndex - 1), obj); 2495 } 2496 2497 2546 public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { 2547 Object obj[]; 2548 checkParamIndex(parameterIndex); 2549 2550 obj = new Object [2]; 2551 obj[0] = x; 2552 obj[1] = new Integer (targetSqlType); 2553 if (params == null){ 2554 throw new SQLException("Set initParams() before setObject"); 2555 } 2556 params.put(new Integer (parameterIndex - 1), obj); 2557 } 2558 2559 2619 public void setObject(int parameterIndex, Object x) throws SQLException { 2620 checkParamIndex(parameterIndex); 2621 if (params == null) { 2622 throw new SQLException("Set initParams() before setObject"); 2623 } 2624 params.put(new Integer (parameterIndex - 1), x); 2625 } 2626 2627 2666 public void setRef (int parameterIndex, Ref ref) throws SQLException { 2667 checkParamIndex(parameterIndex); 2668 if (params == null) { 2669 throw new SQLException("Set initParams() before setRef"); 2670 } 2671 params.put(new Integer (parameterIndex - 1), new SerialRef(ref)); 2672 } 2673 2674 2710 public void setBlob (int parameterIndex, Blob x) throws SQLException { 2711 checkParamIndex(parameterIndex); 2712 if(params == null){ 2713 throw new SQLException("Set initParams() before setBlob"); 2714 } 2715 params.put(new Integer (parameterIndex - 1), new SerialBlob(x)); 2716 } 2717 2718 2755 public void setClob (int parameterIndex, Clob x) throws SQLException { 2756 checkParamIndex(parameterIndex); 2757 if(params == null){ 2758 throw new SQLException("Set initParams() before setClob"); 2759 } 2760 params.put(new Integer (parameterIndex - 1), new SerialClob(x)); 2761 } 2762 2763 2802 public void setArray (int parameterIndex, Array array) throws SQLException { 2803 checkParamIndex(parameterIndex); 2804 if (params == null){ 2805 throw new SQLException("Set initParams() before setArray"); 2806 } 2807 params.put(new Integer (parameterIndex - 1), new SerialArray(array)); 2808 } 2809 2810 2862 public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException { 2863 Object date[]; 2864 checkParamIndex(parameterIndex); 2865 2866 date = new Object [2]; 2867 date[0] = x; 2868 date[1] = cal; 2869 if(params == null){ 2870 throw new SQLException("Set initParams() before setDate"); 2871 } 2872 params.put(new Integer (parameterIndex - 1), date); 2873 } 2874 2875 2928 public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws SQLException { 2929 Object time[]; 2930 checkParamIndex(parameterIndex); 2931 2932 time = new Object [2]; 2933 time[0] = x; 2934 time[1] = cal; 2935 if(params == null){ 2936 throw new SQLException("Set initParams() before setTime"); 2937 } 2938 params.put(new Integer (parameterIndex - 1), time); 2939 } 2940 2941 2994 public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) throws SQLException { 2995 Object timestamp[]; 2996 checkParamIndex(parameterIndex); 2997 2998 timestamp = new Object [2]; 2999 timestamp[0] = x; 3000 timestamp[1] = cal; 3001 if(params == null){ 3002 throw new SQLException("Set initParams() before setTimestamp"); 3003 } 3004 params.put(new Integer (parameterIndex - 1), timestamp); 3005 } 3006 3007 3025 public void clearParameters() throws SQLException { 3026 params.clear(); 3027 } 3028 3029 3063 public Object [] getParams() throws SQLException { 3064 if (params == null) { 3065 3066 initParams(); 3067 Object [] paramsArray = new Object [params.size()]; 3068 return paramsArray; 3069 3070 } else { 3071 3076 Object [] paramsArray = new Object [params.size()]; 3077 for (int i = 0; i < params.size(); i++) { 3078 paramsArray[i] = params.get(new Integer (i)); 3079 if (paramsArray[i] == null) { 3080 throw new SQLException("missing parameter: " + (i + 1)); 3081 } } return paramsArray; 3084 3085 } 3087 } 3089 static final long serialVersionUID = 4886719666485113312L; 3090 3091} 3093 | Popular Tags |