Actions for using the properties class and configuration files in JDBC

Source: Internet
Author: User
Tags list of attributes

Also posted in: 811,497,101, properties configuration file

The 4 parameters (driver, URL, user name, password) that are connected in the development usually exist in the configuration file, convenient for later maintenance, if the program needs to replace the database, only need to modify the configuration file.

Typically, we are accustomed to using the properties file, Java has the class Properties (Java.util.Properties) that are specifically used to load the configuration file, which is used to store some values that may be modified in the program, as long as the configuration file is modified without modifying the program itself. The Java configuration file is typically a. properties file, formatted as a text file, and the format of the contents of the file is the "key = value" format. In the properties file, adding # at the beginning of the line is a comment, or a <!----> including this line is also a comment.


  
    
   
  1. #例如
  2. driverClass=com.mysql.jdbc.Driver
  3. url=jdbc:mysql://localhost:3306/mydatabase
  4. username=root
  5. password=7894561230
  6. <!--注释-->

With MyEclipse Open is this look, here I saved in the SRC directory, named Database.properties

PS: A set of data in a row, formatted as "Key=value".

A) key is named Custom, if it is more than one word, it is customary to use dot separation. Example: Jdbc.driver

b) value does not support Chinese, Unicode conversion If you need to use non-English characters

Second, load the configuration file

   
  
  1. Public static void loadpro() throws Exception {
  2. Reading the input stream from a file
  3. FileInputStream FIS = new fileinputstream ("src/database.properties");
  4. Create a Properties Object
  5. Properties Pro = New properties ();
  6. Loading data from the stream
  7. Pro.load (FIS);
  8. Close the stream
  9. Fis.close ();
  10. Reads a value from the properties object according to the key
  11. String Driverclass = Pro.getproperty ("Driverclass");
  12. String url = pro.getproperty ("url");
  13. String username = pro.getproperty ("username");
  14. String password = pro.getproperty ("password");
  15. Print value
  16. System.out.println (Driverclass);
  17. System.out.println (URL);
  18. SYSTEM.OUT.PRINTLN (username);
  19. SYSTEM.OUT.PRINTLN (password);
  20. }

1. read files via IO

2. Create a Properties Object

3. Load data using the Properties object's load (stream) method

4. Use the GetProperty (key) method of the Properties object to get the corresponding value

Third, using JDBC to establish a connection

Because this article mainly introduces the use of the. Properties configuration file, do not repeat here


   
  
  1. /* Assume that you have obtained driverclass,url, username, password value */
  2. Registration driver
  3. Class.forName (Driverclass);
  4. Establish a connection
  5. Connection con = drivermanager.getconnection (URL, username, password);
  6. Get SQL statement Execution object
  7. Statement stat = con.createstatement ();
  8. Call the Performer object method, execute the SQL statement to get the result set
  9. String sql = "SELECT * from sort";
  10. ResultSet rs = stat.executequery (SQL);
  11. The ResultSet interface method, Boolean next () returns True, has a result set, returns false without a result set
  12. while (Rs.next ()) {
  13. To get the data for each column, use the method that is the ResultSet interface Getxx method parameter, it is recommended to write String column name
  14. System.out.println (Rs.getint ("Sid") +"" "+rs.getstring (" sname ") +
  15. " "+rs.getdouble ("sprice") +"" "+rs.getstring (" Sdesc ");
  16. }
  17. Close the object, open and close
  18. Rs.close ();
  19. Stat.close ();
  20. Con.close ();
Iv. common methods of properties objects

1. GetProperty (String key)that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.

2. load (InputStream instream)to read the list of attributes (key and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file (for example, the Test.properties file above). To search for GetProperty (String key).

3. SetProperty (string key, String value) , calls the Hashtable method put. He sets a key-value pair by calling the put method of the base class.

4. Store (OutputStream out, String comments)writes the list of attributes (key and element pairs) in this properties table to the output stream, in a format that is appropriate for loading into the properties table using the Load method. In contrast to the load method, the method writes a key-value pair to the specified file.

5. Clear ()clears all loaded key-value pairs. This method is provided in the base class.

6. KeySet (), get the Key collection (properties inherit Hashtable, internally via map)

This article link: 81149710

Related articles: 81150644

Reference article: https://www.cnblogs.com/bakari/p/3562244.html

Actions for using the properties class and configuration files in JDBC

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.