Java Properties file-JDBC optimization programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.