JDBC Video tutorial - How to read Data from MySQL Database?

This video tutorial is for beginners in database programming using JDBC and teaches them to read the existing data from MySQL database table.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

JDBC Beginners tutorial for reading the data from MySQL database - RoseIndia.Net

In this tutorial you will learn the code to read the data from the MySQL table and understand how to execute the query to get the data from database table. We have used the sql statement which selects all the records from the employee table and then prints on the console.

The Java Database Connectivity API (JDBC) provides rich set of classes, interfaces for performing all types of database operations from the database. JDBC API allows the developers to write program, which connects to the MySQL database using pure type 4 JDBC driver.

Steps for reading the data from MySQL database in a Java program:

Step 1: Connect to the MySQL database
Following code connects to the MySQL database and returns the JDBC Connection object to the Java program.

con = DriverManager.getConnection(url + db, "root", "root");

Step 2: Write the select query - Following is the sql query which is used to select the data.

String sql = "SELECT * FROM employee ";

Step 3: Execute the sql - The Statement object is used to execute the sql statement. Here is the code that executes the query:

ResultSet res = st.executeQuery(selectSQL);

The above code returns the ResultSet object which contains all the data returned from the database.

Step 5: Display the data

Following code block prints the data on the console:

while (res.next()) {
  int id = res.getInt("id");
  String name = res.getString("name");
  String salary = res.getString("salary");
  System.out.println("ID: "+ id);
  System.out.println("Name: "+ name);
  System.out.println("Salary: "+ salary);
}

Step 5: Close the statement and connection object
Following code is used to close the Statement and Connection object:

st.close();
con.close();

Here is the video of the Tutorial of reading the data from MySQL table:

Here is the full code of the Java program:

package net.roseindia;

import java.sql.*;


public class ReadData {

public static void main(String[] args) {

System.out.println("Read Data Example!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "jdbcexamples";
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, "root", "root");
try {
		
	Statement st = con.createStatement();
	String selectSQL="SELECT * FROM  employee";
	
	ResultSet res = st.executeQuery(selectSQL);
	 
	  System.out.println("Data:");
	  while (res.next()) {
		  int id = res.getInt("id");
		  String name = res.getString("name");
		  String salary = res.getString("salary");
		  System.out.println("ID: "+ id);
		  System.out.println("Name: "+ name);
		  System.out.println("Salary: "+ salary);
	  }
	  

	System.out.println("Data Displayed");
} catch (SQLException s) {
	System.out.println("SQL statement is not executed!");
}

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

}

}

To run the program right click in the java code window and then select Run As-> Java Application

After successful execution of the program, application will display the data from the MySQL table.

Briefly following are the steps to delete data in database:

  1. Connect to MySQL Database using JDBC
  2. Create the Statement Object
  3. Construct the sql
  4. Run the sql using the Statement object against database process results if any.

Download the source code of the JDBC read example project in Eclipse project format.

So, in this tutorial on RoseIndia.net you have learned the example program for reading the data from MySQL database using the JDBC driver.

Check all JDBC Tutorials at http://www.roseindia.net/jdbc/index.shtml

Advertisements

Ads
Share on Google+Share on Google+

JDBC Video tutorial - How to read Data from MySQL Database?

Posted on: June 12, 2015 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JDBC Video tutorial - How to read Data from MySQL Database?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads