- 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 delete a row from ResultSet object using JDBC?
The deleteRow() method of the ResultSet interface deletes the current row from the current ResultSet object.
//Deleting a column from the ResultSet object rs.deleteRow();
Assume we have a table named Cricketers_Data with 6 records as shown below:
+----+------------+------------+---------------+----------------+-------------+ | ID | First_Name | Last_Name | Year_Of_Birth | Place_Of_Birth | Country | +----+------------+------------+---------------+----------------+-------------+ | 1 | Shikhar | Dhawan | 1981-12-05 | Delhi | India | | 2 | Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica | | 3 | Lumara | Sangakkara | 1977-10-27 | Matale | Srilanka | | 4 | Virat | Kohli | 1988-11-05 | Delhi | India | | 5 | Rohit | Sharma | 1987-04-30 | Nagpur | India | | 6 | Ravindra | Jadeja | 1988-12-06 | Nagpur | India | +----+------------+------------+---------------+----------------+-------------+
Following JDBC program establishes a connection with the database, retrieves the contents of the Cricketers_Data table into a ResultSet object and, deletes a row from it and prints the results:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ResultSet_DeletingRow { 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/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //Query to retrieve the contents of the employee_data table String query = "select * from Cricketers_Dataa"; //Executing the query ResultSet rs = stmt.executeQuery(query); rs.beforeFirst(); rs.absolute(3); //Deleting a column from the ResultSet object rs.deleteRow(); //Contents of the current row rs.beforeFirst(); while (rs.next()) { System.out.print("id: "+rs.getInt(1)+", "); System.out.print("First name: "+rs.getString(2)+", "); System.out.print("Last name: "+rs.getString(3)+", "); System.out.print("Year of birth: "+rs.getDate(4)+", "); System.out.print("Place of birth: "+rs.getString(5)+", "); System.out.print("Country: "+rs.getString(6)+", "); System.out.println(" "); } } }
Output
Connection established...... id: 1, First name: Shikhar, Last name: Dhawan, Year of birth: 1981-12-05, Place of birth: Delhi, Country: India id: 2, First name: Jonathan, Last name: Trott, Year of birth: 1981-04-22, Place of birth: CapeTown, Country: SouthAfrica id: 4, First name: Virat, Last name: Kohli, Year of birth: 1988-11-05, Place of birth: Delhi, Country: India id: 5, First name: Rohit, Last name: Sharma, Year of birth: 1987-04-30, Place of birth: Nagpur, Country: India id: 6, First name: Ravindra, Last name: Jadeja, Year of birth: 1988-12-06, Place of birth: Nagpur, Country: India
- Related Articles
- How to insert a row into a ResultSet object using JDBC?
- How to find the current row of a ResultSet object using JDBC?
- How to get the row count from ResultSet in JDBC
- How to get Row and Column Count from ResultSet in JDBC
- What is Result in JDBC? How to retrieve data from ResultSet object?
- How to get all the column names from a ResultSet using JDBC
- How to Navigate through a ResultSet using a JDBC program?
- How to move the ResultSet cursor to the next row in JDBC?
- How to move the ResultSet cursor to the previous row in JDBC?
- How to move the ResultSet cursor to the last row in JDBC?
- How to move the ResultSet cursor to the first row in JDBC?
- How to delete a row from a table using jQuery?
- How to update the contents of a ResultSet using a JDBC program?
- How to retrieve a DATALINK object from a table using JDBC?
- How to delete all records from a table in Oracle using JDBC API?

Advertisements