You can connect your Java code with MySQL database using Connection interface and DriverManager class.
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/Database_name?autoReconnect=true&useSSL=false",“username","password");
Or you can use DataSource.
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("user_name");
dataSource.setPassword("password");
dataSource.setServerName("Server_name");
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM USERS");
...
rs.close();
stmt.close();
conn.close();
Hope this helps!
Check out Java training online to become certified expert.
Thanks!