我可以使用相同的JDBC连接,语句和结果集在JDBC中执行两个查询

Bas*_*sit 9 java jdbc

我正在验证用户

public static boolean login(DataManager dataManager, String userName, String password) {    
    boolean authenticated = false;      
    Connection connection = dataManager.getConnection();        
    if (connection != null) {           
         try {                           
             Statement s = connection.createStatement();
             String sql = "query";                 
             try {                   
                 ResultSet rs = s.executeQuery(sql);                                     
                 try {                      
                     while (rs.next()) {                             
                        String group_code = rs.getString(1);
                        String orgaunit = rs.getString(2);

                        authenticated = true;                            
                     } //end of while()                      
                 } finally {                     
                     rs.close();                     
                 }                   
             } finally {
                 s.close();                  
             }

         } catch(SQLException e) {               
             //System.out.println("Could not login from dataabse:" + e.getMessage());                
         } finally {             
             dataManager.putConnection(connection);                          
         }                  
    } //end of if (connection != null)      
    return authenticated;       
} //end of login()
Run Code Online (Sandbox Code Playgroud)

我正在关闭连接dataManager.putConnection(connection).我想问一旦用户登录然后我必须更新用户的状态并维护日志历史记录.我可以使用这样的东西吗?

try {
    Statement s = connection.createStatement();
    String sql = "query";                 
    try {                    
        ResultSet rs = s.executeQuery(sql);                                  
        try {
            while (rs.next()) {                          
                String group_code = rs.getString(1);                                                
                authenticated = true;                            
            } //end of while()  

            if (autherntcated == true) {
                sql = "query2(update status)"; 
                rs = s.executeQuery(sql);

                while (rs.next()) {
                    //dos tuff
                }

                sql = "anotherQuery";
                rs = s.executeQuery(sql);
                while (rs.next()) {
                    //do stuff
                }

            }

        } finally {                      
            rs.close();                      
        }                    
    } finally {
        s.close();                   
    }                
} catch(SQLException e) {
    //System.out.println("Could not login from dataabse:" + e.getMessage());
} finally {              
    dataManager.putConnection(connection);                           
}
Run Code Online (Sandbox Code Playgroud)

意味着使用相同的连接,相同的语句和相同的结果集执行其他查询或是错误的方法?

谢谢.

编辑------------------------------------------------- -------------

if (connection != null) {
    try {
        String sql = "query";
        PreparedStatement prepStatement = connection.prepareStatement(sql);
        try {
            ResultSet rs = prepStatement.executeQuery(sql);                 
            try {
                while (rs.next()) {
                    String group_code = rs.getString(1);
                    authenticated = true;
                } //end of while()
            } finally {                      
                rs.close();                      
            }
        } finally {
            prepStatement.close();                   
        }

        /// Addition   
        if (authenticated == true) {
            updateUser(connection, userName);
        }
    } catch(SQLException e) {
        //System.out.println("Could not login from dataabse:" + e.getMessage());
    } finally {
        dataManager.putConnection(connection);
    }
} //end of if (connection != null)
Run Code Online (Sandbox Code Playgroud)

更新方法:

private static void updateUser(Connection connection, String userName) {

    try {
        String sql = "UPDATE users SET status_code = 'A' WHERE login_id = '" + userName + "'";    
        PreparedStatement prepStatement = connection.prepareStatement(sql);
        try {
            int numberOfRowsUpdated = prepStatement.executeUpdate(sql);                 
        } finally {
            prepStatement.close();                   
        }

        maintainHistory(connection);

    } catch(SQLException e) {
    //System.out.println("Could not login from dataabse:" + e.getMessage());
    }

} //end of updateUser()
Run Code Online (Sandbox Code Playgroud)

maintainHistory:

private static void maintainHistory(Connection connection) {

    try {
        String sql = "INSERT INTO auditlog_user_logins(user_code,logintime,prstid) VALUES ();";
        PreparedStatement prepStatement = connection.prepareStatement(sql);          
        try {            
          int numberOfRowsUpdated = prepStatement.executeUpdate(sql);                   

       } finally {

        prepStatement.close();                   

       }

     } catch(SQLException e) {

         //System.out.println("Could not login from dataabse:" + e.getMessage());

     }

} //end of maintainHistory()
Run Code Online (Sandbox Code Playgroud)

use*_*421 5

我可以使用相同的 JDBC 连接、语句吗

是的。您可以在关闭之前重复使用它们。

和结果集

不。这个问题没有意义。结果集是执行查询或更新的结果。没有重新使用它的问题。我想您需要在执行下一个查询或更新之前关闭前面的结果集。


Suj*_*jay 2

我建议重用连接,因为每次尝试查询数据库时建立连接可能会产生性能开销。

至于语句,我建议改用PreparedStatement。它们不仅被缓存,而且是保护自己免受 SQL 注入的一种好方法。因此,请预先构建查询,执行它们,最后在完成后关闭它们。重用PreparedStatement意味着您将参数值替换为相同的PreparedStatement并执行相同的操作。

例如,如果您有一个像这样的PreparedStatement:

PreparedStatement preparedStatement = connection.prepareStatement("SELECT [col_names] from [table_name] where [col_1_value] = ? and [col_2_value] = ?")
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您只需用新值替换参数即可多次重用同一个PreparedStatement 。一般来说,对于您的情况,您将有多个PreparedStatement。由于这些语句被缓存,因此当您执行相同的语句时,它们不会对性能造成太大影响。

如果您需要的话,这里有一个很好的教程来向您介绍PreparedStatement:“使用准备好的语句”

结果集 - 好吧,我不知道如何重用它。使用完后将其关闭。根据Javadoc

当生成 ResultSet 对象的 Statement 对象被关闭、重新执行或用于从多个结果序列中检索下一个结果时,ResultSet 对象会自动关闭。

但是,使用完毕后请务必关闭连接。可重用性是一件好事,但资源管理不善则不然