Java Program To Fetch Data From MySQL Table (Database) Using JDBC

By Saruque Ahamed Mollick

We have already learned  How to insert data into a MySQL table using Java Program  We will use that same table and will fetch data from the inserted value using a simple Java program.
In case of data insertion we had used PreparedStatement But here we have to use Statement and ResultSet.
To know how to attach JDBC with your Java Project and those previous kinds of stuff please take a look at this post. Java Program To Insert Data Into MySQL Table Using JDBC
We assume you have set up all those required things following that post.

JDBC is Java Database Connectivity. Using JDBC we will be able to make the connection between the database and our java program.
In this program, we will connect MySQL Server with Java. So we need a connector for MySQL that is also known as
MySQL-connector-java and its available in the different versions
Here is the official link to find the file. Or you can download it yourself from google search. https://dev.mysql.com/downloads/connector/j/
after downloading the zip file you have to open it. there you will find a jar file MySQL-connector-java-version
you have to add this file to your project library. Just add this jar file to your project library

for NetBeans User just right click on your project name>>>>properties>>>>>Libraries>>>>>Add JAR/FOLDER>>>>choose the jar file

For eclipse user

Right click on your project name>>>Properties>>>>Java Build Path>>>>Add JARs>>>>choose your file here



Turn on your MySQL server and create a table as per your wish where you are going to insert data or records.
example:
I have created a table like this

Mysql table

here is the create table statement

CREATE TABLE `data` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(10) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`country` varchar(20) DEFAULT NULL,
`age` int(5) DEFAULT NULL,
`sex` varchar(6) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1

We have a row like this. example of data :

Data Fetching

 

To extract this data we have to apply the following code

 

 

 

package code;

import java.sql.*;
public class CodeSpeedy{
    public static void main(String[] args){
   
      

    
   
      
       
         try {
             
             Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crud", "dbusername", "dbpassword");
   Statement mystatement = con.createStatement();
   ResultSet codespeedy=mystatement.executeQuery("select * from data where username=\"ironman\"");
      while(codespeedy.next())
      {
          System.out.println(codespeedy.getString("username")+"  "+codespeedy.getString("password")+"  "+codespeedy.getString("name")+" "+codespeedy.getString("email")+" "+codespeedy.getString("country")+" "+codespeedy.getString("age")+" "+codespeedy.getString("sex"));  
          
      }
      
         }   
         catch (Exception e){
     System.out.println(e);
    }
  }
}

Now if you wish to extract the email id only then you can use this line for output

System.out.println(codespeedy.getString("email");

 

 

Leave a Reply

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