How to connect Java to MySQL?

MySQLMySQLi Database

To connect Java to MySQL, the Java code is as follows −

import java.sql.Connection;
import java.sql.DriverManager;
public class LostConnectionURLDemo {
   public static void main(String[] args){
      String JDBCURL="jdbc:mysql://localhost:3306/web?autoReconnect=true";
      Connection con=null;
       try{
          con = DriverManager.getConnection(JDBCURL,"root","123456");
          System.out.println("connection is established");
       }
      catch(Exception e){
         e.printStackTrace();
      }
   }
}

This will produce the following output −

raja
Published on 24-Dec-2019 07:58:15
Advertisements