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

📄 jdbcconnection.java

📁 Java写的含有一个jdbc驱动的小型数据库数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    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">
     * HSQL 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">
     * HSQL 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">
     * HSQL 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">
     * HSQL 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

    /**
     * Constructor declaration
     *
     *
     * @param s
     * @param user
     * @param password
     */
    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);
	}
    }

    /**
     * Constructor declaration
     *
     *
     * @param c
     */
    jdbcConnection(Channel c) throws SQLException {
	iType = INTERNAL;
	cChannel = c;
	dDatabase = c.getDatabase();
    }

    /**
     * Method declaration
     *
     *
     * @param s
     *
     * @return
     *
     * @throws SQLException
     */
    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);
	}
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    boolean usesLocalFiles() {

	// Standalone connections do, HTTP connections not
	return iType != HTTP;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    String getName() {
	return sDatabaseName;
    }

    // HTTP
    private String      sConnect, sUser, sPassword;
    final static String ENCODING = "8859_1";

    /**
     * Method declaration
     *
     *
     * @param user
     * @param password
     *
     * @throws SQLException
     *
     * @see
     */
    private void openHTTP(String user, String password) throws SQLException {
	sConnect = sDatabaseName;
	sUser = user;
	sPassword = password;

	executeHTTP(" ");
    }

    /**
     * Method declaration
     *
     *
     * @param s
     *
     * @return
     *
     * @throws SQLException
     */
    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));
    }

    /**
     * Method declaration
     *
     *
     * @param user
     * @param password
     *
     * @throws SQLException
     */
    private void openHSQL(String user, String password) throws SQLException {

    sConnect=sDatabaseName.substring(7);
    sUser=user;
    sPassword=password;
    reconnectHSQL();

	sUser = user;
	sPassword = password;

	reconnectHSQL();
    }

    /**
     * Method declaration
     *
     *
     * @throws SQLException
     */
    private void reconnectHSQL() throws SQLException {
	try {
        StringTokenizer st = new StringTokenizer(sConnect, ":" );
        String host = st.hasMoreTokens() ? st.nextToken() : "";
        int port = st.hasMoreTokens() ? Integer.parseInt( st.nextToken() ) : DEFAULT_HSQL_PORT;
        sSocket = new Socket( host, port );

	    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());
	}
    }

    /**
     * Method declaration
     *
     *
     * @param s
     *
     * @return
     *
     * @throws SQLException
     */
    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));
    }

    /**
     * Method declaration
     *
     *
     * @param user
     * @param password
     *
     * @throws SQLException
     */
    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);
    }

    /**
     * Method declaration
     *
     */
    public void finalize() {
	try {
	    close();
	} catch (SQLException e) {}
    }

    /**
     * Method declaration
     *
     *
     * @throws SQLException
     */
    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));
	    execute("DISCONNECT");
	}
    }

    /**
     * Method declaration
     *
     *
     * @param s
     *
     * @return
     *
     * @throws SQLException
     */
    private jdbcResultSet executeStandalone(String s) throws SQLException {
	return new jdbcResultSet(dDatabase.execute(s, cChannel));
    }

}

⌨️ 快捷键说明

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