Properties collection class

Source: Internet
Author: User

Define a field and assign a value to it as a key-value pair to separate the value from the code.

One of the functions of the Properties collection class is to establish a connection with the. properties or xml file in the project to obtain the value that exists in the form of a key-value Pair written in the properties file.

Public class db_util {private static String url; private static String driver; private static String username; private static String password; // static code block, only when the program is started for the first time, static {try {Properties prop = new Properties (); // when the Properties class obtains the value in the property file, the file byte stream InputStream is = new FileInputStream ("db. properties "); prop. load (is); // obtain valueurl = prop through key. getProperty ("url"); driver = prop. getProperty ("driver"); username = prop. getProperty ("username"); password = prop. getProperty ("password");} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}// connect to the database public static Connection getConnection () {Connection conn = null; try {Class. forName (driver); conn = DriverManager. getConnection (url, username, password);} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();} return conn;} public static void main (String [] args) {System. out. println (getConnection ());}}



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.