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 −

Updated on: 2019-12-24T07:58:15+05:30

527 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements