Ad Code

Connect Java to MySQL using JDBC | Java Database Connectivity.

Connect java to Database using JDBC | Java Database Connectivity. 

Welcome to CodingFizz,

In this tutorial, we will learn how to connect the Java program to a MySQL database using Java Database Connectivity (JDBC).


It is easy, firstly you need to install the mysql.connector.java.jar file. Then connect the .jar file to your IDE. In this tutorial, we will connect MySQL to VS Code (Visual Studio Code).

Steps:

  • Download the mysql.connector.java.jar file.
  • Extract the .jar file.
  • Open your Visual Studio Code.
  • Go to your application or program.
  • Then, Select the Java Project.
  •  Select the referenced libraries. 

  • Add the mysql.connector.java.jar file from the driver.
  • After this entire process, the .jar file is connected with your IDE.

Then write some code line to connect your program to the database.

Code:


 // import some packages
 import java.sql.Connection;
 import java.sql.DriverManager;

 // code for connect to database
 class connect {

    static Connection con;

    public static Connection createC() {

        try {

            // load the driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // connet to the database
            String user = "root";
            String password = "root";
            String url = "jdbc:mysql://localhost:3306/students";

            con = DriverManager.getConnection(url, user, password);
        } 

        catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
 }

Post a Comment

0 Comments

Ad Code