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)