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

📄 monetconnection.java

📁 这个是内存数据库的客户端
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		}	}	/**	 * Small helper method to convert a byte string to a hexadecimal	 * string representation.	 *	 * @param digest the byte array to convert	 * @return the byte array as hexadecimal string	 */	private static String toHex(byte[] digest) {		StringBuffer r = new StringBuffer(digest.length * 2);		for (int i = 0; i < digest.length; i++) {			// zero out higher bits to get unsigned conversion			int b = digest[i] << 24 >>> 24;			if (b < 16) r.append("0");			r.append(Integer.toHexString(b));		}		return(r.toString());	}	//== methods of interface Connection	/**	 * Clears all warnings reported for this Connection object. After a	 * call to this method, the method getWarnings returns null until a	 * new warning is reported for this Connection object.	 */	public void clearWarnings() {		warnings = null;	}	/**	 * Releases this Connection object's database and JDBC resources	 * immediately instead of waiting for them to be automatically	 * released. All Statements created from this Connection will be	 * closed when this method is called.	 * <br /><br />	 * Calling the method close on a Connection object that is already	 * closed is a no-op.	 */	public void close() {		Iterator it = statements.keySet().iterator();		while (it.hasNext()) {			try {				((Statement)it.next()).close();			} catch (SQLException e) {				// better luck next time!			}		}		// close the socket		monet.disconnect();		// report ourselves as closed		closed = true;	}	/**	 * Makes all changes made since the previous commit/rollback	 * permanent and releases any database locks currently held by this	 * Connection object.  This method should be used only when	 * auto-commit mode has been disabled.	 *	 * @throws SQLException if a database access error occurs or this	 *         Connection object is in auto-commit mode	 * @see #setAutoCommit(boolean)	 */	public void commit() throws SQLException {		// note: can't use sendIndependentCommand here because we need		// to process the auto_commit state the server gives		// create a container for the result		ResponseList l = new ResponseList(			0,			0,			ResultSet.FETCH_FORWARD,			ResultSet.CONCUR_READ_ONLY		);		// send commit to the server		try {			l.processQuery("COMMIT");		} finally {			l.close();		}	}	/**	 * Creates a Statement object for sending SQL statements to the	 * database.  SQL statements without parameters are normally	 * executed using Statement objects. If the same SQL statement is	 * executed many times, it may be more efficient to use a	 * PreparedStatement object.	 * <br /><br />	 * Result sets created using the returned Statement object will by	 * default be type TYPE_FORWARD_ONLY and have a concurrency level of	 * CONCUR_READ_ONLY.	 *	 * @return a new default Statement object	 * @throws SQLException if a database access error occurs	 */	public Statement createStatement() throws SQLException {		return(createStatement(					ResultSet.TYPE_FORWARD_ONLY,					ResultSet.CONCUR_READ_ONLY));	}	/**	 * Creates a Statement object that will generate ResultSet objects	 * with the given type and concurrency. This method is the same as	 * the createStatement method above, but it allows the default	 * result set type and concurrency to be overridden.	 *	 * @param resultSetType a result set type; one of	 *        ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE,	 *        or ResultSet.TYPE_SCROLL_SENSITIVE	 * @param resultSetConcurrency a concurrency type; one of	 *        ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE	 * @return a new Statement object that will generate ResultSet objects with	 *         the given type and concurrency	 * @throws SQLException if a database access error occurs	 */	public Statement createStatement(		int resultSetType,		int resultSetConcurrency)		throws SQLException	{		try {			Statement ret =				new MonetStatement(					this, resultSetType, resultSetConcurrency				);			// store it in the map for when we close...			statements.put(ret, null);			return(ret);		} catch (IllegalArgumentException e) {			throw new SQLException(e.toString());		}		// we don't have to catch SQLException because that is declared to		// be thrown	}	public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) {return(null);}	/**	 * Retrieves the current auto-commit mode for this Connection	 * object.	 *	 * @return the current state of this Connection object's auto-commit	 *         mode	 * @see #setAutoCommit(boolean)	 */	public boolean getAutoCommit() throws SQLException {		return(autoCommit);	}	/**	 * Retrieves this Connection object's current catalog name.	 *	 * @return the current catalog name or null if there is none	 * @throws SQLException if a database access error occurs or the	 *         current language is not SQL	 */	public String getCatalog() throws SQLException {		if (lang != LANG_SQL)			throw new SQLException("This method is only supported in SQL mode");		// this is a dirty hack, but it works as long as MonetDB		// only handles one catalog (dbfarm) at a time		ResultSet rs = getMetaData().getCatalogs();		if (rs.next()) {			String ret = rs.getString(1);			rs.close();			return(ret);		} else {			return(null);		}	}		/**	 * Retrieves the current holdability of ResultSet objects created	 * using this Connection object.	 *	 * @return the holdability, one of	 *         ResultSet.HOLD_CURSORS_OVER_COMMIT or	 *         ResultSet.CLOSE_CURSORS_AT_COMMIT	 * @throws SQLException is a database access error occors	 */	public int getHoldability() {		// TODO: perhaps it is better to have the server implement		//       CLOSE_CURSORS_AT_COMMIT		return(ResultSet.HOLD_CURSORS_OVER_COMMIT);	}	/**	 * Retrieves a DatabaseMetaData object that contains metadata about	 * the database to which this Connection object represents a	 * connection. The metadata includes information about the	 * database's tables, its supported SQL grammar, its stored	 * procedures, the capabilities of this connection, and so on.	 *	 * @throws SQLException if the current language is not SQL	 * @return a DatabaseMetaData object for this Connection object	 */	public DatabaseMetaData getMetaData() throws SQLException {		if (lang != LANG_SQL)			throw new SQLException("This method is only supported in SQL mode");		return(new MonetDatabaseMetaData(this));	}	/**	 * Retrieves this Connection object's current transaction isolation	 * level.	 *	 * @return the current transaction isolation level, which will be	 *         Connection.TRANSACTION_SERIALIZABLE	 */	public int getTransactionIsolation() {		return(TRANSACTION_SERIALIZABLE);	}	/**	 * Retrieves the Map object associated with this Connection object.	 * Unless the application has added an entry, the type map returned	 * will be empty.	 *	 * @return the java.util.Map object associated with this Connection	 *         object	 */	public Map getTypeMap() {		return(typeMap);	}	/**	 * Retrieves the first warning reported by calls on this Connection	 * object.  If there is more than one warning, subsequent warnings	 * will be chained to the first one and can be retrieved by calling	 * the method SQLWarning.getNextWarning on the warning that was	 * retrieved previously.	 * <br /><br />	 * This method may not be called on a closed connection; doing so	 * will cause an SQLException to be thrown.	 * <br /><br />	 * Note: Subsequent warnings will be chained to this SQLWarning.	 *	 * @return the first SQLWarning object or null if there are none	 * @throws SQLException if a database access error occurs or this method is	 *         called on a closed connection	 */	public SQLWarning getWarnings() throws SQLException {		if (closed) throw new SQLException("Cannot call on closed Connection");		// if there are no warnings, this will be null, which fits with the		// specification.		return(warnings);	}	/**	 * Retrieves whether this Connection object has been closed.  A	 * connection is closed if the method close has been called on it or	 * if certain fatal errors have occurred.  This method is guaranteed	 * to return true only when it is called after the method	 * Connection.close has been called.	 * <br /><br />	 * This method generally cannot be called to determine whether a	 * connection to a database is valid or invalid.  A typical client	 * can determine that a connection is invalid by catching any	 * exceptions that might be thrown when an operation is attempted.	 *	 * @return true if this Connection object is closed; false if it is	 *         still open	 */	public boolean isClosed() {		return(closed);	}	/**	 * Retrieves whether this Connection object is in read-only mode.	 * MonetDB currently doesn't support updateable result sets.	 *	 * @return true if this Connection object is read-only; false otherwise	 */	public boolean isReadOnly() {		return(true);	}	public String nativeSQL(String sql) {return(sql);}	public CallableStatement prepareCall(String sql) {return(null);}	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) {return(null);}	public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {return(null);}	/**	 * Creates a PreparedStatement object for sending parameterized SQL	 * statements to the database.	 * <br /><br />	 * A SQL statement with or without IN parameters can be pre-compiled	 * and stored in a PreparedStatement object. This object can then be	 * used to efficiently execute this statement multiple times.	 * <br /><br />	 * Note: This method is optimized for handling parametric SQL	 * statements that benefit from precompilation. If the driver	 * supports precompilation, the method prepareStatement will send	 * the statement to the database for precompilation. Some drivers	 * may not support precompilation. In this case, the statement may	 * not be sent to the database until the PreparedStatement object is	 * executed. This has no direct effect on users; however, it does	 * affect which methods throw certain SQLException objects.	 * <br /><br />	 * Result sets created using the returned PreparedStatement object	 * will by default be type TYPE_FORWARD_ONLY and have a concurrency	 * level of CONCUR_READ_ONLY.	 *	 * @param sql an SQL statement that may contain one or more '?' IN	 *        parameter placeholders	 * @return a new default PreparedStatement object containing the	 *         pre-compiled SQL statement	 * @throws SQLException if a database access error occurs	 */	public PreparedStatement prepareStatement(String sql) throws SQLException {		return(			prepareStatement(					sql,					ResultSet.TYPE_FORWARD_ONLY,					ResultSet.CONCUR_READ_ONLY			)		);	}	/**	 * Creates a PreparedStatement object that will generate ResultSet	 * objects with the given type and concurrency.  This method is the	 * same as the prepareStatement method above, but it allows the	 * default result set type and concurrency to be overridden.	 *	 * @param sql a String object that is the SQL statement to be sent to the	 *            database; may contain one or more ? IN parameters	 * @param resultSetType a result set type; one of	 *        ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE,	 *        or ResultSet.TYPE_SCROLL_SENSITIVE	 * @param resultSetConcurrency a concurrency type; one of	 *        ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE	 * @return a new PreparedStatement object containing the pre-compiled SQL	 *         statement that will produce ResultSet objects with the given	 *         type and concurrency	 * @throws SQLException if a database access error occurs or the given	 *                      parameters are not ResultSet constants indicating	 *                      type and concurrency	 */	public PreparedStatement prepareStatement(		String sql,		int resultSetType,		int resultSetConcurrency)		throws SQLException	{		try {			PreparedStatement ret;			// use a server-side PreparedStatement			ret = new MonetPreparedStatement(					this, resultSetType, resultSetConcurrency, sql			);			// store it in the map for when we close...			statements.put(ret, null);			return(ret);		} catch (IllegalArgumentException e) {			throw new SQLException(e.toString());		}		// we don't have to catch SQLException because that is declared to		// be thrown	}	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) {return(null);}	public PreparedStatement prepareStatement(String sql, int[] columnIndexes) {return(null);}	public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {return(null);}	public PreparedStatement prepareStatement(String sql, String[] columnNames) {return(null);}	/**	 * Removes the given Savepoint object from the current transaction.	 * Any reference to the savepoint after it have been removed will	 * cause an SQLException to be thrown.	 *	 * @param savepoint the Savepoint object to be removed	 * @throws SQLException if a database access error occurs or the given	 *         Savepoint object is not a valid savepoint in the current	 *         transaction	 */	public void releaseSavepoint(Savepoint savepoint) throws SQLException {		if (!(savepoint instanceof MonetSavepoint)) throw			new SQLException("This driver can only handle savepoints it created itself");		MonetSavepoint sp = (MonetSavepoint)savepoint;		// note: can't use sendIndependentCommand here because we need		// to process the auto_commit state the server gives		// create a container for the result		ResponseList l = new ResponseList(			0,			0,			ResultSet.FETCH_FORWARD,			ResultSet.CONCUR_READ_ONLY		);		// send the appropriate query string to the database		try {			l.processQuery("RELEASE SAVEPOINT " + sp.getName());		} finally {			l.close();		}	}	/**	 * Undoes all changes made in the current transaction and releases	 * any database locks currently held by this Connection object. This	 * method should be used only when auto-commit mode has been	 * disabled.	 *	 * @throws SQLException if a database access error occurs or this

⌨️ 快捷键说明

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