In this post we will see how to write a simple java program to demonstrate the connectivity established with MySQL server. Open an IDE, we use Intelliji and you can use any based on your preference.
Import the MySQL driver we downloaded in our previous post. Then create a class file, in which we will connected to the database
CODE
import java.sql.*;
public class DB_Connectivity {
public static void main(String[] args) throws SQLException {
String host = "localhost";
String port = "3306";
String password = "password";
String query = "SELECT VERSION()";
Connection con = DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/mermaid", "root", password );
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
if (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
EXPLANATION
out