- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to retrieve particular columns of a table using JDBC program?
A ResultSet interface in JDBC represents the tabular data generated by SQL queries. It has a cursor which points to the current row. Initially this cursor is positioned before first row.
You can move the cursor using the next() method and, you can retrieve the column values of a row using the getter methods of the ResultSet interface (getInt(), getString(), getDate() etc..).
To retrieve required data from a table:
Connect to the database.
Create a Statement object.
Execute the Statement using the executeQuery() method. To this method, pass the select query in the String format. To retrieve all the values, we use the following query:
Select * from TableName;
To retrieve particular columns, specify the required column names instead of * as:
select Name, DOB from Emp
Example
Assume we have a table named Emp in the database with the following description:
+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | Name | varchar(255) | YES | | NULL | | | DOB | date | YES | | NULL | | | Location | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+
Following JDBC example retrieves Name and DOB values of the employees from the Emp table.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class RetrievingParticularColumn { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Retrieving the data ResultSet rs = stmt.executeQuery("select Name, DOB from Emp"); System.out.println("Contents of the table"); while(rs.next()) { System.out.print("Name of the Employee: "+rs.getString("Name")+", "); System.out.print("Date of Birth: "+rs.getDate("DOB")); System.out.println(""); } } }
Output
Connection established...... Contents of the table Name of the Employee: Amit, Date of Birth: 1970-01-08 Name of the Employee: Sumith, Date of Birth: 1970-01-08 Name of the Employee: Sudha, Date of Birth: 1970-01-05
- Related Articles
- How to retrieve binary data from a table using JDBC?
- How to retrieve a DATALINK object from a table using JDBC?
- How to get the number of columns of a table using JDBC?
- How to retrieve Date from a table in JDBC?
- How to retrieve multiple ResultSets from a stored procedure using a JDBC program?
- How to retrieve a record from an existing table in a database using JDBC API?
- How to reorder the columns of a table in JDBC?
- How to retrieve a record from an existing table in oracle database using JDBC API?
- How to insert data into a table with auto-incremented columns using JDBC?
- How can we retrieve time from a table in JDBC?
- How can we retrieve a blob datatype from a table using the getBinaryStream() method in JDBC?
- Write an JDBC example to retrieve Clob value from a table using the getCharacterStream() method?
- How to Read data from BLOB and CLOB type columns from a table using JDBC?
- How to write data into BLOB and CLOB type columns in a table using JDBC?
- How to create a table in JDBC using another table?
