JDBC create connection or test connection to MySQL database
Here is a example how to make connection to database MySQL using (Java DataBase Connectivity)JDBC driver. In your project you must include MySQL JDBC Driver, otherwise you will get error "MySQL JDBC Driver not found" .
This is the full Method to check or test connection to MySQL Database using JDBC driver.
By Mohd Zulkamal
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
This is the full Method to check or test connection to MySQL Database using JDBC driver.
The TestConnectionDb Method
public static boolean TestConnectionDb() {
String DbConnectionString ="jdbc:mysql://localhost:3306/";
//or you can directly connecto to your database schema like this :
//String DbConnectionString ="jdbc:mysql://localhost:3306/<schema name>";
String User = "root";
String Password = "P@ssw0rd";
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DbConnectionString, User, Password);
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found");
return false;
} catch (SQLException ex) {
System.out.println("Connection Failed! Err Msg : " + ex.getMessage());
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
return true;
} else {
System.out.println("Failed to make connection!");
return false;
}
The Output
You made it, take control your database now!
By Mohd Zulkamal
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)