Computer >> Computer tutorials >  >> Programming >> MySQL

How to connect Java to MySQL?


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 −

How to connect Java to MySQL?