📄 resultset.java
字号:
public java.sql.Date getDate(int columnIndex) throws java.sql.SQLException { return getDate(columnIndex, null); } /** * JDBC 2.0 Get the value of a column in the current row as a java.sql.Date * object. Use the calendar to construct an appropriate millisecond value * for the Date, if the underlying database doesn't store timezone * information. * * @param columnIndex * the first column is 1, the second is 2, ... * @param cal * the calendar to use in constructing the date * * @return the column value; if the value is SQL NULL, the result is null * * @exception SQLException * if a database-access error occurs. */ public java.sql.Date getDate(int columnIndex, Calendar cal) throws SQLException { if (this.isBinaryEncoded) { return getNativeDate(columnIndex, (cal != null) ? cal.getTimeZone() : this.getDefaultTimeZone()); } if (!this.useFastDateParsing) { String stringVal = getStringInternal(columnIndex, false); if (stringVal == null) { return null; } return getDateFromString(stringVal, columnIndex); } else { checkColumnBounds(columnIndex); return getDateFromBytes(((byte[][])this.thisRow)[columnIndex - 1], columnIndex); } } /** * DOCUMENT ME! * * @param columnName * DOCUMENT ME! * * @return DOCUMENT ME! * * @throws java.sql.SQLException * DOCUMENT ME! */ public java.sql.Date getDate(String columnName) throws java.sql.SQLException { return getDate(findColumn(columnName)); } /** * Get the value of a column in the current row as a java.sql.Date object. * Use the calendar to construct an appropriate millisecond value for the * Date, if the underlying database doesn't store timezone information. * * @param columnName * is the SQL name of the column * @param cal * the calendar to use in constructing the date * * @return the column value; if the value is SQL NULL, the result is null * * @exception SQLException * if a database-access error occurs. */ public java.sql.Date getDate(String columnName, Calendar cal) throws SQLException { return getDate(findColumn(columnName), cal); } private final java.sql.Date getDateFromString(String stringVal, int columnIndex) throws SQLException { int year = 0; int month = 0; int day = 0; try { this.wasNullFlag = false; if (stringVal == null) { this.wasNullFlag = true; return null; } // // JDK-6 doesn't like trailing whitespace // // Note this isn't a performance issue, other // than the iteration over the string, as String.trim() // will return a new string only if whitespace is present // stringVal = stringVal.trim(); if (stringVal.equals("0") || stringVal.equals("0000-00-00") || stringVal.equals("0000-00-00 00:00:00") || stringVal.equals("00000000000000") || stringVal.equals("0")) { if (ConnectionProperties.ZERO_DATETIME_BEHAVIOR_CONVERT_TO_NULL .equals(this.connection.getZeroDateTimeBehavior())) { this.wasNullFlag = true; return null; } else if (ConnectionProperties.ZERO_DATETIME_BEHAVIOR_EXCEPTION .equals(this.connection.getZeroDateTimeBehavior())) { throw SQLError.createSQLException("Value '" + stringVal + "' can not be represented as java.sql.Date", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } // We're left with the case of 'round' to a date Java _can_ // represent, which is '0001-01-01'. return fastDateCreate(null, 1, 1, 1); } else if (this.fields[columnIndex - 1].getMysqlType() == MysqlDefs.FIELD_TYPE_TIMESTAMP) { // Convert from TIMESTAMP switch (stringVal.length()) { case 21: case 19: { // java.sql.Timestamp format year = Integer.parseInt(stringVal.substring(0, 4)); month = Integer.parseInt(stringVal.substring(5, 7)); day = Integer.parseInt(stringVal.substring(8, 10)); return fastDateCreate(null, year, month, day); } case 14: case 8: { year = Integer.parseInt(stringVal.substring(0, 4)); month = Integer.parseInt(stringVal.substring(4, 6)); day = Integer.parseInt(stringVal.substring(6, 8)); return fastDateCreate(null, year, month, day); } case 12: case 10: case 6: { year = Integer.parseInt(stringVal.substring(0, 2)); if (year <= 69) { year = year + 100; } month = Integer.parseInt(stringVal.substring(2, 4)); day = Integer.parseInt(stringVal.substring(4, 6)); return fastDateCreate(null, year + 1900, month, day); } case 4: { year = Integer.parseInt(stringVal.substring(0, 4)); if (year <= 69) { year = year + 100; } month = Integer.parseInt(stringVal.substring(2, 4)); return fastDateCreate(null, year + 1900, month, 1); } case 2: { year = Integer.parseInt(stringVal.substring(0, 2)); if (year <= 69) { year = year + 100; } return fastDateCreate(null, year + 1900, 1, 1); } default: throw SQLError.createSQLException(Messages.getString( "ResultSet.Bad_format_for_Date", new Object[] { stringVal, new Integer(columnIndex) }), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } /* endswitch */ } else if (this.fields[columnIndex - 1].getMysqlType() == MysqlDefs.FIELD_TYPE_YEAR) { if (stringVal.length() == 2 || stringVal.length() == 1) { year = Integer.parseInt(stringVal); if (year <= 69) { year = year + 100; } year += 1900; } else { year = Integer.parseInt(stringVal.substring(0, 4)); } return fastDateCreate(null, year, 1, 1); } else if (this.fields[columnIndex - 1].getMysqlType() == MysqlDefs.FIELD_TYPE_TIME) { return fastDateCreate(null, 1970, 1, 1); // Return EPOCH } else { if (stringVal.length() < 10) { if (stringVal.length() == 8) { return fastDateCreate(null, 1970, 1, 1); // Return EPOCH for TIME } throw SQLError.createSQLException(Messages.getString( "ResultSet.Bad_format_for_Date", new Object[] { stringVal, new Integer(columnIndex) }), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } if (stringVal.length() != 18) { year = Integer.parseInt(stringVal.substring(0, 4)); month = Integer.parseInt(stringVal.substring(5, 7)); day = Integer.parseInt(stringVal.substring(8, 10)); } else { // JDK-1.3 timestamp format, not real easy to parse positionally :p StringTokenizer st = new StringTokenizer(stringVal, "- "); year = Integer.parseInt(st.nextToken()); month = Integer.parseInt(st.nextToken()); day = Integer.parseInt(st.nextToken()); } } return fastDateCreate(null, year, month, day); } catch (SQLException sqlEx) { throw sqlEx; // don't re-wrap } catch (Exception e) { throw SQLError.createSQLException(Messages.getString( "ResultSet.Bad_format_for_Date", new Object[] { stringVal, new Integer(columnIndex) }), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } } private final java.sql.Date getDateFromBytes(byte[] dateAsBytes, int columnIndex) throws SQLException { checkColumnBounds(columnIndex); int year = 0; int month = 0; int day = 0; try { this.wasNullFlag = false; if (dateAsBytes == null) { this.wasNullFlag = true; return null; } boolean allZeroDate = true; boolean onlyTimePresent = StringUtils.indexOf(dateAsBytes, ':') != -1; int length = dateAsBytes.length; for (int i = 0; i < length; i++) { byte b = dateAsBytes[i]; if (b == ' ' || b == '-' || b == '/') { onlyTimePresent = false; } if (b != '0' && b != ' ' && b != ':' && b != '-' && b != '/' && b != '.') { allZeroDate = false; break; } } if (!onlyTimePresent && allZeroDate) { if (ConnectionProperties.ZERO_DATETIME_BEHAVIOR_CONVERT_TO_NULL .equals(this.connection.getZeroDateTimeBehavior())) { this.wasNullFlag = true; return null; } else if (ConnectionProperties.ZERO_DATETIME_BEHAVIOR_EXCEPTION .equals(this.connection.getZeroDateTimeBehavior())) { throw SQLError.createSQLException("Value '" + new String(dateAsBytes) + "' can not be represented as java.sql.Date", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } // We're left with the case of 'round' to a date Java _can_ // represent, which is '0001-01-01'. return fastDateCreate(null, 1, 1, 1); } else if (this.fields[columnIndex - 1].getMysqlType() == MysqlDefs.FIELD_TYPE_TIMESTAMP) { // Convert from TIMESTAMP switch (length) { case 21: case 19: { // java.sql.Timestamp format year = StringUtils.getInt(dateAsBytes, 0, 4); month = StringUtils.getInt(dateAsBytes, 5, 7); day = StringUtils.getInt(dateAsBytes, 8, 10); return fastDateCreate(null, year, month, day); } case 14: case 8: { year = StringUtils.getInt(dateAsBytes, 0, 4); month = StringUtils.getInt(dateAsBytes, 4, 6); day = StringUtils.getInt(dateAsBytes, 6, 8); return fastDateCreate(null, year, month, day); } case 12: case 10: case 6: { year = StringUtils.getInt(dateAsBytes, 0, 2); if (year <= 69) { year = year + 100; } month = StringUtils.getInt(dateAsBytes, 2, 4); day = StringUtils.getInt(dateAsBytes, 4, 6); return fastDateCreate(null, year + 1900, month, day); } case 4: { year = StringUtils.getInt(dateAsBytes, 0, 4); if (year <= 69) { year = year + 100; } month = StringUtils.getInt(dateAsBytes, 2, 4); return fastDateCreate(null, year + 1900, month, 1); } case 2: { year = StringUtils.getInt(dateAsBytes, 0, 2); if (year <= 69) { year = year + 100; } return fastDateCreate(null, year + 1900, 1, 1); } default: throw SQLError.createSQLException(Messages.getString( "ResultSet.Bad_format_for_Date", new Object[] { new String(dateAsBytes), new Integer(columnIndex) }), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } /* endswitch */ } else if (this.fields[columnIndex - 1].getMysqlType() == MysqlDefs.FIELD_TYPE_YEAR) { if (length == 2 || length == 1) { year = StringUtils.getInt(dateAsBytes); if (year <= 69) { year = year + 100; } year += 1900; } else { year = StringUtils.getInt(dateAsBytes, 0, 4); } return fastDateCreate(null, year, 1, 1); } else if (this.fields[columnIndex - 1].getMysqlType() == MysqlDefs.FIELD_TYPE_TIME) { return fastDateCreate(null, 1970, 1, 1); // Return EPOCH } else { if (length < 10) { if (length == 8) { return fastDateCreate(null, 1970, 1, 1); // Return EPOCH for TIME } throw SQLError.createSQLException(Messages.getString( "ResultSet.Bad_format_for_Date", new Object[] { new String(dateAsBytes), new Integer(columnIndex) }), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } if (length != 18) { year = StringUtils.getInt(dateAsBytes, 0, 4); month = StringUtils.getInt(dateAsBytes, 5, 7); day = StringUtils.getInt(dateAsBytes, 8, 10); } else { // JDK-1.3 timestamp format, not real easy to parse positionally :p StringTokenizer st = new StringTokenizer(new String(dateAsBytes), "- "); year = Integer.parseInt(st.nextToken()); month = Integer.parseInt(st.nextToken()); day = Integer.parseInt(st.nextToken()); } } return fastDateCreate(null, year, month, day); } catch (SQLException sqlEx) { throw sqlEx; // don't re-wrap } catch (Exception e) { throw SQLError.createSQLException(Messages.getString( "ResultSet.Bad_format_for_Date", new Object[] { new String(dateAsBytes), new Integer(columnIndex) }), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } } private TimeZone getDefaultTimeZone() { return this.connection.getDefaultTimeZone(); } /** * Get the value of a column in the current row as a Java double. * * @param columnIndex * the first column is 1, the second is 2,... * * @return the column value; 0 if SQL NULL * * @exception SQLException * if a database access error occurs */ public double getDouble(int columnIndex) throws SQLException { if (!this.isBinaryEncoded) { return getDoubleInternal(columnIndex); } return getNativeDouble(columnIndex); } /** * DOCUMENT ME! * * @param columnName * DOCUMENT ME! * * @return DOCUMENT ME! * * @throws SQLException * DOCUMENT ME! */ public double getDouble(String columnName) throws SQLException { return getDouble(findColumn(columnName)); } private final double getDoubleFromString(String stringVal, int columnIndex) throws SQLException { return getDoubleInternal(stringVal, columnIndex); } /** * Converts a string representation of a number to a double. Need a faster * way to do this. * * @param colIndex * the 1-based index of the column to retrieve a double from. * * @return the double value represented by the string in buf * * @throws SQLException * if an error occurs */ protected double getDoubleInternal(int colIndex) throws SQLException { return getDoubleInternal(getString(colIndex), colIndex); } /** * Converts a string representation of a number to a double. Need a faster * way to do this.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -