Thursday 5 March 2015

Java Code For Mysql Database Connection


Java code to connect database

Step 1:
          Download   mysql-connector-java-5.1.6-bin.jar and add in your project.

step 2:

Write this code to connect mysql

import java.sql.DriverManager;
import com.mysql.jdbc.Connection;

    Connection con = null;
        try {
             Class.forName("com.mysql.jdbc.Driver");
            String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
            String DB_URL = "jdbc:mysql://localhost:3306/DB_NAME";
            String USER = "root";
             String PASS = "root";
          con =(Connection) DriverManager.getConnection(DB_URL,USER,PASS);
           return  con;
        } catch (Exception e) {
                   System.out.println(e);
        }

No comments:

Post a Comment