How to Connect Java Program to a Database

Below are steps involved in connecting a Java Program to MySQL database, and querying for data.
#1. import java.sql.*; to use classes or interfaces to connect Java App to database.
#2. Register and load JDBC Driver, using Class.forName(“JDBC_Driver_Class”);
#3. Invoke DriverManager.getConnection(), by passing database URL, username, password.
#4. Using Connection returned from step #3. create Statement
#5. Execute SQL Query, which returns ResultSet
#6. Now iterate through ResultSet,
#7. fetch each Column data, and print.
#8. Any problem in connecting to db, db name, and db credentials causes SQLException.

Also note that, JDBC jar file need to be added to the project, to connect to database. For example mysql JDBC driver jar file is available here. https://dev.mysql.com/downloads/connector/j/5.0.html

//Step 1
import java.sql.*;

public class DatabaseApp {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/mondaybt";
//jdbc:mysql is the driver used to connect to underlying db   
//abcd is database name
   //localhost is domain name where database server is available
   //3306 is port number
   

   //  Database credentials
   static final String USER = "root";
   static final String PASS = "rswxyz";
   
   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //Step 2, Register JDBC driver
      Class.forName(JDBC_DRIVER);

      //Step 3, Open connection to db server
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //Step 4, create Statement object
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT * FROM student where sname like 'k%'";

      //Step 5, execute SQL Query
      ResultSet rs = stmt.executeQuery(sql);

      //Step 6, iterate thru result set
      while(rs.next()){
         //Retrieve by column name
         //Can retrieve values with column index also eg. getInt(int column_index); 
         int id=0;
          id  = rs.getInt("sid");
         String name = rs.getString("sname");
         //Step 7, fetch each Column data
         String addr = rs.getString("saddr");
         String fname = rs.getString("sfather_name");
         //Display values
         System.out.println("Row Number:"+rs.getRow());
         System.out.print("ID: " + id);
         
         System.out.print(", Name: " + name);
         System.out.print(", Address: " + addr);
         System.out.print(", Father name: " + fname);
      }
      
      //Step 6, Close ResultSet,Statement
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      //Step 7
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }
   System.out.println("Exiting Program...");
}//end main
   }

How many types of JDBC Drivers are there?
Can we create db table, from Java App
Difference between Statement and PreparedStatement