JDBC Updateable ResultSet Example


 

JDBC Updateable ResultSet Example

In this tutorial you will learn how to update table using ResultSet

In this tutorial you will learn how to update table using ResultSet

Updateable ResultSet Example

You can update the database table using result set. To update the table using result set you need to set the ResultSet property as Statement statement = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);. And execute the query and move the cursor to the row in which you want to update.

An example given below which illustrate how to update table through resultset.

UpdateableResultSet.java

package roseindia.net;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateableResultSet {
	Connection connection = null;
	static int roll;

	public UpdateableResultSet() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
	}

	public Connection getConnection() throws SQLException {
		connection = DriverManager.getConnection(
				"jdbc:mysql://192.168.10.13:3306/student", "root", "root");

		return connection;
	}
	// Auto incrementing roll No
	public int getRoll(int roll) {
		this.roll = roll + 1;
		return this.roll;
	}

	public static void main(String[] args) throws Exception {
		UpdateableResultSet updateableResultSet = new UpdateableResultSet();
		Connection conn = updateableResultSet.getConnection();
		Statement statement = conn.createStatement(
				ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
		String query = "SELECT * FROM student";
		ResultSet rs = statement.executeQuery(query);
		System.out.println("Before Update");
		while (rs.next()) {
			System.out.println(rs.getInt(1) + ":" + rs.getString(2) + ":"
					+ rs.getString(3) + ":" + rs.getString(4));
		}
		//Updating First Row
		rs.first();
		rs.updateString("name", "Java");
		rs.updateRow();
		
		rs.last();
		int lastRoll = rs.getInt(1);
		
		rs.moveToInsertRow();
		int roll = updateableResultSet.getRoll(lastRoll);
		rs.updateInt("RollNo", roll);
		rs.updateString("name", "Data Structures");
		rs.updateString("Course", "Program");
		rs.updateString("Address", "LaheriaSarai");
		rs.insertRow();

		rs.beforeFirst();
		while (rs.next()) {
			System.out.println(rs.getInt(1) + "\t" + rs.getString("Name")
					+ "\t" + rs.getString("Course") + "\t"
					+ rs.getString("Address") + "\t");

		}
		rs.close();
		statement.close();
		conn.close();
	}
}
When you run this application it will display message as shown below:

Before Update
1:Java:MCA:Motihari
2:Ravi:BCA:Patna
3:Mansukh:M.Sc:Katihar
4:Raman:B.Tech:Betiah
5:Kanhaiya:M.Tech:Delhi
6:Tinkoo:MBA:Alligarh
7:Pawan:BBA:Darbhanga
8:Ram:BCA:Patna
1 Java MCA Motihari
2 Ravi BCA Patna
3 Mansukh M.Sc Katihar
4 Raman B.Tech Betiah
5 Kanhaiya M.Tech Delhi
6 Tinkoo MBA Alligarh
7 Pawan BBA Darbhanga
8 Ram BCA Patna
9 Data Structures Program LaheriaSarai

Download this example code

Ads