Views :- 505

Jdbc Select Query(statement) example in java

Retrieving Data using JDBC Select Query(executeQuery example). This example provides how to select records from a table using JDBC application. JDBC Statement and ResultSet plays major role to get the data from table.

Refer below example to retrieve data from Mysql table using Jdbc.Get the Statement from Jdbc Connection. Statement object use to sending SQL statements to the database.


Statement statement = connection.createStatement();

Executes the given SQL statement, which returns a ResultSet object.


ResultSet rs = statement.executeQuery("SELECT * FROM trn_employee");

To process each row of the table using next() method of ResultSet. Return false when it reach to the last record of the table

package com.net.example;

import java.sql.*;
public class JdbcMysqlSelectExample {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JdbcMysqlSelectExample jdbcMysqlSelectExample = new JdbcMysqlSelectExample();
		Connection connection = jdbcMysqlSelectExample.getConnection();
		Statement statement = null;
		ResultSet rs = null;
		try {
			statement = connection.createStatement();
			rs = statement.executeQuery("SELECT * FROM trn_employee");
			while (rs.next()) {
				int empId = rs.getInt("EMP_ID");
				String empName = rs.getString("EMP_NAME");
				int empSalary = rs.getInt("EMP_SALARY");				
				System.out.println("Empl Id:- " +empId);
				System.out.println("Empl Name:- " +empName);
				System.out.println("Empl Salary:-" +empSalary);
				System.out.println("==========================");
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("SQLException Occured..");
		} finally {
			try {
				if (rs != null) {
					rs.close(); // close resultset
				}
				
				if (statement != null) {
					statement.close(); // close statement
				}

				if (connection != null) {
					connection.close(); // close connection
				}
			} catch (SQLException e) {
				System.out.println("SQLException Occured..");
			}
		}

	}

	/***
	 * Get Connection
	 * @return
	 */
	private Connection getConnection() {
		Connection connection = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/technicalkeeda", "root", "");
			
		} catch (ClassNotFoundException e) {
			System.out.println("ClassNotFoundException Occured...");
		} catch (SQLException e) {
			System.out.println("SQLException Occured...");
		}

		return connection;
	}
}

Output

Empl Id:- 1
Empl Name:- Sandeep
Empl Salary:-10000
==========================
Empl Id:- 2
Empl Name:- Suhas
Empl Salary:-19000
==========================

java  jdbc 

Current Donation 10 $

You also Like this Tutorials

HTML Comment Box is loading comments...

Other Posts

 
Hi I am Yashwant founder of www.technicalkeeda.com, Purpose of this website to share the programming knowledge in the form post , blogs and articles.
 

| Rss Feed | Contact Us | Find us on Google+