Skip to content

Commit 70c4e25

Browse files
Updating a person's data.
1 parent 835bf50 commit 70c4e25

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

demo/src/main/java/com/example/database/PersonDao.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ public Person getById(int id) throws SQLException{
6767

6868
return null;
6969
}
70+
71+
public void update(Person person) throws SQLException{
72+
Statement statement = connetion.createStatement();
73+
74+
/* UPDATE table SET column = value [, ...] [WHERE condition] */
75+
76+
statement.executeUpdate("update person set name = '" + person.getName() + "' where id = " + person.getId());
77+
78+
statement.close();
79+
}
7080
}

demo/src/main/java/com/example/database/TestDB.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public static void main(String[] args) throws IOException{
3333
// Delete data
3434
/* personDao.delete(2); */
3535

36+
Person person3 = new Person();
37+
38+
person3.setId(3);
39+
person3.setName("Carlos");
40+
41+
personDao.update(person3);
42+
43+
44+
3645
// Get all data
3746
List<Person> persons = personDao.getAll();
3847

@@ -45,6 +54,8 @@ public static void main(String[] args) throws IOException{
4554
/* Person person2 = personDao.getById(2);
4655
System.out.println(person2); */
4756

57+
58+
4859
} catch (SQLException e) {
4960
// if the error message is "out of memory",
5061
// it probably means no database file is found

0 commit comments

Comments
 (0)