How to use jdbc to connect to a database in java?

JDBC (Java Database Connectivity) is a Java API to connect and execute queries with databases.

Example:


  Connection connection = DriverManager.getConnection(
      "jdbc:mysql://localhost:3306/mydatabase", "username", "password");
  

Solution:

Executing a query and retrieving results:


  Statement statement = connection.createStatement();
  ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
  while (resultSet.next()) {
      String name = resultSet.getString("name");
      System.out.println(name);
  }
  

Beginner's Guide to Java