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 ());}}