Java connection to Oracle Database code sample via configuration file (Properties Class)

Source: Internet
Author: User

1 ImportJava.io.FileInputStream;2 Importjava.io.IOException;3 ImportJava.io.InputStream;4 Importjava.sql.Connection;5 ImportJava.sql.DriverManager;6 ImportJava.sql.ResultSet;7 Importjava.sql.SQLException;8 Importjava.sql.Statement;9 Importjava.util.Properties;Ten  One  Public classJdbcdemo { A     //To create an object association configuration file -     PrivateProperties Pro =NewProperties (); -     PrivateString driver; the     PrivateString URL; -     PrivateString user; -     PrivateString PSW; -      +      PublicJdbcdemo () { -         Try { +             //get data flow through reflection APro.load ( This. GetClass (). getResourceAsStream ("Myorcdb.properties")); at             //Get Profile Data -              This. Setdriver (Pro.getproperty ("Driver")); -              This. SetUrl (Pro.getproperty ("url")); -              This. SetUser (Pro.getproperty ("User")); -              This. SETPSW (Pro.getproperty ("PSW")); -}Catch(IOException e) { in e.printstacktrace (); -         } to     } +      -     Private voidMinsalname () { the          *         //connecting to a database $         Try {Panax Notoginseng Class.forName (driver); -             //Get Connections the Connection con; +Con =drivermanager.getconnection (URL,USER,PSW); A             //SQL statement Loading database theStatement st =con.createstatement (); +             //Read all departmental minimum wage lists -             //Close the database $String sql = "Select Emp.deptno,emp.ename,emp.sal from EMP," $+ "(select Deptno,min (SAL) minsal from emp GROUP by Deptno) a" -+ "Where Emp.deptno = A.deptno" -+ "and emp.sal = A.minsal"; the             //get the result set -ResultSet rst =st.executequery (SQL);Wuyi             //get the number of columns in the result set the             intn =rst.getmetadata (). getColumnCount (); -             //traversing result sets WuString str = ""; -              for(; Rst.next ();) { About                  for(inti=1;i<=n;i++){ $str + = rst.getstring (i) + "\ T"; -                 } -str + = "\ n"; -             } A System.out.println (str); +             //Turn off JDBC (open back off) the rst.close (); - st.close (); $ con.close (); the}Catch(ClassNotFoundException e) { the e.printstacktrace (); the         } the         Catch(SQLException e) { - e.printstacktrace (); in         } the     } the          PublicString Getdriver () { About         returndriver; the     } the      Public voidsetdriver (String driver) { the          This. Driver =driver; +     } -      PublicString GetUrl () { the         returnURL;Bayi     } the      Public voidseturl (String url) { the          This. url =URL; -     } -      PublicString GetUser () { the         returnuser; the     } the      Public voidsetUser (String user) { the          This. user =user; -     } the      PublicString getpsw () { the         returnPSW; the     }94      Public voidSETPSW (String psw) { the          This. PSW =PSW; the     } the      Public Static voidMain (string[] args) {98Jdbcdemo JDBC =NewJdbcdemo (); About jdbc.minsalname (); -     }101 102}

Java connection to Oracle Database code sample via configuration file (Properties Class)

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.