Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.sql.*;Importjava.util.ArrayList;Importjava.util.List;Importjava.util.Properties; Public classDaohelper {/*** * * @paramargs*/ Public Static voidMain (String []args) {Daohelper de=NewDaohelper (); String SQL= "SELECT * from Users"; List<Object[]> list=de.querylist (SQL,NULL); for(intI=0;i<list.size (); i++) {Object []obj=List.get (i); System.out.println (obj[1].tostring () + "" +obj[2].tostring ());}}//Oracle Connection Property SettingsPrivate StaticString driver;Private StaticString URL;Private StaticString user;Private StaticString passwd;//connection classes and InterfacesConnection CT; PreparedStatement PS; ResultSet rs;//Read Properties file Initialize database connection variableStatic{Properties pp=NewProperties (); InputStream fis= Daohelper.class. getClassLoader (). getResourceAsStream ("Derby.properties");Try{pp.load (FIS);d River=pp.getproperty ("Driver"); URL=pp.getproperty ("url"); user=pp.getproperty ("username");p asswd=pp.getproperty ("Password");} Catch(IOException e) {//TODO auto-generated Catch blocke.printstacktrace ();}finally{if(pp!=NULL) pp.clear ();Try {if(fis!=NULL) Fis.close ();} Catch(IOException e) {//TODO auto-generated Catch blocke.printstacktrace (); }}} PublicDaohelper () {Try{class.forname (driver); CT=drivermanager.getconnection (URL,USER,PASSWD); } Catch(Exception e) {//TODO auto-generated Catch blocke.printstacktrace ();}} Public intUpdate (String sql,string []paras) {intCount=0;Try{PS=ct.preparestatement (SQL);//Injection Parameters This. Setparameters (paras);//gets the number of updated data rows. Count=ps.executeupdate (); } Catch(SQLException e) {//TODO auto-generated Catch blocke.printstacktrace ();}finally{ This. Close ();}returncount;}/*** @paramsql*@paramparas No injection parameters can be null or empty string. * @returnreturns a resultset result set. */ PublicResultSet query (String sql,string[] paras) {Try{PS=ct.preparestatement (SQL); This. Setparameters (paras); Rs=ps.executequery ();} Catch(SQLException e) {//TODO auto-generated Catch blocke.printstacktrace ();}finally{//not off. }returnrs; }/*** @paramsql*@paramparas No injection parameters can be null or empty string. * @returnreturns a list containing an array of objects. */ Publiclist querylist (String sql,string []paras) {list<Object[]> list=NewArraylist<object[]>();Try{PS=ct.preparestatement (SQL); This. Setparameters (paras); Rs=Ps.executequery (); ResultSetMetaData RSMD=(ResultSetMetaData) rs.getmetadata ();intCount=Rsmd.getcolumncount ();//Place the query results in an array of objects. while(Rs.next ()) {intRows=Rs.getrow (); Object obj[]=NewObject[count]; for(intj=0;j<count;j++) {Obj[j]=rs.getstring (j+1);} List.add (obj);}} Catch(SQLException e) {//TODO auto-generated Catch blocke.printstacktrace ();}finally{ This. Close ();}returnlist;} Public voidClose () {Try {if(rs!=NULL) Rs.close ();if(ps!=NULL) Ps.close ();if(ct!=NULL) Ct.close ();} Catch(SQLException e) {//TODO auto-generated Catch blocke.printstacktrace ();}}Private voidSetparameters (String[] paras)throwsSQLException {if(paras!=NULL&&!paras.equals ("")){ for(inti=0;i<paras.length;i++) {ps.setstring (i+1, Paras[i]); }}}Properties File Sql.properties
Username=Sapassword=bbc223344url=jdbc\:sqlserver\://192.168.1.109\:142; Databasename\=qqdbDriver=com.microsoft.sqlserver.jdbc.sqlserverdriver
Derby.properties
Username=classiccarspassword=classiccarsurl=jdbc\:d erby\://localhost\:1527/myeclipsedriver= Org.apache.derby.jdbc.ClientDriver
Mysql.properties
Username=rootpassword=1234url=jdbc\:mysql\://192.168.1.114\:3306/testdriver=com.mysql.jdbc.driver
Oracle.properties
Username=scottpassword=1234url=jdbc\:oracle\:thin\:@192.168.1.114\:1521\:oracle1driver= Oracle.jdbc.driver.OracleDriver
PS: IP address, user name, and password in the properties file need to be modified as appropriate
Java calls a database through a property file