jtjdbcadapter.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 869 行 · 第 1/2 页

JAVA
869
字号
        }

        if (query == null)
            return (null);

        try {
            pst = connection.prepareStatement (query);
        } 
        catch (Exception ex) {
            handleException (ex);
        }
        return (pst);    
    }


    private Object executePreparedUpdate (PreparedStatement pst )
    {
        int cnt = 0;

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

        if (connection == null) {
            handleError ("JtJDBCAdapter.prepareStatement: invalid connection (null)");
            return (null);
        }
        try {
            cnt = pst.executeUpdate ();
            return (new Integer (cnt));
        } catch (Exception ex) {
            handleException (ex);
            return (ex); // check todo
        }

    }
    

    
    
    private Object executePreparedQuery (PreparedStatement pst )
    {
        int cnt = 0;
        ResultSet res;

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

        if (connection == null) {
            handleError ("JtJDBCAdapter.prepareStatement: invalid connection (null)");
            return (null);
        }       
        
        try {
            res = pst.executeQuery ();
            return (res);
        } catch (Exception ex) {
            handleException (ex);
            return (null);
        }

    }

    /**
     * Begin transaction
     */
    private void beginTransaction () {

        if (connection == null) {
            handleWarning ("beginTransaction: connection is null");
            return;
        }

        try {
            connection.setAutoCommit(false);
        } catch (Exception ex) {
            handleException (ex);            
        }


    }

    /**
     *  Commit transaction
     */

    private void commitTransaction () {

        try {
            connection.commit();
            connection.setAutoCommit(true);
        } catch (Exception ex) {
            handleException (ex);
        }
    }

    /**
     *  Rollback transaction
     */

    private void rollbackTransaction () {

        try {
            connection.rollback();
            connection.setAutoCommit(true);
        } catch (Exception ex) {
            handleException (ex);
        }
    }


    /**
     * Process object messages.
     * <ul>
     * <li> JtCONNECT - Establishes a database connection using the appropriate object attributes 
     * (datasource or database driver, url and user/password if any).
     * <li> JtEXECUTE_QUERY - Executes an SQL statement. Typically this is an SQL SELECT statement.
     * Returns a ResultSet object. The message contains the query to be executed (msgContent).
     * <li> JtEXECUTE_UPDATE - Executes an SQL statement.The SQL statement must be an INSERT, UPDATE, DELETE; or an SQL statement that returns
     * no value as a DDL statemet. Returns the row count (Integer) for INSERT, UPDATE  or DELETE statements, or 0 for SQL statements 
     * that return nothing. The message contains the query to be executed (msgContent).
     * <li> JtPREPARE_STATEMENT - Returns a prepared statement. The message contains the query (msgContent).
     * <li> JtEXECUTE_PREPARED_QUERY - Executes a prepared statement a returns the ResultSet. The message 
     * (msgContent) contains the prepared statement to be executed. 
     * <li> JtEXECUTE_PREPARED_UPDATE - Executes a prepared statement. The message contains the prepared SQL statement
     * to be executed. The SQL statement must be an INSERT, UPDATE, DELETE; or an SQL statement that returns
     * no value as a DDL statemet. Returns the row count (Integer) for INSERT, UPDATE  or DELETE statements, or 0 for SQL statements 
     * that return nothing.
     * <li> JtBEGIN_TRANSACTION - Starts a transaction. Sets the autoCommit attribute of the connection (false value).
     * <li> JtCOMMIT - Commits the transaction. Resets the autocommit attribute (true value).
     * <li> JtROLLBACK - Rollbacks the transaction. Resets the autocommit attribute (false value).   
     * <li> JtCLOSE - Closes the database connection.
     * </ul>
     */

    public Object processMessage (Object message) {
        //String content;
        String query;
        JtMessage e = (JtMessage) message;
        Object reply;

        if (e == null ||  (e.getMsgId() == null))
            return (null);

        if (!initted) {
            initial ();
            initted = true;        
        }

        // establish connection
        if (e.getMsgId().equals(JtJDBCAdapter.JtCONNECT)) {
            connect ();
            return (connection);
        }

        // execute a query
        if (e.getMsgId().equals(JtJDBCAdapter.JtQUERY)) {
            connect ();
            query = (String) e.getMsgContent();
            show_output (execute_query (query));
            //if (dataSource != null)
            //  close ();

            return (null);
        }

        if (e.getMsgId().equals(JtJDBCAdapter.JtEXECUTE_QUERY)) {
            query = (String) e.getMsgContent();
            connect ();
            reply = execute_query (query);
            // When dealing with DataSources Close the connection after each operation
            //if (dataSource != null)
            //  close ();
            return (reply);

        }


        if (e.getMsgId().equals(JtJDBCAdapter.JtPREPARE_STATEMENT)) {
            query = (String) e.getMsgContent();
            connect ();
            reply = prepareStatement (query);
            //if (dataSource != null)
            //  close ();
            return (reply);
        }


        if (e.getMsgId().equals(JtJDBCAdapter.JtEXECUTE_PREPARED_UPDATE)) {
            //query = (String) e.getMsgContent();
            connect ();
            reply = executePreparedUpdate 
            ((PreparedStatement) e.getMsgContent());
            //if (dataSource != null)
            //  close ();
            return (reply);
        }
        
        if (e.getMsgId().equals(JtJDBCAdapter.JtEXECUTE_PREPARED_QUERY)) {
            //query = (String) e.getMsgContent();
            connect ();
            reply = executePreparedQuery 
            ((PreparedStatement) e.getMsgContent());
            //if (dataSource != null)
            //  close ();
            return (reply);
        }

        // execute an Update
        if (e.getMsgId().equals(JtJDBCAdapter.JtUPDATE) || e.getMsgId().equals(JtJDBCAdapter.JtEXECUTE_UPDATE)) {
            query = (String) e.getMsgContent();
            connect ();
            reply = execute_update (query);
            //if (dataSource != null)
            //  close ();
            return (reply);
        }

        // map query onto object list
        if (e.getMsgId().equals("JtMAP")) {
            query = (String) e.getMsgContent();
            if (query == null)
                return (null);
            connect ();
            map_query (query, e);
            //if (dataSource != null)
            //  close ();
            return (null);
        }

        // close a connection
        if (e.getMsgId().equals(JtJDBCAdapter.JtCLOSE) || e.getMsgId().equals (JtObject.JtREMOVE)) {
            close ();
            return (null);
        }

        if (e.getMsgId().equals (JtJDBCAdapter.JtBEGIN_TRANSACTION)) {
            connect ();
            beginTransaction ();
            return (this);
        }

        if (e.getMsgId().equals (JtJDBCAdapter.JtCOMMIT)) {
            commitTransaction ();
            return (this);
        }

        if (e.getMsgId().equals (JtJDBCAdapter.JtROLLBACK)) {
            rollbackTransaction ();
            return (this);
        }

        return (super.processMessage(message));

    }

    /**
     * Specifies the database user.
     * @param newUser user
     */

    public void setUser (String newUser) {
        user = newUser;
    }


    /**
     * Returns the database user.
     */

    public String getUser () {
        return (user);
    }


    /**
     * Specifies the database password.
     * @param newPassword password
     */

    public void setPassword (String newPassword) {
        password = newPassword;
    }


    /**
     * Returns the database password.
     */

    public String getPassword () {
        return (password);
    }


    /**
     * Specifies the URL of the database.
     * @param newUrl url
     */

    public void setUrl (String newUrl) {
        url = newUrl;
    }

    /**
     * Returns the URL of the database.
     */

    public String getUrl () {
        return (url);
    }


    /**
     * Specifies the Datasource logical name (if any).
     * @param datasource Datasource logical name
     */

    public void setDatasource (String datasource) {
        this.datasource = datasource;
    }

    /**
     * Returns the Datasource logical name (JNDI).
     */

    public String getDatasource () {
        return (datasource);
    }


    /**
     * Specifies the database driver.
     * @param newDriver driver
     */

    public void setDriver (String newDriver) {
        driver = newDriver;
    }

    /**
     * Returns the database driver.
     */
    public String getDriver () {
        return (driver);
    }

    /**
     * Method being deprecated.
     */

    public void setBase (String newBase) {
        base = newBase;
    }

    /**
     * Method being deprecated.
     */

    public String getBase () {
        return (base);
    }


    /**
     * Specifies the database Connection.
     */

    public void setConnection (Connection newConnection) {
        connection = newConnection;
    }

    /**
     * Returns the database Connection.
     */

    public Connection getConnection () {
        return (connection);
    }

    /**
     * Demonstrates the use of the JtJDBCAdapter.
     */

    private static void test () {
        JtFactory factory = new JtFactory ();  // JtFactory
        JtMessage msg = new JtMessage (JtJDBCAdapter.JtQUERY);
        JtJDBCAdapter adapter;

        // Create an instance of JtJDBCAdapter

        adapter = (JtJDBCAdapter) factory.createObject (JtJDBCAdapter.JtCLASS_NAME, "adapter");

        // Execute a query

        msg.setMsgContent ("select email from roster where email='member@hotmail.com';");        
        factory.sendMessage (adapter, msg);        

        // Illustrates the use of transactions. Begin a transaction

        factory.sendMessage ("adapter", new JtMessage (JtJDBCAdapter.JtBEGIN_TRANSACTION));

        // Execute the query

        msg = new JtMessage (JtJDBCAdapter.JtEXECUTE_UPDATE);    
        msg.setMsgContent("update roster set email_flag=1;");
        factory.sendMessage (adapter, msg);

        // Commit the transaction

        factory.sendMessage (adapter, new JtMessage (JtJDBCAdapter.JtCOMMIT));


        factory.removeObject (adapter);

    }

    /**
     * Unit tests the messages processed by JtJDBCAdapter. The following attributes should be
     * included in the Jt resource file:
     * <ul>
     * <li>Jt.JtJDBCAdapter.user
     * <li>Jt.JtJDBCAdapter.password
     * <li>Jt.JtJDBCAdapter.driver
     * <li>Jt.JtJDBCAdapter.url
     * <li>Jt.JtJDBCAdapter.datasource (if this attribute is present, the previous
     * attributes are not needed)
     * </ul>
     */

    public static void main (String[] args) {
        test ();
    }
}

⌨️ 快捷键说明

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