Test time spent connecting to an Oracle database

Source: Internet
Author: User

Maven Project

Main program: Connoracle.java

1  Packageorg.guangsoft.oracle;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.SQLException;6 7 ImportOrg.guangsoft.util.PropUtil;8 9  Public classconnoracle {Ten     Private Static FinalString Driverclass = Proputil.getprops (). GetProperty ("Driverclass");  One     Private Static FinalString URL = proputil.getprops (). GetProperty ("url");  A     Private Static FinalString USERNAME = Proputil.getprops (). GetProperty ("USERNAME");  -     Private Static FinalString PASSWORD = Proputil.getprops (). GetProperty ("PASSWORD");  -     Private StaticConnection conn =NULL;  the    -      Public Static voidMain (string[] args) { -System.out.println ("# # #DRIVERCLASS is" +driverclass); -System.out.println ("# # #URL is" +URL); +System.out.println ("# # #USERNAME is" +USERNAME); -System.out.println ("# # #PASSWORD is" +PASSWORD); +         Try {   A Class.forName (Driverclass);  at}Catch(ClassNotFoundException e) { -System.out.println ("# # # #Class. forname failed!"); - E.printstacktrace ();  -         }   -    -         Try {   in             LongStartTime =System.currenttimemillis (); -conn =drivermanager.getconnection (URL, USERNAME, PASSWORD);  to             LongEndTime =System.currenttimemillis (); +             if(conn = =NULL) {   -SYSTEM.OUT.PRINTLN ("Conn is null to get link failed! ");  the}Else {   *SYSTEM.OUT.PRINTLN ("conn--" +conn);  $ System.out.println (Conn.hashcode ());Panax NotoginsengSYSTEM.OUT.PRINTLN ("Get Connection Time:" + (Endtime-starttime) + "MS"); -             }   the    +}Catch(SQLException e) { ASystem.out.println ("# # # #DriverManager. getconnection failed!"); the E.printstacktrace ();  +}finally {   -             Try {   $ Conn.close ();  $}Catch(Exception e) { - E.printstacktrace ();  -             }   the         }   -     }  Wuyi}

Proputil.java

 PackageOrg.guangsoft.util;ImportJava.io.InputStream;Importjava.util.Properties; Public classProputil {Private StaticProperties props =NULL; Static{readproperties ("Db.properties"); }        Private Static voidreadproperties (String fileName) {Try{Props=NewProperties (); InputStream InputStream=Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream (FileName);            Props.load (InputStream);        Inputstream.close (); } Catch(Exception e) {e.printstacktrace (); }        }          Public StaticProperties GetProps () {returnprops; }    }

Db.properties

driverclass=oracle.jdbc.driver.OracleDriver

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

USERNAME=Test

PASSWORD=test

Pom.xml

1 <Projectxmlns= "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>Ten             <groupId>Com.oracle</groupId> One             <Artifactid>Ojdbc5</Artifactid> A             <version>11.2</version> -         </Dependency> -     </Dependencies> the     <Build> -         <sourcedirectory>Src</sourcedirectory> -         <Resources> -             <Resource> +                 <Directory>Src</Directory> -                 <excludes> +                     <Exclude>**/*.java</Exclude> A                 </excludes> at             </Resource> -         </Resources> -         <Plugins> -             <plugin> -                 <Artifactid>Maven-compiler-plugin</Artifactid> -                 <version>2.3.2</version> in                 <Configuration> -                     <Source>1.5</Source> to                     <Target>1.5</Target> +                 </Configuration> -             </plugin> the         </Plugins> *     </Build> $ </Project>

Test time spent connecting to an Oracle database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.