Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Java Programming » JDBC » Display data from table using JDBC and MYSQL
Suryateja Pericherla Categories: JDBC. No Comments on Display data from table using JDBC and MYSQL
0
(0)

In this example, we will use MYSQL DBMS which contains a database named sampledb and a table named students. The credentials for connecting to the database requires username and password as root and 123456 respectively. The data in the students table is given below:

In this example, we will use MYSQL DBMS which contains a database named sampledb and a table named students. The credentials for connecting to the database requires username and password as root and 123456 respectively. The data in the students table is given below:
 

For working with MYSQL DBMS and the databases in it, MYSQL provides their own driver for Java. Programmer must develop the driver named Connector/J from MYSQL website. It is free of cost. After downloading the driver, place the JAR file in the classpath environment variable.
 

The code for displaying data from a table using JDBC is given below:

import java.sql.*;
public class GetData {
   static final String DB_URL = "jdbc:mysql://localhost/sampledb";
   static final String USER = "root";
   static final String PASS = "123456";
   static final String QUERY = "SELECT id, name, mobile FROM students";

   public static void main(String[] args) {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery(QUERY);) {
         // Extract data from result set
         while (rs.next()) {
            // Retrieve by column name
            System.out.print("ID: " + rs.getInt("id"));
            System.out.print(", Name: " + rs.getString("name"));
            System.out.print(", mobile: " + rs.getLong("mobile"));
            System.out.println();
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

 

Output of the above program is given below:

ID: 1, Name: Ramesh, mobile: 9988776655
ID: 2, Name: Mahesh, mobile: 9988776622
ID: 3, Name: Priya, mobile: 7888776622
ID: 4, Name: Harshita, mobile: 8877662233
ID: 5, Name: Praneeth, mobile: 8877662233
ID: 6, Name: Nithya, mobile: 8877661122

 

We can see that the mobile number of students with id 4 and 5 is same. So, we are going to update the mobile number of students with id 5 next.
Note: The above compiles successfully only when we add the JAR file to the CLASSPATH environment variable as shown below:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


set CLASSPATH=.;C:\Soft\mysql-connector-j-9.1.0\mysql-connector-j-9.1.0.jar

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram