IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Java technology | Web development  >

Dynamic Web-based data access using JSP and JDBC technologies

developerWorks


Listing 2. JDBC code segment showing exception handling and resource cleanup
        
try
{
   Connection connection = DriverManager.getConnection(URL, user, password);
    try
    {
        Statement statement = connection.createStatement();
        logSQLWarnings(connection.getWarnings());
        connection.clearWarnings();
        try
        {
            ResultSet results = statement.executeQuery(sqlQuery);
            logSQLWarnings(statement.getWarnings());
            try
            {
                while (results.next())
                {
                    ... process query results ...
                    logSQLWarnings(results.getWarnings());
                }
            }
            catch (SQLException e)
            {
                logSQLExceptions(e);
            }
            finally
            {
                results.close();
            }
        }
        catch (SQLException e)
        {
            logSQLExceptions(e);
        }
        finally
        {
            statement.close();
        }
    }
    catch (SQLException e)
    {
        logSQLExceptions(e);
    }
    finally
    {
       connection.close();
    }
}
catch (SQLException e)
{
    logSQLExceptions(e);
}
      

Return to article

    关于 IBM 隐私条约 联系 IBM 使用条款