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

📄 jdbc4resultset.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 Copyright  2002-2007 MySQL AB, 2008 Sun Microsystems

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as 
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL 
 as it is applied to this software. View the full text of the 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 software distribution.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

package com.mysql.jdbc;

import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.sql.NClob;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Field;
import com.mysql.jdbc.NotUpdatable;
import com.mysql.jdbc.ResultSetImpl;
import com.mysql.jdbc.RowData;
import com.mysql.jdbc.SQLError;
import com.mysql.jdbc.Statement;


public class JDBC4ResultSet extends ResultSetImpl {

	public JDBC4ResultSet(long updateCount, long updateID, 
			ConnectionImpl conn, StatementImpl creatorStmt) {
		super(updateCount, updateID, conn, creatorStmt);
	}
	
	public JDBC4ResultSet(String catalog, Field[] fields, RowData tuples,
			ConnectionImpl conn, StatementImpl creatorStmt) throws SQLException {
		super(catalog, fields, tuples, conn, creatorStmt);
	}
	
	/**
	 * JDBC 4.0
	 * 
	 * <p>
	 * Get the value of a column in the current row as a java.io.Reader.
	 * </p>
	 * 
	 * @param columnIndex
	 *            the column to get the value from
	 * 
	 * @return the value in the column as a java.io.Reader.
	 * 
	 * @throws SQLException
	 *             if an error occurs
	 */
	public Reader getNCharacterStream(int columnIndex) throws SQLException {
		checkColumnBounds(columnIndex);
		
		String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
		if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
			throw new SQLException(
					"Can not call getNCharacterStream() when field's charset isn't UTF-8");
		}
		return getCharacterStream(columnIndex);
	}

	/**
	 * JDBC 4.0
	 * 
	 * <p>
	 * Get the value of a column in the current row as a java.io.Reader.
	 * </p>
	 * 
	 * @param columnName
	 *            the column name to retrieve the value from
	 * 
	 * @return the value as a java.io.Reader
	 * 
	 * @throws SQLException
	 *             if an error occurs
	 */
	public Reader getNCharacterStream(String columnName) throws SQLException {
		return getNCharacterStream(findColumn(columnName));
	}

	/**
	 * JDBC 4.0 Get a NCLOB column.
	 * 
	 * @param i
	 *            the first column is 1, the second is 2, ...
	 * 
	 * @return an object representing a NCLOB
	 * 
	 * @throws SQLException
	 *             if an error occurs
	 */
	public NClob getNClob(int columnIndex) throws SQLException {
		checkColumnBounds(columnIndex);
		
		String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
		if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
			throw new SQLException(
					"Can not call getNClob() when field's charset isn't UTF-8");
		}
		if (!this.isBinaryEncoded) {
			String asString = getStringForNClob(columnIndex);

			if (asString == null) {
				return null;
			}

			return new com.mysql.jdbc.JDBC4NClob(asString);
		}

		return getNativeNClob(columnIndex);
	}

	/**
	 * JDBC 4.0 Get a NCLOB column.
	 * 
	 * @param colName
	 *            the column name
	 * 
	 * @return an object representing a NCLOB
	 * 
	 * @throws SQLException
	 *             if an error occurs
	 */
	public NClob getNClob(String columnName) throws SQLException {
		return getNClob(findColumn(columnName));
	}
	
	/**
	 * JDBC 4.0 Get a NCLOB column.
	 * 
	 * @param columnIndex
	 *            the first column is 1, the second is 2, ...
	 * 
	 * @return an object representing a NCLOB
	 * 
	 * @throws SQLException
	 *             if an error occurs
	 */
	protected java.sql.NClob getNativeNClob(int columnIndex)
			throws SQLException {
		String stringVal = getStringForNClob(columnIndex);

		if (stringVal == null) {
			return null;
		}

		return getNClobFromString(stringVal, columnIndex);
	}
	
	private String getStringForNClob(int columnIndex) throws SQLException {
		String asString = null;

		String forcedEncoding = "UTF-8";

		try {
			byte[] asBytes = null;

			if (!this.isBinaryEncoded) {
				asBytes = getBytes(columnIndex);
			} else {
				asBytes = getNativeBytes(columnIndex, true);
			}

			if (asBytes != null) {
				asString = new String(asBytes, forcedEncoding);
			}
		} catch (UnsupportedEncodingException uee) {
			throw SQLError.createSQLException("Unsupported character encoding "
					+ forcedEncoding, SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
		}

		return asString;
	}
	
	private final java.sql.NClob getNClobFromString(String stringVal,
			int columnIndex) throws SQLException {
		return new com.mysql.jdbc.JDBC4NClob(stringVal);
	}
	
	/**
	 * JDBC 4.0
	 * 
	 * Get the value of a column in the current row as a Java String
	 * 
	 * @param columnIndex
	 *            the first column is 1, the second is 2...
	 * 
	 * @return the column value, null for SQL NULL
	 * 
	 * @exception SQLException
	 *                if a database access error occurs
	 */
	public String getNString(int columnIndex) throws SQLException {
		checkColumnBounds(columnIndex);
		
		String fieldEncoding = this.fields[columnIndex - 1].getCharacterSet();
		if (fieldEncoding == null || !fieldEncoding.equals("UTF-8")) {
			throw new SQLException(
					"Can not call getNString() when field's charset isn't UTF-8");
		}
		return getString(columnIndex);
	}
	
	/**
	 * JDBC 4.0
	 * 
	 * The following routines simply convert the columnName into a columnIndex
	 * and then call the appropriate routine above.
	 * 
	 * @param columnName
	 *            is the SQL name of the column
	 * 
	 * @return the column value
	 * 
	 * @exception SQLException
	 *                if a database access error occurs
	 */
	public String getNString(String columnName) throws SQLException {
		return getNString(findColumn(columnName));
	}
	
	/**
	 * JDBC 4.0 Update a column with a character stream value. The updateXXX()
	 * methods are used to update column values in the current row, or the
	 * insert row. The updateXXX() methods do not update the underlying
	 * database, instead the updateRow() or insertRow() methods are called to
	 * update the database.
	 * 
	 * @param columnIndex
	 *            the first column is 1, the second is 2, ...
	 * @param x
	 *            the new column value
	 * @param length
	 *            the length of the stream
	 * 
	 * @exception SQLException
	 *                if a database-access error occurs
	 * @throws NotUpdatable
	 *             DOCUMENT ME!
	 */
	public void updateNCharacterStream(int columnIndex, Reader x, int length)
			throws SQLException {
		throw new NotUpdatable();
	}

⌨️ 快捷键说明

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