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:
- Connect to MySQL Database using JDBC
- Create the Statement Object
- Construct the sql
- 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