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

📄 jdbcconnection.java

📁 一个可以在applet窗体上持行sql语句并显示返回结果的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    if(Trace.TRACE) Trace.trace(catalog);
  }
  /**
   * Returns the Connection's current catalog name.
   *
   * @return the current catalog name or null
   */
  public String getCatalog() {
    if(Trace.TRACE) Trace.trace();
    return null;
  }
  /**
   * Attempts to change the transaction
   * isolation level to the one given.
	 * The constants defined in the interface <code>Connection</code>
	 * are the possible transaction isolation levels.
   *
   * <P><B>Note:</B> This method should not be called while
   * in the middle of a transaction.
   * <P><font color="#009900">
   * Hypersonic SQL currently ignores this call.
   * </font><P>
   * @param level one of the TRANSACTION_* isolation values with the
   * exception of TRANSACTION_NONE; some databases may not support
   * other values
   * @see DatabaseMetaData#supportsTransactionIsolationLevel
   */
  public void setTransactionIsolation(int level) {
    if(Trace.TRACE) Trace.trace(level);
  }
  /**
   * Gets this Connection's current transaction isolation level.
   *
   * @return the current TRANSACTION_* mode value
   */
  public int getTransactionIsolation() {
    if(Trace.TRACE) Trace.trace();
    return Connection.TRANSACTION_READ_UNCOMMITTED;
  }
  /**
   * Returns the first warning reported by calls on this Connection.
   * <P><font color="#009900">
   * Hypersonic SQL never produces warnings and returns always null.
   * </font><P>
   * @return the first SQLWarning or null
   */
  public SQLWarning getWarnings() {
    if(Trace.TRACE) Trace.trace();
    return null;
  }
  /**
   * Clears all warnings reported for this <code>Connection</code> object.
   * After a call to this method, the method <code>getWarnings</code>
	 * returns null until a new warning is
   * reported for this Connection.
   */
  public void clearWarnings() {
    if(Trace.TRACE) Trace.trace();
  }
  /**
   * JDBC 2.0
   *
	 * Creates a <code>Statement</code> object that will generate
	 * <code>ResultSet</code> objects with the given type and concurrency.
   * This method is the same as the <code>createStatement</code> method
	 * above, but it allows the default result set
   * type and result set concurrency type to be overridden.
   * <P><font color="#009900">
   * Hypersonic SQL currently supports type TYPE_FORWARD_ONLY and
   * concurrency CONCUR_READ_ONLY.
   * </font><P>
   * @param resultSetType a result set type; see ResultSet.TYPE_XXX
   * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
   * @return a new Statement object
   * @exception SQLException if a database access error occurs
   */
//#ifdef JAVA2
  public Statement createStatement(int type,int concurrency)
  throws SQLException {
    return createStatement();
  }
//#endif JAVA2
  /**
   * JDBC 2.0
   *
	 * Creates a <code>PreparedStatement</code> object that will generate
	 * <code>ResultSet</code> objects with the given type and concurrency.
   * This method is the same as the <code>prepareStatement</code> method
	 * above, but it allows the default result set
   * type and result set concurrency type to be overridden.
   * <P><font color="#009900">
   * Hypersonic SQL currently supports type TYPE_FORWARD_ONLY and
   * concurrency CONCUR_READ_ONLY.
   * </font><P>
   * @param resultSetType a result set type; see ResultSet.TYPE_XXX
   * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
   * @return a new PreparedStatement object containing the
   * pre-compiled SQL statement
   * @exception SQLException if a database access error occurs
   */
//#ifdef JAVA2
  public PreparedStatement prepareStatement(String sql,int type,
  int concurrency) throws SQLException {
    return prepareStatement(sql);
  }
//#endif JAVA2
  /**
   * JDBC 2.0
   *
	 * Creates a <code>CallableStatement</code> object that will generate
	 * <code>ResultSet</code> objects with the given type and concurrency.
   * This method is the same as the <code>prepareCall</code> method
   * above, but it allows the default result set
   * type and result set concurrency type to be overridden.
   * <P><font color="#009900">
   * Hypersonic SQL currently supports type TYPE_FORWARD_ONLY and
   * concurrency CONCUR_READ_ONLY.
   * </font><P>
   * @param resultSetType a result set type; see ResultSet.TYPE_XXX
   * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
   * @return a new CallableStatement object containing the
   * pre-compiled SQL statement
   * @exception SQLException if a database access error occurs
   */
//#ifdef JAVA2
  public CallableStatement prepareCall(String sql,int resultSetType,
  int resultSetConcurrency) throws SQLException {
    return prepareCall(sql);
  }
//#endif JAVA2
  /**
   * JDBC 2.0
   *
   * Gets the type map object associated with this connection.
   * Unless the application has added an entry to the type map,
	 * the map returned will be empty.
   *
	 * @return the <code>java.util.Map</code> object associated
	 *         with this <code>Connection</code> object
   */
//#ifdef JAVA2
  public Map getTypeMap() throws SQLException {
    // todo
    return new HashMap(0);
  }
//#endif JAVA2
  /**
   * JDBC 2.0
   *
   * Installs the given type map as the type map for
   * this connection.  The type map will be used for the
	 * custom mapping of SQL structured types and distinct types.
	 *
	 * @param the <code>java.util.Map</code> object to install
	 *        as the replacement for this <code>Connection</code>
	 *        object's default type map
   */
//#ifdef JAVA2
  public void setTypeMap(Map map) throws SQLException {
    throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED);
  }
//#endif JAVA2

  jdbcConnection(String s,String user,String password)
  throws SQLException {
    if(Trace.TRACE) Trace.trace(s);
    bAutoCommit=true;
    sDatabaseName=s;
    if(s.toUpperCase().startsWith("HTTP://")) {
      iType=HTTP;
      openHTTP(user,password);
    } else if(s.toUpperCase().startsWith("HSQL://")) {
      iType=HSQL;
      openHSQL(user,password);
    } else {
      iType=STANDALONE;
      openStandalone(user,password);
    }
  }
  jdbcConnection(Channel c) throws SQLException {
    iType=INTERNAL;
    cChannel=c;
    dDatabase=c.getDatabase();
  }
  jdbcResultSet execute(String s) throws SQLException {
    if(Trace.TRACE) Trace.trace(s);
    Trace.check(!bClosed,Trace.CONNECTION_IS_CLOSED);
    if(iType==HTTP) {
      return executeHTTP(s);
    } else if(iType==HSQL) {
      return executeHSQL(s);
    } else {
      // internal and standalone
      return executeStandalone(s);
    }
  }
  boolean usesLocalFiles() {
    // Standalone connections do, HTTP connections not
    return iType!=HTTP;
  }
  String getName() {
    return sDatabaseName;
  }

  // HTTP
  private String sConnect,sUser,sPassword;
  final static String ENCODING="8859_1";
  private void openHTTP(String user,String password)
  throws SQLException {
    sConnect=sDatabaseName;
    sUser=user;
    sPassword=password;
    executeHTTP(" ");
  }
  private synchronized jdbcResultSet executeHTTP(String s) throws SQLException {
    byte result[];
    try {
      URL url=new URL(sConnect);
      String p=StringConverter.unicodeToHexString(sUser);
      p+="+"+StringConverter.unicodeToHexString(sPassword);
      p+="+"+StringConverter.unicodeToHexString(s);
      URLConnection c=url.openConnection();
      c.setDoOutput(true);
      OutputStream os=c.getOutputStream();
      os.write(p.getBytes(ENCODING));
      os.close();
      c.connect();
      InputStream is=(InputStream)c.getContent();
      BufferedInputStream in=new BufferedInputStream(is);
      int len=c.getContentLength();
      result=new byte[len];
      for(int i=0;i<len;i++) {
        int r=in.read();
        result[i]=(byte)r;
      }
    } catch(Exception e) {
      throw Trace.error(Trace.CONNECTION_IS_BROKEN,e.getMessage());
    }
    return new jdbcResultSet(new Result(result));
  }

  // HSQL
  Socket sSocket;
  DataOutputStream dOutput;
  DataInputStream dInput;
  public static final int DEFAULT_HSQL_PORT=9001;
  private void openHSQL(String user,String password)
  throws SQLException {
    // replace hsql:// with http:// because hsql is an 'unknown' protocol
    sConnect="http"+sDatabaseName.substring(4);
    if(sConnect.lastIndexOf(':')<6) {
      sConnect+=":"+DEFAULT_HSQL_PORT;
    }
    sUser=user;
    sPassword=password;
    reconnectHSQL();
  }
  private void reconnectHSQL() throws SQLException {
    try {
      URL url=new URL("http","localhost",DEFAULT_HSQL_PORT,"");
      url=new URL(url,sConnect);
      sSocket=new Socket(url.getHost(),url.getPort());
      sSocket.setTcpNoDelay(true);
      dOutput=new DataOutputStream(
          new BufferedOutputStream(sSocket.getOutputStream()));
      dInput=new DataInputStream(
          new BufferedInputStream(sSocket.getInputStream()));
      dOutput.writeUTF(sUser);
      dOutput.writeUTF(sPassword);
      dOutput.flush();
    } catch(Exception e) {
      throw Trace.error(Trace.CONNECTION_IS_BROKEN,e.getMessage());
    }
  }
  private synchronized jdbcResultSet executeHSQL(String s) throws SQLException {
    byte result[];
    try {
      dOutput.writeUTF(s);
      dOutput.flush();
      int len=dInput.readInt();
      result=new byte[len];
      int p=0;
      while(true) {
        int l=dInput.read(result,p,len);
        if(l==len) {
          break;
        } else {
          len-=l;
          p+=l;
        }
      }
    } catch(Exception e) {
      throw Trace.error(Trace.CONNECTION_IS_BROKEN,e.getMessage());
    }
    return new jdbcResultSet(new Result(result));
  }

  // Standalone
  private Database dDatabase;
  private Channel cChannel;
  private static Hashtable tDatabase=new Hashtable();
  private static Hashtable iUsageCount=new Hashtable();
  private synchronized void openStandalone(String user,String password)
  throws SQLException {
    dDatabase=(Database)tDatabase.get(sDatabaseName);
    int usage;
    if(dDatabase==null) {
      dDatabase=new Database(sDatabaseName);
      tDatabase.put(sDatabaseName,dDatabase);
      usage=1;
    } else {
      usage=1+((Integer)iUsageCount.get(sDatabaseName)).intValue();
    }
    iUsageCount.put(sDatabaseName,new Integer(usage));
    cChannel=dDatabase.connect(user,password);
  }
  public void finalize() {
    try {
      close();
    } catch(SQLException e) {
    }
  }
  private synchronized void closeStandalone() throws SQLException {
    Integer i=(Integer)iUsageCount.get(sDatabaseName);
    if(i==null) {
      // It was already closed - ignore it.
      return;
    }
    int usage=i.intValue()-1;
    if(usage==0) {
      iUsageCount.remove(sDatabaseName);
      tDatabase.remove(sDatabaseName);
      if(!dDatabase.isShutdown()) {
        execute("SHUTDOWN");
      }
      dDatabase=null;
      cChannel=null;
    } else {
      iUsageCount.put(sDatabaseName,new Integer(usage));
    }
  }
  private jdbcResultSet executeStandalone(String s) throws SQLException {
    return new jdbcResultSet(dDatabase.execute(s,cChannel));
  }
}

⌨️ 快捷键说明

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