Make updates in Updatable ResultSet
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost/testdb", "root", "");
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query = "SELECT id, code, name, quantity, price FROM products";
ResultSet uprs = statement.executeQuery(query);
while (uprs.next()) {
System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":"
+ uprs.getString("name") + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price"));
}
uprs.first();
uprs.updateString("name", "Java");
uprs.updateRow();
uprs.next();
uprs.deleteRow();
uprs.moveToInsertRow();
uprs.updateString("code", "1");
uprs.updateString("name", "Data Structures");
uprs.updateInt("quantity", 1);
uprs.updateDouble("price", 5.99);
uprs.insertRow();
uprs.beforeFirst();
while (uprs.next()) {
System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t"
+ uprs.getString("name") + "\t" + uprs.getInt("quantity") + "\t"
+ uprs.getDouble("price"));
}
connection.close();
}
}
Related examples in the same category