⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resultset.java

📁 用java访问MySQL数据库的JDBC驱动程序。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public float getFloat(int columnIndex) throws java.sql.SQLException
    {
	String S = getString(columnIndex);

	if (S != null) {
	    if (S.length() == 0) {
		return 0;
	    }
	    try {
		return Float.valueOf(S).floatValue();
	    } 
	    catch (NumberFormatException E) {
		throw new java.sql.SQLException ("Bad format for float '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
	    }
	}
	return 0;           // SQL NULL
    }
  
    /**
     * 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 java.sql.SQLException if a database access error occurs
     */

    public double getDouble(int columnIndex) throws java.sql.SQLException
    {
	String S = getString(columnIndex);

	if (S != null) {
	    if (S.length() == 0) {
		return 0;
	    }
	    try {
		return Double.valueOf(S).doubleValue();
	    } 
	    catch (NumberFormatException E) {
		throw new java.sql.SQLException ("Bad format for double '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
	    }
	}
	return 0;           // SQL NULL
    }

    /**
     * Get the value of a column in the current row as a
     * java.lang.BigDecimal object
     *
     * @param columnIndex  the first column is 1, the second is 2...
     * @param scale the number of digits to the right of the decimal
     * @return the column value; if the value is SQL NULL, null
     * @exception java.sql.SQLException if a database access error occurs
     */

    public BigDecimal getBigDecimal(int columnIndex, int scale) 
	throws java.sql.SQLException
    {
	String S = getString(columnIndex);
	BigDecimal Val;

	if (S != null) {
	    if (S.length() == 0) {
		Val = new BigDecimal(0);
		return Val.setScale(scale);
	    }
	    try {
		Val = new BigDecimal(S);
	    } 
	    catch (NumberFormatException E) {
		throw new java.sql.SQLException ("Bad format for BigDecimal '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
	    }
	    try {
		return Val.setScale(scale);
	    } 
	    catch (ArithmeticException E) {
		throw new java.sql.SQLException ("Bad format for BigDecimal '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
	    }
	}
	return null;                // SQL NULL
    }

    /**
     * Get the value of a column in the current row as a Java byte array.
     *
     * <p><b>Be warned</b> If the blob is huge, then you may run out
     * of memory.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return the column value; if the value is SQL NULL, the result
     *    is null
     * @exception java.sql.SQLException if a database access error occurs
     */

    public byte[] getBytes(int columnIndex) throws java.sql.SQLException
    {
	if (columnIndex < 1 || columnIndex > Fields.length)
	    throw new java.sql.SQLException("Column Index out of range ( " + columnIndex + " > " + Fields.length + ").", "S1002");

	try {
	    if (This_Row[columnIndex - 1] == null) {
		wasNullFlag = true;
	    }
	    else {
		wasNullFlag = false;
	    }
	}
	catch (NullPointerException E) {
	    wasNullFlag = true;
	}
  
	if(wasNullFlag) {
	    return null;
	}
	else {
	    return This_Row[columnIndex - 1];
	}
    }
    
    /**
     * Get the value of a column in the current row as a java.sql.Date
     * object
     *
     * @param columnIndex the first column is 1, the second is 2...
     * @return the column value; null if SQL NULL
     * @exception java.sql.SQLException if a database access error occurs
     */
  
    public java.sql.Date getDate(int columnIndex) throws java.sql.SQLException
    {
	Integer Y = null, M = null, D = null;
	String S = "";
	
   	try {
	    S = getString(columnIndex);
	    
	    if (S == null) {
		return null;
	    }
	    else if (Fields[columnIndex - 1].getMysqlType() == MysqlDefs.FIELD_TYPE_TIMESTAMP) {
				// Convert from TIMESTAMP
		switch (S.length()) {
		case 14: 
		case 8:
		    {
			Y =  new Integer(S.substring(0,4));
			M =  new Integer(S.substring(4,6));
			D =  new Integer(S.substring(6,8));
			return new java.sql.Date(Y.intValue()-1900, M.intValue()-1,D.intValue());
		    }
		case 12: 
		case 10: 
		case 6:
		    {
			Y  = new Integer(S.substring(0,2));
			M  = new Integer(S.substring(2,4));
			D  = new Integer(S.substring(4,6));
			return new java.sql.Date(Y.intValue(), M.intValue()-1,D.intValue());
		    }
		case 4:
		    {
			Y  = new Integer(S.substring(0,2));
			M  = new Integer(S.substring(2,4));
			return new java.sql.Date(Y.intValue(), M.intValue()-1, 1);
		    }
		case 2:
		    {
			Y  = new Integer(S.substring(0,2));
			return new java.sql.Date(Y.intValue(), 0, 1);
		    }
		default:
		    throw new SQLException("Bad format for Date '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
		} /* endswitch */
	    }
	    else {
		if( S.length() < 10) {
		    throw new SQLException("Bad format for Date '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
		}
		
		Y = new Integer(S.substring(0,4));
		M = new Integer(S.substring(5,7));
		D = new Integer(S.substring(8,10));
	    }

	    return new java.sql.Date(Y.intValue()-1900, M.intValue()-1,D.intValue());
	}
	catch( Exception e ) {
	    throw new java.sql.SQLException("Cannot convert value '" + S + "' from column " + columnIndex + "(" + Fields[columnIndex]+ " ) to DATE.", "S1009");
	}
    }

    /**
     * Get the value of a column in the current row as a java.sql.Time
     * object
     *
     * @param columnIndex the first column is 1, the second is 2...
     * @return the column value; null if SQL NULL
     * @exception java.sql.SQLException if a database access error occurs
     */
    
    public Time getTime(int columnIndex) throws java.sql.SQLException
    {  
	int hr = 0, min = 0, sec = 0;
	
	try {
	    String S = getString(columnIndex);
	    if (S == null) return null;
	    
	    Field F = Fields[columnIndex - 1];

	    if (F.getMysqlType() == MysqlDefs.FIELD_TYPE_TIMESTAMP) {
				// It's a timestamp
   		int length = S.length();
		switch (length) {
		case 14:
		case 12: {
		    hr  = Integer.parseInt(S.substring(length - 6, length - 4));
		    min = Integer.parseInt(S.substring(length - 4, length - 2));
		    sec = Integer.parseInt(S.substring(length -2, length));
		}
		break;
		case 10: {
		    hr  = Integer.parseInt(S.substring(6, 8));
		    min = Integer.parseInt(S.substring(8,10));
		    sec = 0;
		}
		break;
		default:
		    throw new SQLException("Timestamp too small to convert to Time value in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
		} /* endswitch */
	  
		SQLWarning W = new SQLWarning("Precision lost converting TIMESTAMP to Time with getTime() on column " + columnIndex + "(" + Fields[columnIndex] + ").");

		if (Warnings == null) {
		    Warnings = W;
		}
		else {
		    Warnings.setNextWarning(W);
		}
	    }
	    else if (F.getMysqlType() == MysqlDefs.FIELD_TYPE_DATETIME) {
		
   		hr  = Integer.parseInt(S.substring(11, 13));
   		min = Integer.parseInt(S.substring(14, 16));
   		sec = Integer.parseInt(S.substring(17, 19)); 

   		SQLWarning W = new SQLWarning("Precision lost converting DATETIME to Time with getTime() on column " + columnIndex + "(" + Fields[columnIndex] + ").");
		
   		if (Warnings == null) {
		    Warnings = W;
		}
   		else {
		    Warnings.setNextWarning(W);
   		}
	    }
	    else {
				// convert a String to a Time
		
   		if (S.length() != 5 && S.length() != 8) {
		    throw new SQLException("Bad format for Time '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
   		}

   		hr  = Integer.parseInt(S.substring(0,2));
   		min = Integer.parseInt(S.substring(3,5));
   		sec = (S.length() == 5) ? 0 : Integer.parseInt(S.substring(6));
	    }
	    return new Time(hr, min, sec);
	}
	catch( Exception E ) {
	    throw new java.sql.SQLException(E.getClass().getName(), "S1009");
	}
    }



    /**
     * Get the value of a column in the current row as a
     * java.sql.Timestamp object
     *
     * @param columnIndex the first column is 1, the second is 2...
     * @return the column value; null if SQL NULL
     * @exception java.sql.SQLException if a database access error occurs
     */
    
    public Timestamp getTimestamp(int columnIndex) throws java.sql.SQLException
    {
	String S = getString(columnIndex);
	
	if (S == null) {
	    return null;
	}

	try {
	    switch (S.length()) {
	    case 19:
		{
		    try {
			java.util.Date D = _TSDF.parse(S);
			return new java.sql.Timestamp(D.getTime());
		    }
		    catch (ParseException E) {
			throw new java.sql.SQLException("Bad format for Timestamp '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
		    }
		}
	    case 14:
		{
		    Integer Y =  new Integer(S.substring(0,4));
		    Integer M =  new Integer(S.substring(4,6));
		    Integer D =  new Integer(S.substring(6,8));
		    Integer HR = new Integer(S.substring(8,10));
		    Integer MI = new Integer(S.substring(10,12));
		    Integer SE = new Integer(S.substring(12,14));
		    return new java.sql.Timestamp(Y.intValue()-1900, M.intValue()-1, D.intValue(), HR.intValue(), MI.intValue(), SE.intValue(), 0);
		}
	    case 12:
		{
		    Integer Y  = new Integer(S.substring(0,2));
		    Integer M  = new Integer(S.substring(2,4));
		    Integer D  = new Integer(S.substring(4,6));
		    Integer HR = new Integer(S.substring(6,8));
		    Integer MI = new Integer(S.substring(8,10));
		    Integer SE = new Integer(S.substring(10,12));
		    return new java.sql.Timestamp(Y.intValue(), M.intValue()-1, D.intValue(), HR.intValue(), MI.intValue(), SE.intValue(), 0);
		}
	    case 10:
		{
		    Integer Y  = new Integer(S.substring(0,2));
		    Integer M  = new Integer(S.substring(2,4));
		    Integer D  = new Integer(S.substring(4,6));
		    Integer HR = new Integer(S.substring(6,8));
		    Integer MI = new Integer(S.substring(8,10));
		    return new java.sql.Timestamp(Y.intValue(), M.intValue()-1, D.intValue(), HR.intValue(), MI.intValue(), 0, 0);
		}
	    case 8:
		{
		    Integer Y = new Integer(S.substring(0,4));
		    Integer M = new Integer(S.substring(4,6));
		    Integer D = new Integer(S.substring(6,8));
		    return new java.sql.Timestamp(Y.intValue()-1900, M.intValue()-1, D.intValue(), 0, 0, 0, 0);
		}
	    case 6:
		{
		    Integer Y = new Integer(S.substring(0,2));
		    Integer M = new Integer(S.substring(2,4));
		    Integer D = new Integer(S.substring(4,6));
		    return new java.sql.Timestamp(Y.intValue(), M.intValue()-1, D.intValue(), 0, 0, 0, 0);
		}
	    case 4:
		{
		    Integer Y = new Integer(S.substring(0,2));
		    Integer M = new Integer(S.substring(2,4));
		    return new java.sql.Timestamp(Y.intValue(), M.intValue()-1, 1, 0, 0, 0, 0);
		}
	    case 2:
		{
		    Integer Y = new Integer(S.substring(0,2));
		    return new java.sql.Timestamp(Y.intValue(), 0, 1, 0, 0, 0, 0);
		}
	    default:
		throw new java.sql.SQLException("Bad format for Timestamp '" + S + "' in column " + columnIndex + "(" + Fields[columnIndex] + ").", "S1009");
	    }
	}
	catch( Exception e ) {
	    throw new java.sql.SQLException(e.getClass().getName(), "S1009");
	}
    }

    /**
     * A column value can be retrieved as a stream of ASCII characters
     * and then read in chunks from the stream.  This method is
     * particulary suitable for retrieving large LONGVARCHAR values.
     * The JDBC driver will do any necessary conversion from the
     * database format into ASCII.
     *
     * <p><B>Note:</B> All the data in the returned stream must be read
     * prior to getting the value of any other column.  The next call
     * to a get method implicitly closes the stream.  Also, a stream
     * may return 0 for available() whether there is data available
     * or not.
     *
     * @param columnIndex the first column is 1, the second is 2, ...
     * @return a Java InputStream that delivers the database column
     *    value as a stream of one byte ASCII characters.  If the
     *    value is SQL NULL then the result is null
     * @exception java.sql.SQLException if a database access error occurs
     * @see getBinaryStream
     */

    public InputStream getAsciiStream(int columnIndex) throws java.sql.SQLException
    {
	return getBinaryStream(columnIndex);
    }

    /**
     * A column value can also be retrieved as a stream of Unicode
     * characters. We implement this as a binary stream.
     *
     * @param columnIndex the first column is 1, the second is 2...
     * @return a Java InputStream that delivers the database column value
     *    as a stream of two byte Unicode characters.  If the value is
     *    SQL NULL, then the result is null
     * @exception java.sql.SQLException if a database access error occurs
     * @see getAsciiStream
     * @see getBinaryStream
     */
  
    public InputStream getUnicodeStream(int columnIndex) throws java.sql.SQLException
    {
	return getBinaryStream(columnIndex);
    }

    /**
     * A column value can also be retrieved as a binary strea.  This
     * method is suitable for retrieving LONGVARBINARY values.
     *
     * @param columnIndex the first column is 1, the second is 2...
     * @return a Java InputStream that delivers the database column value
     * as a stream of bytes.  If the value is SQL NULL, then the result
     * is null
     * @exception java.sql.SQLException if a database access error occurs
     * @see getAsciiStream
     * @see getUnicodeStream

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -