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

📄 connectionutils.java

📁 根据Scott W. Ambler在1998年写的关于ORM Persistence Layer的详细设计论文的设计思路,Artem Rudoy 开发了一个开源的ORM实现 -- PL(Persist
💻 JAVA
字号:
package pl.sql;

public class ConnectionUtils
{
    /**
     * Commit the transaction associated with the connection connection.
     */
    public static void commit(java.sql.Connection connection) throws java.sql.SQLException
    {
        if (connection.getMetaData().supportsTransactions())
            connection.commit();
    }

    /**
     * Processe the specified SQL statement.
     *
     * @return java.sql.ResultSet
     * @param statement the statement to process
     */
    public static java.sql.ResultSet processQuery(pl.sql.SqlStatement statement, java.sql.Connection connection) throws pl.PlException
    {
        try
        {
            // Print statement's SQL string if the Persistence Layer is in the debug mode
            if(pl.PersistenceBroker.getInstance().isDebug())
            {
                System.out.println(statement.toString());
            }

            java.sql.ResultSet rs = null;
            java.sql.PreparedStatement pst = connection.prepareStatement(statement.getSqlString());
            for (int i = 0; i < statement.getSize(); i++)
            {
                SqlStatement.Parameter parameter = statement.getParameter(i);
                PlTypes.setParameter(pst, parameter.getValue(), parameter.getPlType(), i + 1);
            }
            //System.out.println("  ***processQuery pst sql ="+pst.toString());
            rs = pst.executeQuery();
            pst.clearParameters();
            statement.setStatement(pst);
            return rs;
        }
        catch(java.sql.SQLException e)
        {
            throw pl.PlException.toPlException(e);
        }
    }

    /**
     * Processe the specified SQL statement.
     *
     * @return java.sql.ResultSet
     * @param statement the statement to process
     * 保存对象
     */
    public static int processUpdate(pl.sql.SqlStatement statement, java.sql.Connection connection) throws pl.PlException
    {
        try
        {
            // Print statement's SQL string if the Persistence Layer is in the debug mode
            if(pl.PersistenceBroker.getInstance().isDebug())
            {
                System.out.println(statement.toString());
            }

            int result = 0;
            java.sql.PreparedStatement pst = connection.prepareStatement(statement.getSqlString());
            for (int i = 0; i < statement.getSize(); i++)
            {
                SqlStatement.Parameter parameter = statement.getParameter(i);
                PlTypes.setParameter(pst, parameter.getValue(), parameter.getPlType(), i + 1);
            }
//            System.out.println("  ***processUpdate pst sql ="+pst.toString());
            result = pst.executeUpdate();
            pst.clearParameters();
            statement.setStatement(pst);
            return result;
        }
        catch(java.sql.SQLException e)
        {
            throw pl.PlException.toPlException(e);
        }
    }

    /**
     * Rollback the transaction associated with this connection.
     */
    public void rollback(java.sql.Connection connection) throws java.sql.SQLException
    {
        if(connection.getMetaData().supportsTransactions())
            connection.rollback();
    }

    /**
     * Set connection auto-commit mode if transactions are supported.
     *
     * @param autoCommit boolean
     */
    public static void setAutoCommit(boolean autoCommit, java.sql.Connection connection) throws java.sql.SQLException
    {
        if(connection.getMetaData().supportsTransactions())
            connection.setAutoCommit(autoCommit);
    }
}

⌨️ 快捷键说明

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