Properties File - zhamri/MyClass-MySQL GitHub Wiki

How to manage userid and password?

Example of Java Codes for Database Connection:

    // 1. Get a connection to database
    myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB_Name", "zhamri", "stiw2024");

The above codes can be replaced by the steps below:

Step 1: Create a simple text file - student.properties

user=zhamri
password=stiw2024
dburl=jdbc:mysql://localhost:3306/DB_Name

Step 2: Replace with the codes below.

    // 1. Load the properties file
    Properties props = new Properties();
    props.load(new FileInputStream("student.properties"));
    // props.load(new FileInputStream("/Users/zhamricheani/Desktop/student.properties"));

    // 2. Read the props
    String the_User = props.getProperty("user");
    String the_Password = props.getProperty("password");
    String the_Dburl = props.getProperty("dburl");
			
    // 3. Get a connection to database
    myConn = DriverManager.getConnection(the_Dburl, the_User, the_Password);