简体   繁体   English

优化以下代码,以免花费更多时间不执行查询

[英]optimize the below code such that not to execute the query if take more time

I have the below code which is a simple jdbc java program which fetch the results upon execution of the query and retrieve the results and further store them in a resultset 我有以下代码,它是一个简单的jdbc Java程序,它在执行查询时获取结果并检索结果,并将其进一步存储在结果集中

The Statement.execute() method allows us to execute any kind of query like select, update. Statement.execute()方法使我们能够执行任何类型的查询,例如选择,更新。 It returns boolean. 它返回布尔值。 If the return value is true, then it executed select query, get the ResultSet object and read the resulted records. 如果返回值为true,则执行select查询,获取ResultSet对象并读取结果记录。 If it returns false, then it can be update query, call getUpdateCount() method to get total records updated 如果返回false,则可以进行更新查询,调用getUpdateCount()方法获取更新的总记录

public static void main(String a[]){

        Connection con = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.
                    getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
                        ,"user","password");
            Statement stmt = con.createStatement();
            //The query can be update query or can be select query
            String query = "select * from emp";
            boolean status = stmt.execute(query);
            if(status){
                //query is a select query.
                ResultSet rs = stmt.getResultSet();
                while(rs.next()){
                    System.out.println(rs.getString(1));
                }
                rs.close();
            } else {
                //query can be update or any query apart from select query
                int count = stmt.getUpdateCount();
                System.out.println("Total records updated: "+count);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            try{
                if(con != null) con.close();
            } catch (Exception ex){}
        }
    }

Now my query is to I have to optimize this program in such a way like 现在我的查询是我必须以这种方式优化该程序

1)It should print the time taken by query to execute at runtime 1)它应该打印查询在运行时执行所花费的时间

2) need to add the condition in such a way ...if the query takes more than 1 min then it should stop at that time means some counter should be there which will keep track of the time that it should not be more than 1 min 2)需要以这种方式添加条件...如果查询花费了超过1分钟的时间,那么它应该在那个时候停止,这意味着应该有一个计数器来跟踪它应该不超过1的时间分

Please advise how to achieve 请告知如何实现

  • Log System.currentTimeMillis() (which returns the current time in milliseconds) as the last thing before executing your query. 在执行查询之前,将System.currentTimeMillis() (返回当前时间,以毫秒为单位)记录为最后一件事。 Subtract as soon as the execute() returns. execute()返回时立即减去。
  • And, use Statement.setQueryTimeout(int seconds) to specify your timeout value as 并且,使用Statement.setQueryTimeout(int seconds)将超时值指定为

     stmt.setQueryTimeout(60); long start = System.currentTimeMillis(); boolean status = stmt.execute(query); System.out.println("Took " + (start - System.currentTimeMillis()) + " ms"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM