Reading Data using JDBC

by | Apr 15, 2021 | Java

Home » Java » Reading Data using JDBC

Reading Data Using JDBC

Introduction

JDBC stands for Java Database Connectivity. It acts as a link connecting Java programs with various independent databases. The JDBC uses several tools to perform the various functions as mentioned below:

  • Setting up a strong connection between your Java code and an external database.
  • Creation of SQL or MySQL statements.
  • When queries in SQL or MySQL arise, the JDBC uses various APIs to solve/resolve them.
  • Reading data from the various databases, modifying them and/or deleting them.

As a fundamental concept, the JDBC accounts for a complete set of interfaces that allow users to interact with databases and fetch data from the same for the execution of various programs. With the help of the JDBC, various executables can be written as follows:

  • Java Applications
  • Java Applets
  • Java Servlets
  • Java ServerPages (JSPs)
  • Enterprise JavaBeans (EJBs)

Prerequisites

The ability to read data from databases is a very imperative skill of any program. Without free access to data, any real-life program will eventually terminate due to data starvation. The examples provided in the articles show how to read data. The ‘username’ and ‘password’ mentioned in the sample codes can be replaced by actual credentials. You should also make sure the database you are using (SQL, MySQL, or any other database) is up to date and properly functioning.

Steps to Create a New Database

To be able to read data from a database, we have to create one. The following steps will guide you through the process, which will take the help of the JDBC application.

  • Importing the packages – The very first step is to include the packages that contain the JDBC classes. More than often, importing the ‘java.sql.*’ package should be enough to create a database.
  • Registering the JDBC Driver – You will need to initialize a JDBC driver such that it can initiate communication channels with a new database.
  • Opening a connection – You need to create a connection object of the JDBC classes. For this step, the following method needs to be implemented: DriverManager.getConnection(). This will be followed by creating a connection object. The object will represent a physical connection with the database server.
  • Executing a Query – Query in the form of data fetching will be executed by objects of the type Statement. The query can either be to fetch data, modify data, or even delete data from the database.
  • Extract Data – Once the SQL query has been registered and executed, the program can then fetch data from the Database.
  • Cleaning up the Environment – Once the execution of the code has finished, it is responsible programming to clean up the environment, such as the drivers and databases can be used by another program. This step is mainly carried out by the JVM’s garbage collection.

Sample Code

//This code will demonstrate how to read data using JDBC
import java.sql.*;
public class Demo {
static final String JDBC_DRIVER = “//name of the driver”;
static final String DB_URL = “//URL of the Database”;
static final String USER = “//your username”;
static final String PASS = “//Password”;
public static void main (String args [])
{
Connection conn = null;
Statement stmt = null;
try {
Class.forName (“//Name of the Driver”);
System.out.println (“Establishing connection to the Database”);
conn = DriverManager.getConnection (DB_URL, USER, PASS);
System.out.println(“Connection made successfully with DB”);
System.out.println(“Creating Statement”);
stmt = conn.createStatement ();
String sql = “SELECT id, first, last, age FROM Registration”;
Result rs = stmt.executeQuery(sql);
while (es.nect())
{
int id = rs.getInt(“id”);
int age = rs.getInt(“age”);
String first = rs.getString(“first”);
String last = rs.getString (“last”);
System.out.println(“ID: “ + id);
System.out.println(“Age: “ + age);
System.out.println(“First: “ + first);
System.out.println(“Last: “ + last);
}
rs.clode();
}catch(SQL Exception se){
se.printlnStackTrace();
} finally {
try{
if (stmt! = null)
conn.close();
} catch (SQLException se){
}
try {
if (stmt! =null)
conn.close();
}
catch(SQLException se){
}
try{
if (conn!= null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
System.out.println(“Exit”);
}
}

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author