getConnection_method1
 
Connection string :
  • c = DriverManager
    .getConnection("jdbc:postgresql://localhost:5432/postgres",
    "postgres", "postgre_manas9");
  • Driver manager sets the connections to db
Note: Yellow bulbs are shown for unused items.

 
Code :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package template1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Manas9
 */
public class Template1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        // TODO code application logic here
        System.out.println("main block executing ");
       Connection c = null;  Statement st = null;
        // connecting to db
      try {
         //replacing Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/postgres",
            "postgres", "postgre_manas9");
          System.out.println("Connected to  database successfully"); 
      } catch (SQLException ex) { ex.getErrorCode();
            String message = ex.getMessage();  
            System.out.println(message); 
      } finally {
          System.out.println("going through final block"); 
            try {
                if (c != null && !c.isClosed()) {
                    c.close(); }
            } catch (SQLException ex) { ex.getErrorCode();
                  ex.getMessage();              
            }
        }
      System.out.println("dis-Connected to  database successfully"); 
     // st.close(); con.commit();con.close();
    }
        // main block ends here
    }

		
		
Runt Time confirmation