The configuration file can reduce our workload and bring convenience.
Build Properties File
1. First, create a new dbconfig.properties. Then add the following code:
driver=com.mysql.jdbc.Driverdburl=jdbc:mysql://localhost:3306/jsp_dbbuser=rootpassword=
Read the Float Properties file
Create a new properties class that lets the instance of this class load method read into the input stream of the configuration file.
Gets the input stream that is used by the ConnectionFactory.class.getClassLoader (). getResourceAsStream method.
Finally, we use the GetProperties method of the properties instance to get the corresponding property value.
The specific code is as follows:
Private StaticString driver;Private StaticString Dburl;Private StaticString name;Private StaticString password;Static{Properties prop =NewProperties ();Try{InputStreaminch= ConnectionFactory.class.getClassLoader (). getResourceAsStream ("Dbconfig.properties"); Prop.load (inch); }Catch(Exception e) {System. out. println ("======== configuration file Read error ==========="); } Driver = Prop.getproperty ("Driver"); Dburl = Prop.getproperty ("Dburl"); Name = Prop.getproperty ("Name"); Password = Prop.getproperty ("Password"); }
Java Properties file-JDBC optimization programming