测试连接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>
原文地址:https://www.cnblogs.com/guanghe/p/9067982.html