Program to access data of the database and print them.

Here, we have a basic program example to print the data from the database using Python and Java.

Code to get data from database in Python language

import mysql.connector
con = mysql.connector.connect(host="localhost",user="root",passwd="root", database="coder")
cur = con.cursor()
 
cur.execute("select * from Book")
print(cur.fetchall())
con.close()

Code to get data from database in Java language

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import com.mysql.jdbc.Statement;

public class ShowData {
   public static void main(String[] args) {
      Connection con = null;
      Statement statement = null;
      try {
         HashMap hm = new HashMap<>();
         Class.forName("com.mysql.jdbc.Driver");
         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/TestDb", "root", "vsit@123");
         statement = (Statement) con.createStatement();
         String sql;
         sql = "select * from DemoTable";
         ResultSet resultSet = statement.executeQuery(sql);
         while (resultSet.next()) {
         hm.put(resultSet.getString("Name"), resultSet.getInt("Age"));
      }
      System.out.println(hm);

      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}