First, file location
Properties File Contents:
driver=com.mysql.jdbc.Driverurl=jdbc\:mysql\://localhost\:3306/qqonlineuser= Rootpassword=
Second, the Code
It is important to note that the path to the Java project is web-inf\classes\config.properties for the Src\config.properties,java web. When deployed, Tomcat compiles config.properties into the web-inf\classes\config.properties directory.
So if you still use the Java engineering path under Java Web Engineering, you will not get the parameters.
Public classDbutil {Private StaticString Driver = ""; Private StaticString url = ""; Private StaticString user = ""; Private StaticString Password = ""; Private Static BooleanFlag =false; /*** Load JDBC Connection database parameters*/ Public voidGetparm () {Properties P=NewProperties (); Try{InputStream in= This. GetClass (). getClassLoader (). getResourceAsStream ("Config.properties"); P.load (in); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Driver= P.getproperty ("Driver"); URL= P.getproperty ("url"); User= P.getproperty ("User"); Password= P.getproperty ("Password"); Flag=true; } Public StaticConnection getconnection () {if(!flag) {//every time I connect, I look at the load.Dbutil db =NewDbutil (); Db.getparm (); }}
Java Read the properties file