测试连接oracle数据库耗时

Posted 光何

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试连接oracle数据库耗时相关的知识,希望对你有一定的参考价值。

maven项目

主程序:ConnOracle.java

 1 package org.guangsoft.oracle;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 
 7 import org.guangsoft.util.PropUtil;
 8 
 9 public class ConnOracle {
10     private static final String DRIVERCLASS = PropUtil.getProps().getProperty("DRIVERCLASS");  
11     private static final String URL = PropUtil.getProps().getProperty("URL");  
12     private static final String USERNAME = PropUtil.getProps().getProperty("USERNAME");  
13     private static final String PASSWORD = PropUtil.getProps().getProperty("PASSWORD");  
14     private static Connection conn = null;  
15   
16     public static void main(String[] args) {  
17         System.out.println("####DRIVERCLASS is " + DRIVERCLASS);
18         System.out.println("####URL is " + URL);
19         System.out.println("####USERNAME is " + USERNAME);
20         System.out.println("####PASSWORD is " + PASSWORD);
21         try {  
22             Class.forName(DRIVERCLASS);  
23         } catch (ClassNotFoundException e) {
24             System.out.println("####Class.forName failed!");
25             e.printStackTrace();  
26         }  
27   
28         try {  
29             long startTime = System.currentTimeMillis();
30             conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);  
31             long endTime = System.currentTimeMillis();
32             if (conn == null) {  
33                 System.out.println("conn is null 获取链接失败!");  
34             } else {  
35                 System.out.println("conn --> " + conn);  
36                 System.out.println(conn.hashCode());
37                 System.out.println("获取连接耗时:" + (endTime - startTime) + " ms");
38             }  
39   
40         } catch (SQLException e) {
41             System.out.println("####DriverManager.getConnection failed!");
42             e.printStackTrace();  
43         } finally {  
44             try {  
45                 conn.close();  
46             } catch (Exception e) {  
47                 e.printStackTrace();  
48             }  
49         }  
50     }  
51 }  

PropUtil.java

package org.guangsoft.util;

import java.io.InputStream;
import java.util.Properties;

public class PropUtil {

    private static Properties props = null;
    
    static {
        readProperties("db.properties");
    }
    
    private static void readProperties(String fileName) {    
        try {    
            props = new Properties();
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            props.load(inputStream);
            inputStream.close();
        } catch (Exception e) {    
            e.printStackTrace();    
        }    
    } 
    
    public static Properties getProps() {
        return props;
    }
    
}

db.properties

 DRIVERCLASS=oracle.jdbc.driver.OracleDriver

URL=jdbc:oracle:thin:@localhost:1521:orcl

USERNAME=test

PASSWORD=test  

pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>org.guangsoft</groupId>
 5     <artifactId>oracle</artifactId>
 6     <version>1.0</version>
 7     <packaging>jar</packaging>
 8     <dependencies>
 9         <dependency>
10             <groupId>com.oracle</groupId>
11             <artifactId>ojdbc5</artifactId>
12             <version>11.2</version>
13         </dependency>
14     </dependencies>
15     <build>
16         <sourceDirectory>src</sourceDirectory>
17         <resources>
18             <resource>
19                 <directory>src</directory>
20                 <excludes>
21                     <exclude>**/*.java</exclude>
22                 </excludes>
23             </resource>
24         </resources>
25         <plugins>
26             <plugin>
27                 <artifactId>maven-compiler-plugin</artifactId>
28                 <version>2.3.2</version>
29                 <configuration>
30                     <source>1.5</source>
31                     <target>1.5</target>
32                 </configuration>
33             </plugin>
34         </plugins>
35     </build>
36 </project>

 

以上是关于测试连接oracle数据库耗时的主要内容,如果未能解决你的问题,请参考以下文章

稳定性 耗时 监控原因分析-- dubbo rpc 框架 的线程池,io 连接模型. 客户端,服务端

如何使用 Java 测试与 Oracle 数据库的连接

oracle 第二个数据库实例,用plsql不能连接

oracle 高效分页查询SQL

如何在 Toad for Oracle 中使用自定义代码片段?

Oracle 如何写出高效的 SQL