Step 1: Create Connection object and getter method if needed. This Interface is inside the java.sql package.
/*
* This code helpful only for
* MySQL system. if you want to use with Oracle
* than change the url and driver registration parameter
*/
public class DBConnection {
private static Connection conn;
/*
* this method will help to get Connection Object
*/
public static Connection getConn() {
return conn;
}
![]() |
Don't miss any step |
Step 2:
load the Driver into your Project.
Steps
download MySQL connector jar file from the Internet.
In the case of an eclipse IDE
right-click on project -> property -> Build project
into Library tab, add external jar(add jar file with the name bean).
Step 3:
- In order to access DB, We must have an authentication of the Server and the Database and for that, we need a valid username and password also.
- Location of the Database must be needed, Where it is hosted and internal drivers and name and all.
For these, we created 3 variables url, username, password.
Driver registration is needed to open the communication channel with the database.
DriverManager(args) Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.
Formate of the url is jdbc:subprotocol:subname
/*
* This method is root of the code
*/
public static boolean connectToDB(String database) {
String url;
String username;
String password;
url = "jdbc:mysql://localhost:3306/" + database;
username = "root";
password = "";
try {
// Registration of Driver
Class.forName("com.mysql.jdbc.Driver");
try {
// Try to open a Connection
conn = DriverManager.getConnection(url, username, password);
return true;
} catch (SQLException e) {
System.out.println("Problem in username and password or there will no database with name " + database);
return false;
}
} catch (ClassNotFoundException e1) {
System.out.println("Driver Not Found \nAdd Jar file of Driver in the Project ");
return false;
}
}
* This method is root of the code
*/
public static boolean connectToDB(String database) {
String url;
String username;
String password;
url = "jdbc:mysql://localhost:3306/" + database;
username = "root";
password = "";
try {
// Registration of Driver
Class.forName("com.mysql.jdbc.Driver");
try {
// Try to open a Connection
conn = DriverManager.getConnection(url, username, password);
return true;
} catch (SQLException e) {
System.out.println("Problem in username and password or there will no database with name " + database);
return false;
}
} catch (ClassNotFoundException e1) {
System.out.println("Driver Not Found \nAdd Jar file of Driver in the Project ");
return false;
}
}
Step 4:
once a connection is established, it is good to close the connection channel in the end.
/*
* It is good habit to close the connection this method is for close the
* connection
*/
public static void releaseConnection(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("There is problem in closing connection");
}
}
* It is good habit to close the connection this method is for close the
* connection
*/
public static void releaseConnection(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("There is problem in closing connection");
}
}
Step 5:
This step may be different in your case.public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Database name: ");
String databaseName = sc.next();
boolean status = DBConnection.connectToDB(databaseName);
if (status) {
System.out.println("You are connected with database " + databaseName);
if (DBConnection.getConn() != null) {
System.out.println("Connection object is found");
} else {
System.out.println("Connection is not established yet");
}
} else {
System.out.println("You are not connected with database " + databaseName);
}
}
}
The whole code is here
https://github.com/denesvaghani/JDBCConnection-MySQL-
February 23, 2020
Tags :
JDBC
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
Please comment here...