1 7 8 package javax.sql.rowset; 9 10 import java.sql.*; 11 import javax.sql.*; 12 import java.io.*; 13 14 import java.lang.reflect.*; 15 16 34 public class RowSetMetaDataImpl implements RowSetMetaData, Serializable { 35 36 41 private int colCount; 42 43 52 private ColInfo[] colInfo; 53 54 63 private void checkColRange(int col) throws SQLException { 64 if (col <= 0 || col > colCount) { 65 throw new SQLException("Invalid column index :"+col); 66 } 67 } 68 69 80 private void checkColType(int SQLType) throws SQLException { 81 try { 82 Class c = java.sql.Types .class; 83 Field[] publicFields = c.getFields(); 84 int fieldValue = 0; 85 for (int i = 0; i < publicFields.length; i++) { 86 fieldValue = publicFields[i].getInt(c); 87 if (fieldValue == SQLType) { 88 return; 89 } 90 } 91 } catch (Exception e) { 92 throw new SQLException(e.getMessage()); 93 } 94 throw new SQLException("Invalid SQL type for column"); 95 } 96 97 105 public void setColumnCount(int columnCount) throws SQLException { 106 107 if (columnCount <= 0) { 108 throw new SQLException("Invalid column count. Cannot be less " + 109 "or equal to zero"); 110 } 111 112 colCount = columnCount; 113 114 122 if(!(colCount == Integer.MAX_VALUE)) { 123 colInfo = new ColInfo[colCount + 1]; 124 125 for (int i=1; i <= colCount; i++) { 126 colInfo[i] = new ColInfo(); 127 } 128 } 129 130 131 } 132 133 147 public void setAutoIncrement(int columnIndex, boolean property) throws SQLException { 148 checkColRange(columnIndex); 149 colInfo[columnIndex].autoIncrement = property; 150 } 151 152 164 public void setCaseSensitive(int columnIndex, boolean property) throws SQLException { 165 checkColRange(columnIndex); 166 colInfo[columnIndex].caseSensitive = property; 167 } 168 169 183 public void setSearchable(int columnIndex, boolean property) 184 throws SQLException { 185 checkColRange(columnIndex); 186 colInfo[columnIndex].searchable = property; 187 } 188 189 200 public void setCurrency(int columnIndex, boolean property) 201 throws SQLException { 202 checkColRange(columnIndex); 203 colInfo[columnIndex].currency = property; 204 } 205 206 226 public void setNullable(int columnIndex, int property) throws SQLException { 227 if ((property < ResultSetMetaData.columnNoNulls) || 228 property > ResultSetMetaData.columnNullableUnknown) { 229 throw new SQLException("Invalid nullable constant set. Must be " + 230 "either columnNoNulls, columnNullable or columnNullableUnknown"); 231 } 232 checkColRange(columnIndex); 233 colInfo[columnIndex].nullable = property; 234 } 235 236 248 public void setSigned(int columnIndex, boolean property) throws SQLException { 249 checkColRange(columnIndex); 250 colInfo[columnIndex].signed = property; 251 } 252 253 265 public void setColumnDisplaySize(int columnIndex, int size) throws SQLException { 266 if (size < 0) { 267 throw new SQLException("Invalid column display size. Cannot be less " + 268 "than zero"); 269 } 270 checkColRange(columnIndex); 271 colInfo[columnIndex].columnDisplaySize = size; 272 } 273 274 288 public void setColumnLabel(int columnIndex, String label) throws SQLException { 289 checkColRange(columnIndex); 290 if (label != null) { 291 colInfo[columnIndex].columnLabel = new String (label); 292 } else { 293 colInfo[columnIndex].columnLabel = new String (""); 294 } 295 } 296 297 308 public void setColumnName(int columnIndex, String columnName) throws SQLException { 309 checkColRange(columnIndex); 310 if (columnName != null) { 311 colInfo[columnIndex].columnName = new String (columnName); 312 } else { 313 colInfo[columnIndex].columnName = new String (""); 314 } 315 } 316 317 330 public void setSchemaName(int columnIndex, String schemaName) throws SQLException { 331 checkColRange(columnIndex); 332 if (schemaName != null ) { 333 colInfo[columnIndex].schemaName = new String (schemaName); 334 } else { 335 colInfo[columnIndex].schemaName = new String (""); 336 } 337 } 338 339 351 public void setPrecision(int columnIndex, int precision) throws SQLException { 352 353 if (precision < 0) { 354 throw new SQLException("Invalid precision value. Cannot be less " + 355 "than zero"); 356 } 357 checkColRange(columnIndex); 358 colInfo[columnIndex].colPrecision = precision; 359 } 360 361 373 public void setScale(int columnIndex, int scale) throws SQLException { 374 if (scale < 0) { 375 throw new SQLException("Invalid scale size. Cannot be less " + 376 "than zero"); 377 } 378 checkColRange(columnIndex); 379 colInfo[columnIndex].colScale = scale; 380 } 381 382 393 public void setTableName(int columnIndex, String tableName) throws SQLException { 394 checkColRange(columnIndex); 395 if (tableName != null) { 396 colInfo[columnIndex].tableName = new String (tableName); 397 } else { 398 colInfo[columnIndex].tableName = new String (""); 399 } 400 } 401 402 414 public void setCatalogName(int columnIndex, String catalogName) throws SQLException { 415 checkColRange(columnIndex); 416 if (catalogName != null) 417 colInfo[columnIndex].catName = new String (catalogName); 418 else 419 colInfo[columnIndex].catName = new String (""); 420 } 421 422 436 public void setColumnType(int columnIndex, int SQLType) throws SQLException { 437 checkColType(SQLType); 440 checkColRange(columnIndex); 441 colInfo[columnIndex].colType = SQLType; 442 } 443 444 455 public void setColumnTypeName(int columnIndex, String typeName) 456 throws SQLException { 457 checkColRange(columnIndex); 458 if (typeName != null) { 459 colInfo[columnIndex].colTypeName = new String (typeName); 460 } else { 461 colInfo[columnIndex].colTypeName = new String (""); 462 } 463 } 464 465 472 public int getColumnCount() throws SQLException { 473 return colCount; 474 } 475 476 487 public boolean isAutoIncrement(int columnIndex) throws SQLException { 488 checkColRange(columnIndex); 489 return colInfo[columnIndex].autoIncrement; 490 } 491 492 503 public boolean isCaseSensitive(int columnIndex) throws SQLException { 504 checkColRange(columnIndex); 505 return colInfo[columnIndex].caseSensitive; 506 } 507 508 519 public boolean isSearchable(int columnIndex) throws SQLException { 520 checkColRange(columnIndex); 521 return colInfo[columnIndex].searchable; 522 } 523 524 535 public boolean isCurrency(int columnIndex) throws SQLException { 536 checkColRange(columnIndex); 537 return colInfo[columnIndex].currency; 538 } 539 540 553 public int isNullable(int columnIndex) throws SQLException { 554 checkColRange(columnIndex); 555 return colInfo[columnIndex].nullable; 556 } 557 558 569 public boolean isSigned(int columnIndex) throws SQLException { 570 checkColRange(columnIndex); 571 return colInfo[columnIndex].signed; 572 } 573 574 584 public int getColumnDisplaySize(int columnIndex) throws SQLException { 585 checkColRange(columnIndex); 586 return colInfo[columnIndex].columnDisplaySize; 587 } 588 589 599 public String getColumnLabel(int columnIndex) throws SQLException { 600 checkColRange(columnIndex); 601 return colInfo[columnIndex].columnLabel; 602 } 603 604 613 public String getColumnName(int columnIndex) throws SQLException { 614 checkColRange(columnIndex); 615 return colInfo[columnIndex].columnName; 616 } 617 618 630 public String getSchemaName(int columnIndex) throws SQLException { 631 checkColRange(columnIndex); 632 String str =""; 633 if(colInfo[columnIndex].schemaName == null){ 634 } else { 635 str = colInfo[columnIndex].schemaName; 636 } 637 return str; 638 } 639 640 650 public int getPrecision(int columnIndex) throws SQLException { 651 checkColRange(columnIndex); 652 return colInfo[columnIndex].colPrecision; 653 } 654 655 665 public int getScale(int columnIndex) throws SQLException { 666 checkColRange(columnIndex); 667 return colInfo[columnIndex].colScale; 668 } 669 670 681 public String getTableName(int columnIndex) throws SQLException { 682 checkColRange(columnIndex); 683 return colInfo[columnIndex].tableName; 684 } 685 686 697 public String getCatalogName(int columnIndex) throws SQLException { 698 checkColRange(columnIndex); 699 String str =""; 700 if(colInfo[columnIndex].catName == null){ 701 } else { 702 str = colInfo[columnIndex].catName; 703 } 704 return str; 705 } 706 707 720 public int getColumnType(int columnIndex) throws SQLException { 721 checkColRange(columnIndex); 722 return colInfo[columnIndex].colType; 723 } 724 725 735 public String getColumnTypeName(int columnIndex) throws SQLException { 736 checkColRange(columnIndex); 737 return colInfo[columnIndex].colTypeName; 738 } 739 740 741 752 public boolean isReadOnly(int columnIndex) throws SQLException { 753 checkColRange(columnIndex); 754 return colInfo[columnIndex].readOnly; 755 } 756 757 770 public boolean isWritable(int columnIndex) throws SQLException { 771 checkColRange(columnIndex); 772 return colInfo[columnIndex].writable; 773 } 774 775 786 public boolean isDefinitelyWritable(int columnIndex) 787 throws SQLException { return true;} 788 789 811 public String getColumnClassName(int columnIndex) throws SQLException { 812 String className = (new String ()).getClass().getName(); 813 814 int sqlType = getColumnType(columnIndex); 815 816 switch (sqlType) { 817 818 case Types.NUMERIC: 819 case Types.DECIMAL: 820 className = (new java.math.BigDecimal (0)).getClass().getName (); 821 break; 822 823 case Types.BIT: 824 className = (new Boolean (false)).getClass().getName (); 825 break; 826 827 case Types.TINYINT: 828 className = (new Byte ("0")).getClass().getName (); 829 break; 830 831 case Types.SMALLINT: 832 className = (new Short ("0")).getClass().getName (); 833 break; 834 835 case Types.INTEGER: 836 className = (new Integer (0)).getClass().getName (); 837 break; 838 839 case Types.BIGINT: 840 className = (new Long (0)).getClass().getName (); 841 break; 842 843 case Types.REAL: 844 className = (new Float (0)).getClass().getName (); 845 break; 846 847 case Types.FLOAT: 848 case Types.DOUBLE: 849 className = (new Double (0)).getClass().getName(); 850 break; 851 852 case Types.BINARY: 853 case Types.VARBINARY: 854 case Types.LONGVARBINARY: 855 byte[] b = {}; 856 className = (b.getClass()).getName(); 857 break; 858 859 case Types.DATE: 860 className = (new java.sql.Date (123456)).getClass().getName (); 861 break; 862 863 case Types.TIME: 864 className = (new java.sql.Time (123456)).getClass().getName (); 865 break; 866 867 case Types.TIMESTAMP: 868 className = (new java.sql.Timestamp (123456)).getClass().getName (); 869 break; 870 871 case Types.BLOB: 872 byte[] blob = {}; 873 className = (blob.getClass()).getName(); 874 break; 875 876 case Types.CLOB: 877 char[] c = {}; 878 className = (c.getClass()).getName(); 879 break; 880 } 881 882 return className; 883 } 884 885 static final long serialVersionUID = 6893806403181801867L; 886 887 private class ColInfo implements Serializable { 888 896 public boolean autoIncrement; 897 898 904 public boolean caseSensitive; 905 906 912 public boolean currency; 913 914 922 public int nullable; 923 924 930 public boolean signed; 931 932 939 public boolean searchable; 940 941 947 public int columnDisplaySize; 948 949 955 public String columnLabel; 956 957 962 public String columnName; 963 964 970 public String schemaName; 971 972 980 public int colPrecision; 981 982 988 public int colScale; 989 990 997 public String tableName =""; 998 999 1006 public String catName; 1007 1008 1014 public int colType; 1015 1016 1022 public String colTypeName; 1023 1024 1029 public boolean readOnly = false; 1030 1031 1036 public boolean writable = true; 1037 } 1038} 1039 1040 1041 1042 | Popular Tags |