Create Connection Object using Java

In this tutorial, We will show you about How to Create Connection Object using Java. The program has been tested and shared in the post.

ConnectionProvider.java

The ConnectionProvider class used to provide the connection obeject for Database. All classes are used from JDBC API.

package com.dineshkrish.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionProvider {
private static Connection connection;
private static final String DRIVERNAME = ""; // Database driver name
private static final String CONNECTION_URL = ""; // Connection URL 
private static final String DATABASE_NAME = ""; // Database Name
private static final String DATABASE_USERNAME = ""; // Database User name
private static final String DATABASE_PASSWORD = ""; // Database Password
public static Connection getConnection() {
try {
// Loading the JDBC Driver for Specific Database
Class.forName(DRIVERNAME);
System.out.println("Loaded");
// Getting the Connection by passing Connection URL, User name and Password
connection = DriverManager.getConnection(CONNECTION_URL + DATABASE_NAME, DATABASE_USERNAME, DATABASE_PASSWORD);
System.out.println("Connected");
} catch (ClassNotFoundException ex) {
// Do Something here...
System.out.println(ex.getMessage());
ex.printStackTrace();
} catch (SQLException ex) {
// Do Something here...
System.out.println(ex.getMessage());
ex.printStackTrace();
}
return connection;
}
}

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *