The code is as follows:
Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.sql.Connection;Importjava.sql.SQLException;Importjava.util.Properties;ImportOrg.apache.commons.dbcp.BasicDataSource; Public classDbutil {Private StaticBasicdatasource ds; Static{Properties prop=NewProperties (); Try{prop.load (dbutil).class. getClassLoader (). getResourceAsStream ("config.properties"));//the corresponding file should be in the class directoryString className = Prop.getproperty ("ClassName"); String URL= Prop.getproperty ("url"); String Password= Prop.getproperty ("Password"); String username= Prop.getproperty ("username"); DS=NewBasicdatasource (); Ds.setdriverclassname (ClassName); Ds.seturl (URL); Ds.setusername (username); Ds.setpassword (password); System.out.println ("The connection was successful!" "); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } Public StaticConnection getconnection ()throwsSQLException {returnds.getconnection (); } Public Static voidCloseConnection (Connection conn) {Try{conn.commit (); Conn.close (); } Catch(SQLException e) {e.printstacktrace (); } }}
Connect Database Tools Class Dbutil