jdbc两种不同的实现

来源:互联网 发布:windows时间同步超时 编辑:程序博客网 时间:2024/05/16 13:41

//第一种,比较简单,对于任何的exception没做任何处理

Connection connection = DriverManager.getConnection(URL, user, password);

Statement statement = connection.createStatement();

ResultSet results = statement.executeQuery(sqlQuery);

while (results.next()){

       ... process query results ...

      logSQLWarnings(results.getWarnings());

}

results.close();

statement.close();

connection.close();

//第二种,把exception写到log日志里,推荐!

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

 

原创粉丝点击