获取连接时间的最精确方法JDBC

The most precision way to get connection time JDBC

public class Sql {
    private static Connection con=null;
    private static Statement stat=null;

    public void createConnection(){
        try {
            long a = System.currentTimeMillis();
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","");
            stat = con.createStatement();
            //result in milliseconds
            System.out.println(System.currentTimeMillis() - a); 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是一段代码,为我们提供了与本地主机源的连接。我在那里创建了方法,该方法给出了使用 System.currentTimeMillis() 进行连接所花费的时间的结果。有没有其他方法可以获得更高的精度值?

你可以使用 System.nanoTime() 但我记得在某处读到它不能保证你的结果是一致的

编辑:来自我链接的文档:

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis().

试用监控工具。这将极大地帮助您进一步开发应用程序,而不是在所有 JDBC 调用中结束系统打印。

http://www.jdbmonitor.com/