title: Read the dbURL, User And Password From Property File
body: <pre><code>package com.j2ee.jdbc.firstProgram;
  
  import java.io.FileReader;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.Properties;
  
  import com.mysql.jdbc.Driver;
  
  
  public class JDBC_Properties {
  
  
      public static void main(String[] args) {
          Connection con = null;
          Statement stmt = null;
          ResultSet rs = null;
  
          try {
              //Load The Driver
  
              Class.forName("com.mysql.jdbc.Driver").newInstance();
  
              /*Driver driverRef = new Driver();
              DriverManager.registerDriver(driverRef);*/
  
  
              //Get DB Connection
              //String dbUrl ="jdbc:mysql://localhost:3306/studentdb?user=root&amp;password=root";
              String dbUrl ="jdbc:mysql://localhost:3306/studentdb";
              String fileLocation ="D:\\J2EE_Workspace\\db_Credentials.properties";
  
              FileReader reader = new FileReader(fileLocation);
              Properties properties1 = new Properties();
              properties1.load(reader);
              con = DriverManager.getConnection(dbUrl,properties1);
  
              //Process SQL Queries
              String query ="select * from students_info";
              stmt=con.createStatement();
              rs=stmt.executeQuery(query);
  
              //Process the Results
              while (rs.next())
              {
                  int regno = rs.getInt("regno");
                  String firstName = rs.getString("firstname");
                  String middleName = rs.getString("middlename");
                  String lastName = rs.getString("lastname");
                  String email = rs.getString("email");
                  int mobile= rs.getInt("mobile");
                  int day = rs.getInt("birth_day");
                  String month = rs.getString("birth_month");
                  int year = rs.getInt("birth_year");
  
                  System.out.println("Regno : "+regno);
                  System.out.println("Name : "+firstName+" "+middleName+" "+lastName);
                  /*System.out.println("Middle Name : "+middleName);
                  System.out.println("Last name : "+lastName);*/
                  System.out.println("Email : "+email);
                  System.out.println("Mobile : "+mobile);
                  System.out.println("DOB : "+day+"-"+month+"-"+year);
                  System.out.println();
              }
  
              }
  
          catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          finally {
              //Close all the JDBC Objects
              try
              {
                  if(con!=null)
                  {
                      con.close();
                  }
                  if(rs!=null)
                  {
                      rs.close();
                  }
                  if(stmt!=null)
                  {
                      stmt.close();
                  }
              }
              catch (SQLException e) {
                  e.printStackTrace();
              }
          }
      }
  
  } //END OF CLASS
  </code></pre>
  
  <p>The above program reads the user and password from the property file.
  But I need to read the "dbUrl" from the property file. So i need not to change the program if i switch the Database</p>
  
  <p><strong>db_Credentials.properties</strong></p>
  
  <p>user=root</p>
  
  <p>password=root</p>
  
  <p>i want to read the dbURL from properties file but</p>
  
  <pre><code>con = DriverManager.getConnection(dbUrl,properties1);
  </code></pre>
  
  <p>accepts only 2 argument.</p>