Sunday 10 November 2013

JDBC create connection or test connection to MySQL database

Leave a Comment
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.

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
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 =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.