JDBC Learning 02-properties configuration file

Source: Internet
Author: User

A, properties configuration file
The four parameters (driver, URL, username, password) that are connected in the development are usually in the configuration file, convenient for later maintenance, if the program needs to change the database only need to modify the configuration file.
To store configuration information using the properties file:
1. File location
Arbitrary, recommended under SRC.
2. File name
Any, the extension is properties
3. Contents of the document
A line of data, formatted "Key=value". Where key is named custom, if multiple words are separated by dots "." ; value does not support Chinese and Unicode conversions are used when using non-English characters.
Second, create a configuration file
1. Create a file with a suffix named properties

2. Edit Properties File

third, the use of properties files
Method One: Use ResourceBundle class
1. Load configuration file
To read a configuration file using the Getbundle method of the ResourceBundle class

ResourceBundle bundle = Resourcebundle.getbundle ("Configuration file name");

Use the. getString ("key") method to read the key's corresponding value.
Example: (Loading a configuration file is loaded automatically in a static code block)

    private static String driver;
    private static String username;
    private static String password;
    private static String URL;

    /**
     * Load configuration file
    /static{
        ResourceBundle bundle = Resourcebundle.getbundle ("db");
        Driver = bundle.getstring ("Driver");
        url = bundle.getstring ("url");
        Username = bundle.getstring ("username");
        Password = bundle.getstring ("password");
    }

2. Complete the connection using the content read in the configuration file
Instead, load the driver acquisition connection code in the previous example to use the content read from the configuration file to do the argument:

1. Loading drive
class.forname (driver);
2. Obtain connection
Connection conn =
drivermanager.getconnection (url,username,password);

Method two: Using Properties objects
The configuration file is processed by using the properties object to load the input stream.
Methods are as follows

1. Get class loader
            ClassLoader ClassLoader = TestJDBC03.class.getClassLoader () through current class;
            2. Get input stream
            //filename to write full name
            InputStream is = Classloader.getresourceasstream ("db.properties");
            3. Create a Properties object
            properties Properties = new properties ();
            4. Load input stream
            properties.load (is);
            5. Obtain the value of the relevant parameter
            driver=properties.getproperty ("Driver");
            Url=properties.getproperty ("url");
            Username=properties.getproperty ("username");
            Password=properties.getproperty ("password");

The two methods run the same result:
The results of the run are the same as the previous example

Iv. Examples
The sample code is as follows
Mode one:

Import java.sql.*;

Import Java.util.ResourceBundle;
    public class TestJDBC02 {private static String driver;
    private static String username;
    private static String password;

    private static String URL;
        /** * Load configuration File/static{resourcebundle bundle = Resourcebundle.getbundle ("db");
        Driver = bundle.getstring ("Driver");
        url = bundle.getstring ("url");
        Username = bundle.getstring ("username");
    Password = bundle.getstring ("password"); public static void Main (string[] args) throws ClassNotFoundException, SQLException {//1. Load driver Clas
        S.forname (driver);
        2. Obtain connection Connection conn = drivermanager.getconnection (Url,username,password);
        3. Get statement Execution object String sql = "SELECT * from Fruits WHERE s_id=?";
        PreparedStatement PSMT = conn.preparestatement (sql);
        Psmt.setint (1,107);
        4. Processing result set ResultSet rs = Psmt.executequery (); while (Rs.next ()) {
            System.out.println (rs.getstring ("s_id") + "---" +rs.getstring ("F_name"));
        //5. Release resource if (rs!=null) {rs.close ();
        } if (Psmt!=null) {psmt.close ();
        } if (Conn!=null) {conn.close ();
 }
    }
}

Mode two:

Import java.io.IOException;
Import Java.io.InputStream;
Import java.sql.*;
Import java.util.Properties;

Import Java.util.ResourceBundle;
    public class TestJDBC03 {private static String driver;
    private static String username;
    private static String password;

    private static String URL; /** * Load configuration File/static{try {//1. Gets the class loader from the current class ClassLoader ClassLoader = Test
            JDBC03.class.getClassLoader ();
            2. Get input stream//filename to write full name inputstream is = Classloader.getresourceasstream ("db.properties");
            3. Create a Properties Object properties Properties = new properties ();
            4. Load input stream properties.load (is);
            5. Obtain the value of the relevant parameter Driver=properties.getproperty ("Driver");
            Url=properties.getproperty ("url");
            Username=properties.getproperty ("username");
        Password=properties.getproperty ("password");
  catch (IOException e) {          E.printstacktrace ();
        } public static void Main (string[] args) throws ClassNotFoundException, SQLException {//1. Load driver
        Class.forName (driver);
        2. Obtain connection Connection conn = drivermanager.getconnection (Url,username,password);
        3. Get statement Execution object String sql = "SELECT * from Fruits WHERE s_id=?";
        PreparedStatement PSMT = conn.preparestatement (sql);
        Psmt.setint (1,107);
        4. Processing result set ResultSet rs = Psmt.executequery ();
        while (Rs.next ()) {System.out.println (rs.getstring ("s_id") + "---" +rs.getstring ("F_name"));
        //5. Release resource if (rs!=null) {rs.close ();
        } if (Psmt!=null) {psmt.close ();
        } if (Conn!=null) {conn.close (); }
    }
}

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.