Menu

updates_to_database

Luis M. Villa

Updates to the database

(Return to examples index)

Updates are queries that change the contents of the database. They are created just like the Selects. The execution of updates, always returns an integer indicating the number of rows that have changed.

import net.sourceforge.milk.sql.Update;

...

// Let's insert a new user
final Update queryInsertUser = new Update(
        "Insert user",
        "insert into user (name, surname, age, active) values (?,?,?,?)",
        new Object[] { "MyName", "MySurname", 38, true });
final Integer numberOfRowsInserted = new IssuesDb().sql().run(
        queryInsertUser);

// Now we'll change the data of an user
final Update queryChangeUser = new Update("Change user",
        "update user set name=? where name=?", new Object[] {
                "AnotherName", "MyName" });
final Integer numberOfRowsChanged = new IssuesDb().sql().run(
        queryChangeUser);

// Finally, we'll delete it
final Update queryDeleteUser = new Update("Delete user",
        "delete from user where name = ?", new Object[] { "Luis" });
final Integer numberOfRowsDeleted = new IssuesDb().sql().run(
        queryDeleteUser);

And that's all that it's to it. Now, for the final step: The use of transactions


Related

Wiki: Examples
Wiki: examples_toc
Wiki: objects_from_queries
Wiki: transactions_and_queries

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.