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

📄 connection.java

📁 用java访问MySQL数据库的JDBC驱动程序。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public void commit() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "commit", Args);
      }

    return;
  }

  /**
   * The method rollback() drops all changes made since the previous
   * commit/rollback and releases any database locks currently held by
   * the Connection.
   *
   * <p><b>Note:</b> MySQL does not support transactions, so this
   *                 method is a no-op.
   *
   * @exception java.sql.SQLException if a database access error occurs
   * @see commit
   */

  public void rollback() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "rollback", Args);
      }

          if (_isClosed) {
                throw new java.sql.SQLException("Rollback attempt on closed connection.", "08003");
          }
  }
  
  /**
   * In some cases, it is desirable to immediately release a Connection's
   * database and JDBC resources instead of waiting for them to be
   * automatically released (cant think why off the top of my head)
   *
   * <B>Note:</B> A Connection is automatically closed when it is
   * garbage collected.  Certain fatal errors also result in a closed
   * connection.
   *
   * @exception java.sql.SQLException if a database access error occurs
   */

  public void close() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "close", Args);
      }
      
    if (_IO != null)
      {
        try {
            _IO.quit();
          } 
        catch (Exception e) {}
        _IO = null;
      }
    _isClosed = true;
  }
  
  /**
   * Tests to see if a Connection is closed
   *
   * @return the status of the connection
   * @exception java.sql.SQLException (why?)
   */

  public boolean isClosed() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "isClosed", Args);
	  Debug.returnValue(this, "isClosed", new Boolean(_isClosed));
      }

      if (!_isClosed) {
	 // Test the connection
    
        try {
            synchronized (_IO) {
		   execSQL(_PING_COMMAND, -1);
            }
        } 
        catch (Exception E) {
		_isClosed = true;
        }
      }
	
      return _isClosed;
  }

  /**
   * A connection's database is able to provide information describing
   * its tables, its supported SQL grammar, its stored procedures, the
   * capabilities of this connection, etc.  This information is made
   * available through a DatabaseMetaData object.
   *
   * @return a DatabaseMetaData object for this connection
   * @exception java.sql.SQLException if a database access error occurs
   */

  public java.sql.DatabaseMetaData getMetaData() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "getMetaData", Args);
      }

      org.gjt.mm.mysql.DatabaseMetaData DBMD = 
	  new org.gjt.mm.mysql.DatabaseMetaData(this, _Database);
      
      if (Driver.trace) {
	  Debug.returnValue(this, "getMetaData", DBMD);
      }

      return DBMD;
  }

  /**
   * You can put a connection in read-only mode as a hint to enable
   * database optimizations
   *
   * <B>Note:</B> setReadOnly cannot be called while in the middle
   * of a transaction
   *
   * @param readOnly - true enables read-only mode; false disables it
   * @exception java.sql.SQLException if a database access error occurs
   */

  public void setReadOnly (boolean readOnly) throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = {new Boolean(readOnly)};
	  Debug.methodCall(this, "setReadOnly", Args);
	  Debug.returnValue(this, "setReadOnly", new Boolean(readOnly));
      }
    _readOnly = readOnly;
  } 

  /**
   * Tests to see if the connection is in Read Only Mode.  Note that
   * we cannot really put the database in read only mode, but we pretend
   * we can by returning the value of the readOnly flag
   *
   * @return true if the connection is read only
   * @exception java.sql.SQLException if a database access error occurs
   */

  public boolean isReadOnly() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "isReadOnly", Args);
	  Debug.returnValue(this, "isReadOnly", new Boolean(_readOnly));
      }

    return _readOnly;
  }

  /**
   * A sub-space of this Connection's database may be selected by
   * setting a catalog name.  If the driver does not support catalogs,
   * it will silently ignore this request
   *
   * <p><b>Note:</b> MySQL's notion of catalogs are individual databases.
   *
   * @exception java.sql.SQLException if a database access error occurs
   */

  public void setCatalog(String Catalog) throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = {Catalog};
	  Debug.methodCall(this, "setCatalog", Args);
      }

        execSQL("USE " + Catalog, -1);
        _Database = Catalog;
  }
  
  /**
   * Return the connections current catalog name, or null if no
   * catalog name is set, or we dont support catalogs.
   *
   * <p><b>Note:</b> MySQL's notion of catalogs are individual databases.
   * @return the current catalog name or null
   * @exception java.sql.SQLException if a database access error occurs
   */

  public String getCatalog() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "getCatalog", Args);
	  Debug.returnValue(this, "getCatalog", _Database);
      }

    return _Database;
  }

  /**
   * You can call this method to try to change the transaction
   * isolation level using one of the TRANSACTION_* values.
   *
   * <B>Note:</B> setTransactionIsolation cannot be called while
   * in the middle of a transaction
   *
   * @param level one of the TRANSACTION_* isolation values with
   *    the exception of TRANSACTION_NONE; some databases may
   *    not support other values
   * @exception java.sql.SQLException if a database access error occurs
   * @see java.sql.DatabaseMetaData#supportsTransactionIsolationLevel
   */

  public void setTransactionIsolation(int level) throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = {new Integer(level)};
	  Debug.methodCall(this, "setTransactionIsolation", Args);
      }

    throw new java.sql.SQLException("Transaction Isolation Levels are not supported.", "S1C00");
  }
  
  /**
   * Get this Connection's current transaction isolation mode.
   *
   * @return the current TRANSACTION_* mode value
   * @exception java.sql.SQLException if a database access error occurs
   */

  public int getTransactionIsolation() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "getTransactionIsolation", Args);
	  Debug.returnValue(this, "getTransactionIsolation", new Integer(java.sql.Connection.TRANSACTION_SERIALIZABLE));
      }
    return java.sql.Connection.TRANSACTION_SERIALIZABLE;
  }
  
  /**
   * The first warning reported by calls on this Connection is
   * returned.
   *
   * <B>Note:</B> Sebsequent warnings will be changed to this
   * java.sql.SQLWarning
   *
   * @return the first java.sql.SQLWarning or null
   * @exception java.sql.SQLException if a database access error occurs
   */

  public java.sql.SQLWarning getWarnings() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "getWarnings", Args);
	  Debug.returnValue(this, "getWarnings", null);
      }

    return null;
  }

  /**
   * After this call, getWarnings returns null until a new warning
   * is reported for this connection.
   *
   * @exception java.sql.SQLException if a database access error occurs
   */
  
  public void clearWarnings() throws java.sql.SQLException
  {
      if (Driver.trace) {
	  Object[] Args = new Object[0];
	  Debug.methodCall(this, "clearWarnings", Args);
      }

    // firstWarning = null;
  }

  // *********************************************************************
  //
  //                END OF PUBLIC INTERFACE
  //
  // *********************************************************************

  /**
   * Send a query to the server.  Returns one of the ResultSet
   * objects.
   *
   * This is synchronized, so Statement's queries
   * will be serialized.
   *
   * @param sql the SQL statement to be executed
   * @return a ResultSet holding the results
   * @exception java.sql.SQLException if a database error occurs
   */

  ResultSet execSQL(String Sql, int max_rows) 
      throws java.sql.SQLException
  {
	  return execSQL(Sql, max_rows, null);
  }
  
  ResultSet execSQL(String Sql, int max_rows, Buffer Packet) 
      throws java.sql.SQLException
  {  
      synchronized (_IO) {

	  if (_high_availability) {
	      try {
		  _IO.sqlQuery(_PING_COMMAND, MysqlDefs.MAX_ROWS);
	      }
	      catch (Exception Ex) {

		  double timeout = _initial_timeout;
		  boolean connection_good = false;

		  for (int i = 0; i < _max_reconnects; i++) {
        
		      try {
			  _IO = new MysqlIO(_Host, _port);
			  _IO.init(_User, _Password);
			  _IO.sendCommand(MysqlDefs.INIT_DB, _Database, null);
			  _IO.sqlQuery(_PING_COMMAND, MysqlDefs.MAX_ROWS);
					  
			  connection_good = true;
			  break;
		      } 
		      catch (Exception EEE) {}
                  
		      try {
			  Thread.currentThread().sleep((long)timeout * 1000);
			  timeout = timeout * timeout;
		      }
		      catch (InterruptedException IE) {}
		  }

		  if (!connection_good) { // We've really failed!
		      throw new SQLException("Server connection failure during transaction. \nAttemtped reconnect " + _max_reconnects + " times. Giving up.", "08001");
		  }
	      }
	  }

	  try {
	      int real_max_rows = ( max_rows == -1 ) ? 
		  MysqlDefs.MAX_ROWS : max_rows;

	      if (Packet == null) {
		  String Encoding = null;

		  if (useUnicode()) {
		      Encoding = getEncoding();
		  }

		  return _IO.sqlQuery(Sql, real_max_rows, Encoding);
	      }
	      else {
		  return _IO.sqlQueryDirect(Packet, real_max_rows);
	      }
	  }
	  catch (java.io.EOFException EOFE) {
	      throw new java.sql.SQLException("Lost connection to server during query", "08007");
	  }
	  catch (Exception E) {
	      String ExceptionType = E.getClass().getName();
	      String ExceptionMessage = E.getMessage();
    
	      throw new java.sql.SQLException("Error during query: Unexpected Exception: " + ExceptionType + " message given: " + ExceptionMessage, "S1000");
	  }
      }
  }

  String getURL()
  {
    return _MyURL;
  }

  String getUser()
  {
    return _User;
  }

  String getServerVersion()
  {
    return _IO.getServerVersion();
  }

    int getServerMajorVersion()
    {
        return _IO.getServerMajorVersion();
    }

    int getServerMinorVersion()
    {
        return _IO.getServerMinorVersion();
    }

    int getServerSubMinorVersion()
    {
        return _IO.getServerSubMinorVersion();
    }

    void maxRowsChanged()
    {
        _max_rows_changed = true;
    }

    boolean useMaxRows()
    {
        return _max_rows_changed;
    }

    boolean useUnicode()
    {
	return _do_unicode;
    }

    String getEncoding()
    {
	return _Encoding;
    }

    Object getMutex()
    {
	return _IO;
    }
}

⌨️ 快捷键说明

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