List of usage examples for java.sql Types JAVA_OBJECT
int JAVA_OBJECT
To view the source code for java.sql Types JAVA_OBJECT.
Click Source Link
JAVA_OBJECT
. From source file:com.opencsv.ResultSetHelperService.java
private String getColumnValue(ResultSet rs, int colType, int colIndex, boolean trim, String dateFormatString, String timestampFormatString) throws SQLException, IOException { String value = ""; switch (colType) { case Types.BIT: case Types.JAVA_OBJECT: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getObject(colIndex), ""); value = ObjectUtils.toString(rs.getObject(colIndex), ""); break;/*from w w w. j a v a 2 s . c o m*/ case Types.BOOLEAN: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getBoolean(colIndex)); value = ObjectUtils.toString(rs.getBoolean(colIndex)); break; case Types.NCLOB: // todo : use rs.getNClob case Types.CLOB: Clob c = rs.getClob(colIndex); if (c != null) { StrBuilder sb = new StrBuilder(); sb.readFrom(c.getCharacterStream()); value = sb.toString(); } break; case Types.BIGINT: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getLong(colIndex)); value = ObjectUtils.toString(rs.getLong(colIndex)); break; case Types.DECIMAL: case Types.REAL: case Types.NUMERIC: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getBigDecimal(colIndex), ""); value = ObjectUtils.toString(rs.getBigDecimal(colIndex), ""); break; case Types.DOUBLE: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getDouble(colIndex)); value = ObjectUtils.toString(rs.getDouble(colIndex)); break; case Types.FLOAT: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getFloat(colIndex)); value = ObjectUtils.toString(rs.getFloat(colIndex)); break; case Types.INTEGER: case Types.TINYINT: case Types.SMALLINT: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getInt(colIndex)); value = ObjectUtils.toString(rs.getInt(colIndex)); break; case Types.DATE: java.sql.Date date = rs.getDate(colIndex); if (date != null) { SimpleDateFormat df = new SimpleDateFormat(dateFormatString); value = df.format(date); } break; case Types.TIME: // Once Java 7 is the minimum supported version. // value = Objects.toString(rs.getTime(colIndex), ""); value = ObjectUtils.toString(rs.getTime(colIndex), ""); break; case Types.TIMESTAMP: value = handleTimestamp(rs.getTimestamp(colIndex), timestampFormatString); break; case Types.NVARCHAR: // todo : use rs.getNString case Types.NCHAR: // todo : use rs.getNString case Types.LONGNVARCHAR: // todo : use rs.getNString case Types.LONGVARCHAR: case Types.VARCHAR: case Types.CHAR: String columnValue = rs.getString(colIndex); if (trim && columnValue != null) { value = columnValue.trim(); } else { value = columnValue; } break; default: value = ""; } if (rs.wasNull() || value == null) { value = ""; } return value; }
From source file:org.apache.openjpa.jdbc.schema.Schemas.java
/** * Return the {@link Types} constant for the given SQL type name. *//*from w w w . j a v a 2 s . co m*/ public static int getJDBCType(String name) { if ("array".equalsIgnoreCase(name)) return Types.ARRAY; if ("bigint".equalsIgnoreCase(name)) return Types.BIGINT; if ("binary".equalsIgnoreCase(name)) return Types.BINARY; if ("bit".equalsIgnoreCase(name)) return Types.BIT; if ("blob".equalsIgnoreCase(name)) return Types.BLOB; if ("char".equalsIgnoreCase(name)) return Types.CHAR; if ("clob".equalsIgnoreCase(name)) return Types.CLOB; if ("date".equalsIgnoreCase(name)) return Types.DATE; if ("decimal".equalsIgnoreCase(name)) return Types.DECIMAL; if ("distinct".equalsIgnoreCase(name)) return Types.DISTINCT; if ("double".equalsIgnoreCase(name)) return Types.DOUBLE; if ("float".equalsIgnoreCase(name)) return Types.FLOAT; if ("integer".equalsIgnoreCase(name)) return Types.INTEGER; if ("java_object".equalsIgnoreCase(name)) return Types.JAVA_OBJECT; if ("longvarbinary".equalsIgnoreCase(name)) return Types.LONGVARBINARY; if ("longvarchar".equalsIgnoreCase(name)) return Types.LONGVARCHAR; if ("null".equalsIgnoreCase(name)) return Types.NULL; if ("numeric".equalsIgnoreCase(name)) return Types.NUMERIC; if ("other".equalsIgnoreCase(name)) return Types.OTHER; if ("real".equalsIgnoreCase(name)) return Types.REAL; if ("ref".equalsIgnoreCase(name)) return Types.REF; if ("smallint".equalsIgnoreCase(name)) return Types.SMALLINT; if ("struct".equalsIgnoreCase(name)) return Types.STRUCT; if ("time".equalsIgnoreCase(name)) return Types.TIME; if ("timestamp".equalsIgnoreCase(name)) return Types.TIMESTAMP; if ("tinyint".equalsIgnoreCase(name)) return Types.TINYINT; if ("varbinary".equalsIgnoreCase(name)) return Types.VARBINARY; if ("varchar".equalsIgnoreCase(name)) return Types.VARCHAR; if (name == null || name.toLowerCase().startsWith("unknown")) return Types.OTHER; throw new IllegalArgumentException("name = " + name); }
From source file:org.apache.ddlutils.platform.sybase.SybasePlatform.java
/** * Creates a new platform instance.//from w w w. j a v a2s . c om */ public SybasePlatform() { PlatformInfo info = getPlatformInfo(); info.setMaxIdentifierLength(28); info.setNullAsDefaultValueRequired(true); info.setIdentityColumnAutomaticallyRequired(true); info.setMultipleIdentityColumnsSupported(false); info.setPrimaryKeyColumnsHaveToBeRequired(true); info.setCommentPrefix("/*"); info.setCommentSuffix("*/"); info.addNativeTypeMapping(Types.ARRAY, "IMAGE"); // BIGINT is mapped back in the model reader info.addNativeTypeMapping(Types.BIGINT, "DECIMAL(19,0)"); // we're not using the native BIT type because it is rather limited (cannot be NULL, cannot be indexed) info.addNativeTypeMapping(Types.BIT, "SMALLINT", Types.SMALLINT); info.addNativeTypeMapping(Types.BLOB, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.BOOLEAN, "SMALLINT", Types.SMALLINT); info.addNativeTypeMapping(Types.CLOB, "TEXT", Types.LONGVARCHAR); info.addNativeTypeMapping(Types.DATALINK, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.DATE, "DATETIME", Types.TIMESTAMP); info.addNativeTypeMapping(Types.DISTINCT, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION"); info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION", Types.DOUBLE); info.addNativeTypeMapping(Types.INTEGER, "INT"); info.addNativeTypeMapping(Types.JAVA_OBJECT, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.LONGVARBINARY, "IMAGE"); info.addNativeTypeMapping(Types.LONGVARCHAR, "TEXT"); info.addNativeTypeMapping(Types.NULL, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.OTHER, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.REF, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.STRUCT, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.TIME, "DATETIME", Types.TIMESTAMP); info.addNativeTypeMapping(Types.TIMESTAMP, "DATETIME", Types.TIMESTAMP); info.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT); info.setDefaultSize(Types.BINARY, 254); info.setDefaultSize(Types.VARBINARY, 254); info.setDefaultSize(Types.CHAR, 254); info.setDefaultSize(Types.VARCHAR, 254); setSqlBuilder(new SybaseBuilder(this)); setModelReader(new SybaseModelReader(this)); }
From source file:org.apache.ddlutils.platform.mssql.MSSqlPlatform.java
/** * Creates a new platform instance./*from ww w. j av a 2 s . com*/ */ public MSSqlPlatform() { PlatformInfo info = getPlatformInfo(); info.setMaxIdentifierLength(128); info.setPrimaryKeyColumnAutomaticallyRequired(true); info.setIdentityColumnAutomaticallyRequired(true); info.setMultipleIdentityColumnsSupported(false); info.setSupportedOnUpdateActions( new CascadeActionEnum[] { CascadeActionEnum.CASCADE, CascadeActionEnum.NONE }); info.addEquivalentOnUpdateActions(CascadeActionEnum.NONE, CascadeActionEnum.RESTRICT); info.setSupportedOnDeleteActions( new CascadeActionEnum[] { CascadeActionEnum.CASCADE, CascadeActionEnum.NONE }); info.addEquivalentOnDeleteActions(CascadeActionEnum.NONE, CascadeActionEnum.RESTRICT); info.addNativeTypeMapping(Types.ARRAY, "IMAGE", Types.LONGVARBINARY); // BIGINT will be mapped back to BIGINT by the model reader info.addNativeTypeMapping(Types.BIGINT, "DECIMAL(19,0)"); info.addNativeTypeMapping(Types.BLOB, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.BOOLEAN, "BIT", Types.BIT); info.addNativeTypeMapping(Types.CLOB, "TEXT", Types.LONGVARCHAR); info.addNativeTypeMapping(Types.DATALINK, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.DATE, "DATETIME", Types.TIMESTAMP); info.addNativeTypeMapping(Types.DISTINCT, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.DOUBLE, "FLOAT", Types.FLOAT); info.addNativeTypeMapping(Types.INTEGER, "INT"); info.addNativeTypeMapping(Types.JAVA_OBJECT, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.LONGVARBINARY, "IMAGE"); info.addNativeTypeMapping(Types.LONGVARCHAR, "TEXT"); info.addNativeTypeMapping(Types.NULL, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.OTHER, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.REF, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.STRUCT, "IMAGE", Types.LONGVARBINARY); info.addNativeTypeMapping(Types.TIME, "DATETIME", Types.TIMESTAMP); info.addNativeTypeMapping(Types.TIMESTAMP, "DATETIME"); info.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT); info.setDefaultSize(Types.CHAR, 254); info.setDefaultSize(Types.VARCHAR, 254); info.setDefaultSize(Types.BINARY, 254); info.setDefaultSize(Types.VARBINARY, 254); setSqlBuilder(new MSSqlBuilder(this)); setModelReader(new MSSqlModelReader(this)); }
From source file:com.squid.core.jdbc.formatter.DefaultJDBCDataFormatter.java
public String formatJDBCObject(final Object jdbcObject, final int colType) throws SQLException { String value = ""; switch (colType) { case Types.BIT: if (jdbcObject != null) { value = String.valueOf(jdbcObject); }//from w w w . j a v a 2 s .c o m break; case Types.BOOLEAN: if (jdbcObject != null) { value = new Boolean(jdbcObject.toString()).toString(); } break; case Types.BIGINT: case Types.DECIMAL: case Types.DOUBLE: case Types.FLOAT: case Types.REAL: case Types.NUMERIC: if (jdbcObject != null) { value = "" + formatter.formatDecimal(jdbcObject); } break; case Types.INTEGER: case Types.TINYINT: case Types.SMALLINT: if (jdbcObject != null) { value = "" + formatter.formatDecimal(jdbcObject); } break; case Types.CLOB: if (jdbcObject != null) { try { value = read((Clob) jdbcObject); } catch (SQLException sqle) { value = ""; } catch (IOException ioe) { value = ""; } } break; case Types.ARRAY: try { ResultSet rs = ((Array) jdbcObject).getResultSet(); int arrayType = ((Array) jdbcObject).getBaseType(); boolean isNotNew = false; while (rs.next()) { String current = formatJDBCObject(rs.getObject(2), arrayType); if ("".equals(current.trim()) == false) { if (isNotNew) { value = value + ","; } else { isNotNew = !isNotNew; } value = value + current; } } if ("".equals(value) == false) { value = "{" + value + "}"; } } catch (SQLException sqle) { value = ""; } break; case Types.JAVA_OBJECT: if (jdbcObject != null) { value = String.valueOf(jdbcObject); } break; case Types.DATE: if (jdbcObject != null) { value = formatter.formatDate(jdbcObject); } break; case Types.TIME: if (jdbcObject != null) { value = ((Time) jdbcObject).toString(); } break; case Types.TIMESTAMP: if (jdbcObject != null) { value = formatter.formatTimestamp(jdbcObject); } break; case Types.LONGVARCHAR: case Types.VARCHAR: case Types.CHAR: if (jdbcObject != null) { value = jdbcObject.toString(); } else { value = "NULL"; } break; case Types.BINARY: value = new String((byte[]) jdbcObject); break; default: value = ""; } if (value == null) { value = ""; } return value; }
From source file:com.jaspersoft.jrx.query.PlSqlQueryExecuter.java
private void createStatement() throws JRException { String queryString = getQueryString(); if (connection != null && queryString != null && queryString.trim().length() > 0) { try {/*from w w w . j a va 2 s. co m*/ isStoredProcedure = isOracleStoredProcedure(queryString); if (isStoredProcedure) { statement = connection.prepareCall(queryString); } else { statement = connection.prepareStatement(queryString); } int fetchSize = JRPropertiesUtil.getInstance(getJasperReportsContext()).getIntegerProperty( dataset.getPropertiesMap(), JRJdbcQueryExecuterFactory.PROPERTY_JDBC_FETCH_SIZE, 0); if (fetchSize > 0) { statement.setFetchSize(fetchSize); } List<String> parameterNames = getCollectedParameterNames(); if (!parameterNames.isEmpty()) { for (int i = 0; i < parameterNames.size(); i++) { String parameterName = (String) parameterNames.get(i); JRValueParameter parameter = getValueParameter(parameterName); Class<?> clazz = parameter.getValueClass(); Object parameterValue = parameter.getValue(); if (clazz.equals(java.lang.Object.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.JAVA_OBJECT); } else { statement.setObject(i + 1, parameterValue); } } else if (clazz.equals(java.lang.Boolean.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.BIT); } else { statement.setBoolean(i + 1, ((Boolean) parameterValue).booleanValue()); } } else if (clazz.equals(java.lang.Byte.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.TINYINT); } else { statement.setByte(i + 1, ((Byte) parameterValue).byteValue()); } } else if (clazz.equals(java.lang.Double.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.DOUBLE); } else { statement.setDouble(i + 1, ((Double) parameterValue).doubleValue()); } } else if (clazz.equals(java.lang.Float.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.FLOAT); } else { statement.setFloat(i + 1, ((Float) parameterValue).floatValue()); } } else if (clazz.equals(java.lang.Integer.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.INTEGER); } else { statement.setInt(i + 1, ((Integer) parameterValue).intValue()); } } else if (clazz.equals(java.lang.Long.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.BIGINT); } else { statement.setLong(i + 1, ((Long) parameterValue).longValue()); } } else if (clazz.equals(java.lang.Short.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.SMALLINT); } else { statement.setShort(i + 1, ((Short) parameterValue).shortValue()); } } else if (clazz.equals(java.math.BigDecimal.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.DECIMAL); } else { statement.setBigDecimal(i + 1, (BigDecimal) parameterValue); } } else if (clazz.equals(java.lang.String.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.VARCHAR); } else { statement.setString(i + 1, parameterValue.toString()); } } else if (clazz.equals(java.util.Date.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.DATE); } else { statement.setDate(i + 1, new java.sql.Date(((java.util.Date) parameterValue).getTime())); } } else if (clazz.equals(java.sql.Timestamp.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.TIMESTAMP); } else { statement.setTimestamp(i + 1, (java.sql.Timestamp) parameterValue); } } else if (clazz.equals(java.sql.Time.class)) { if (parameterValue == null) { statement.setNull(i + 1, Types.TIME); } else { statement.setTime(i + 1, (java.sql.Time) parameterValue); } } else if (clazz.equals(java.sql.ResultSet.class)) { if (!isStoredProcedure) { throw new JRException("OUT paramater used in non-stored procedure call : " + parameterName + " class " + clazz.getName()); } else if (cursorParameter > 0) { throw new JRException("A stored procedure can have at most one cursor parameter : " + parameterName + " class " + clazz.getName()); } ((CallableStatement) statement).registerOutParameter(i + 1, ORACLE_CURSOR_TYPE); cursorParameter = i + 1; } else { throw new JRException("Parameter type not supported in query : " + parameterName + " class " + clazz.getName()); } } } } catch (SQLException e) { throw new JRException("Error preparing statement for executing the report query : " + "\n\n" + queryString + "\n\n", e); } } }
From source file:org.brucalipto.sqlutil.DB2SQLManager.java
/** * Method useful for using STORED PROCEDURE * @param spib The {@link SPInputBean} bean containing data to execute the stored procedure * @return The {@link SPOutputBean} containing returned values */// ww w . j av a 2 s . c o m public SPOutputBean executeSP(final SPInputBean spib) throws SQLException { Connection conn = null; CallableStatement call = null; ResultSet resultSet = null; final String procedureName = spib.spName; SPParameter[] inputParameters = spib.inputParams; int[] outputParameters = spib.outputParams; final int inputParametersSize = inputParameters.length; final int outputParametersSize = outputParameters.length; final StringBuffer spName = new StringBuffer("call ").append(procedureName).append('('); int totalParameters = inputParametersSize + outputParametersSize; for (int i = 0; i < totalParameters; i++) { if (i != totalParameters - 1) { spName.append("?,"); } else { spName.append('?'); } } spName.append(")"); try { if (this.dataSource != null) { conn = this.dataSource.getConnection(); } else { conn = this.connection; } call = conn.prepareCall(spName.toString()); for (int i = 0; i < inputParametersSize; i++) { final SPParameter inputParam = inputParameters[i]; final int sqlType = inputParam.sqlType; final Object inputParamValue = inputParam.value; log.debug((i + 1) + ") Setting input value: " + inputParam); if (inputParamValue == null) { call.setNull(i + 1, sqlType); continue; } switch (sqlType) { case Types.VARCHAR: call.setString(i + 1, (String) inputParamValue); break; case Types.INTEGER: if (inputParamValue instanceof Integer) { call.setInt(i + 1, ((Integer) inputParamValue).intValue()); } else if (inputParamValue instanceof Long) { call.setLong(i + 1, ((Long) inputParamValue).longValue()); } break; case Types.DATE: call.setDate(i + 1, (Date) inputParamValue); break; case Types.BOOLEAN: call.setBoolean(i + 1, ((Boolean) inputParamValue).booleanValue()); break; case Types.CHAR: call.setString(i + 1, ((Character) inputParamValue).toString()); break; case Types.DOUBLE: call.setDouble(i + 1, ((Double) inputParamValue).doubleValue()); break; case Types.FLOAT: call.setFloat(i + 1, ((Float) inputParamValue).floatValue()); break; case Types.TIMESTAMP: call.setTimestamp(i + 1, (Timestamp) inputParamValue); break; default: call.setObject(i + 1, inputParamValue); break; } } for (int i = 0; i < outputParametersSize; i++) { int sqlType = outputParameters[i]; log.debug((i + 1) + ") Registering output type 'Types." + SQLUtilTypes.SQL_TYPES.get(Integer.valueOf("" + sqlType)) + "'"); call.registerOutParameter(inputParametersSize + i + 1, sqlType); } log.debug("Going to call: '" + procedureName + "'"); long elapsedTime = System.currentTimeMillis(); boolean hasResultSet = call.execute(); log.debug("SP '" + procedureName + "' executed in " + (System.currentTimeMillis() - elapsedTime) + "millis"); if (hasResultSet) { log.debug("This SP is going to return also a resultSet"); } final SPOutputBean output = new SPOutputBean(); for (int i = 0; i < outputParametersSize; i++) { int sqlType = outputParameters[i]; log.debug((i + 1) + ") Getting output type 'Types." + SQLUtilTypes.SQL_TYPES.get(Integer.valueOf("" + sqlType)) + "'"); final Object spResult = call.getObject(inputParametersSize + i + 1); SPParameter outParam = new SPParameter(sqlType, spResult); output.addResult(outParam); } if (hasResultSet) { RowSetDynaClass rowSetDynaClass = new RowSetDynaClass(call.getResultSet(), false); if (log.isDebugEnabled()) { log.debug("Going to return a RowSetDynaClass with following properties:"); DynaProperty[] properties = rowSetDynaClass.getDynaProperties(); for (int i = 0; i < properties.length; i++) { log.debug("Name: '" + properties[i].getName() + "'; Type: '" + properties[i].getType().getName() + "'"); } } SPParameter outParam = new SPParameter(Types.JAVA_OBJECT, rowSetDynaClass); output.addResult(outParam); } return output; } finally { closeResources(resultSet, call, conn); } }
From source file:CSVWriter.java
private static String getColumnValue(ResultSet rs, int colType, int colIndex) throws SQLException, IOException { String value = ""; //w ww . ja v a 2s .c o m switch (colType) { case Types.BIT: Object bit = rs.getObject(colIndex); if (bit != null) { value = String.valueOf(bit); } break; case Types.BOOLEAN: boolean b = rs.getBoolean(colIndex); if (!rs.wasNull()) { value = Boolean.valueOf(b).toString(); } break; case Types.CLOB: Clob c = rs.getClob(colIndex); if (c != null) { value = read(c); } break; case Types.BIGINT: case Types.DECIMAL: case Types.DOUBLE: case Types.FLOAT: case Types.REAL: case Types.NUMERIC: BigDecimal bd = rs.getBigDecimal(colIndex); if (bd != null) { value = "" + bd.doubleValue(); } break; case Types.INTEGER: case Types.TINYINT: case Types.SMALLINT: int intValue = rs.getInt(colIndex); if (!rs.wasNull()) { value = "" + intValue; } break; case Types.JAVA_OBJECT: Object obj = rs.getObject(colIndex); if (obj != null) { value = String.valueOf(obj); } break; case Types.DATE: java.sql.Date date = rs.getDate(colIndex); if (date != null) { value = DATE_FORMATTER.format(date);; } break; case Types.TIME: Time t = rs.getTime(colIndex); if (t != null) { value = t.toString(); } break; case Types.TIMESTAMP: Timestamp tstamp = rs.getTimestamp(colIndex); if (tstamp != null) { value = TIMESTAMP_FORMATTER.format(tstamp); } break; case Types.LONGVARCHAR: case Types.VARCHAR: case Types.CHAR: value = rs.getString(colIndex); break; default: value = ""; } if (value == null) { value = ""; } return value; }
From source file:org.executequery.gui.resultset.ResultSetTableModel.java
public void createTable(ResultSet resultSet) { if (!isOpenAndValid(resultSet)) { clearData();//from ww w .j a v a2s.c om return; } try { resetMetaData(); ResultSetMetaData rsmd = resultSet.getMetaData(); columnHeaders.clear(); visibleColumnHeaders.clear(); tableData.clear(); int zeroBaseIndex = 0; int count = rsmd.getColumnCount(); for (int i = 1; i <= count; i++) { zeroBaseIndex = i - 1; columnHeaders.add(new ResultSetColumnHeader(zeroBaseIndex, rsmd.getColumnLabel(i), rsmd.getColumnName(i), rsmd.getColumnType(i), rsmd.getColumnTypeName(i))); } int recordCount = 0; interrupted = false; if (holdMetaData) { setMetaDataVectors(rsmd); } List<RecordDataItem> rowData; long time = System.currentTimeMillis(); while (resultSet.next()) { if (interrupted || Thread.interrupted()) { throw new InterruptedException(); } recordCount++; rowData = new ArrayList<RecordDataItem>(count); for (int i = 1; i <= count; i++) { zeroBaseIndex = i - 1; ResultSetColumnHeader header = columnHeaders.get(zeroBaseIndex); RecordDataItem value = recordDataItemFactory.create(header); try { int dataType = header.getDataType(); switch (dataType) { // some drivers (informix for example) // was noticed to return the hashcode from // getObject for -1 data types (eg. longvarchar). // force string for these - others stick with // getObject() for default value formatting case Types.CHAR: case Types.VARCHAR: value.setValue(resultSet.getString(i)); break; case Types.DATE: value.setValue(resultSet.getDate(i)); break; case Types.TIME: value.setValue(resultSet.getTime(i)); break; case Types.TIMESTAMP: value.setValue(resultSet.getTimestamp(i)); break; case Types.LONGVARCHAR: case Types.CLOB: value.setValue(resultSet.getClob(i)); break; case Types.LONGVARBINARY: case Types.VARBINARY: case Types.BINARY: value.setValue(resultSet.getBytes(i)); break; case Types.BLOB: value.setValue(resultSet.getBlob(i)); break; case Types.BIT: case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: case Types.FLOAT: case Types.REAL: case Types.DOUBLE: case Types.NUMERIC: case Types.DECIMAL: case Types.NULL: case Types.OTHER: case Types.JAVA_OBJECT: case Types.DISTINCT: case Types.STRUCT: case Types.ARRAY: case Types.REF: case Types.DATALINK: case Types.BOOLEAN: case Types.ROWID: case Types.NCHAR: case Types.NVARCHAR: case Types.LONGNVARCHAR: case Types.NCLOB: case Types.SQLXML: // use getObject for all other known types value.setValue(resultSet.getObject(i)); break; default: // otherwise try as string asStringOrObject(value, resultSet, i); break; } } catch (Exception e) { try { // ... and on dump, resort to string value.setValue(resultSet.getString(i)); } catch (SQLException sqlException) { // catch-all SQLException - yes, this is hideous // noticed with invalid date formatted values in mysql value.setValue("<Error - " + sqlException.getMessage() + ">"); } } if (resultSet.wasNull()) { value.setNull(); } rowData.add(value); } tableData.add(rowData); if (recordCount == maxRecords) { break; } } if (Log.isTraceEnabled()) { Log.trace("Finished populating table model - " + recordCount + " rows - [ " + MiscUtils.formatDuration(System.currentTimeMillis() - time) + "]"); } fireTableStructureChanged(); } catch (SQLException e) { System.err.println("SQL error populating table model at: " + e.getMessage()); Log.debug("Table model error - " + e.getMessage(), e); } catch (Exception e) { if (e instanceof InterruptedException) { Log.debug("ResultSet generation interrupted.", e); } else { String message = e.getMessage(); if (StringUtils.isBlank(message)) { System.err.println("Exception populating table model."); } else { System.err.println("Exception populating table model at: " + message); } Log.debug("Table model error - ", e); } } finally { if (resultSet != null) { try { resultSet.close(); Statement statement = resultSet.getStatement(); if (statement != null) { statement.close(); } } catch (SQLException e) { } } } }
From source file:CreateNewTable.java
private static Vector getDataTypes(Connection con) throws SQLException { String structName = null, distinctName = null, javaName = null; // create a vector of class DataType initialized with // the SQL code, the SQL type name, and two null entries // for the local type name and the creation parameter(s) Vector dataTypes = new Vector(); dataTypes.add(new DataType(java.sql.Types.BIT, "BIT")); dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT")); dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT")); dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER")); dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT")); dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT")); dataTypes.add(new DataType(java.sql.Types.REAL, "REAL")); dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE")); dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC")); dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL")); dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR")); dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR")); dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR")); dataTypes.add(new DataType(java.sql.Types.DATE, "DATE")); dataTypes.add(new DataType(java.sql.Types.TIME, "TIME")); dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP")); dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY")); dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY")); dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY")); dataTypes.add(new DataType(java.sql.Types.NULL, "NULL")); dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER")); dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB")); dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB")); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { int codeNumber = rs.getInt("DATA_TYPE"); String dbmsName = rs.getString("TYPE_NAME"); String createParams = rs.getString("CREATE_PARAMS"); if (codeNumber == Types.STRUCT && structName == null) structName = dbmsName;/*from w w w. j a v a2 s . com*/ else if (codeNumber == Types.DISTINCT && distinctName == null) distinctName = dbmsName; else if (codeNumber == Types.JAVA_OBJECT && javaName == null) javaName = dbmsName; else { for (int i = 0; i < dataTypes.size(); i++) { // find entry that matches the SQL code, // and if local type and params are not already set, // set them DataType type = (DataType) dataTypes.get(i); if (type.getCode() == codeNumber) { type.setLocalTypeAndParams(dbmsName, createParams); } } } } int[] types = { Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT }; rs = dbmd.getUDTs(null, "%", "%", types); while (rs.next()) { String typeName = null; DataType dataType = null; if (dbmd.isCatalogAtStart()) typeName = rs.getString(1) + dbmd.getCatalogSeparator() + rs.getString(2) + "." + rs.getString(3); else typeName = rs.getString(2) + "." + rs.getString(3) + dbmd.getCatalogSeparator() + rs.getString(1); switch (rs.getInt(5)) { case Types.STRUCT: dataType = new DataType(Types.STRUCT, typeName); dataType.setLocalTypeAndParams(structName, null); break; case Types.DISTINCT: dataType = new DataType(Types.DISTINCT, typeName); dataType.setLocalTypeAndParams(distinctName, null); break; case Types.JAVA_OBJECT: dataType = new DataType(Types.JAVA_OBJECT, typeName); dataType.setLocalTypeAndParams(javaName, null); break; } dataTypes.add(dataType); } return dataTypes; }