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

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.
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.
I have created a table like this

Mysql table

here is the create table statement

`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,

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 {
   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\"");
          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){

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




3 responses to “Java Program To Fetch Data From MySQL Table (Database) Using JDBC”

  1. roger says:

    I got an error “symbol not found” for the code:

    Statement statement = conn.createStatement()
    ResultSet codespeedy=mystatement.executeQuery(“select * from data where username=\”ironman\””);

    These are the error I got (below)
    – createStatement() not found
    – executeQuery() not found.

    • Saruque Ahamed Mollick says:

      There might be numerous reasons for this. But I think the package you are using does not contain any method named createStatement(). Or perhaps you forget to import java sql package.
      Kindly recheck your code.

  2. Alex Segura says:

    Is there a way to print all the columns without the need of read column by colum, row by row using the while cycle ?

Leave a Reply

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