Skip to content

Commit b5ec0a3

Browse files
committed
create update query function
1 parent aea59b2 commit b5ec0a3

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

db-example/src/main/java/entity/Employee.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public Employee(String name, String lastName, String gender, Date birthDate, Dat
2222
this.hireDate = hireDate;
2323
}
2424

25+
public Long getId() {
26+
return id;
27+
}
28+
2529
public String getName() {
2630
return name;
2731
}

db-example/src/main/java/query/SelectQueryApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static int resultLastRecordId(Connection connection) {
3535
--- Neden bu fonksiyonu kullanmalıyız ----
3636
1-> Kayıt silerken o kaydın veritabanında olup olmadığı önemlidir çünkü olmayan kayıdı silmeye calısırsak hata alırız
3737
2-> Kayıt eklerken aynı id'de kayıt olabilir bu seferde hata alırız ve kayıt ekleyemeyebiliriz
38+
3-> Kayıt güncellerken o kaydın olup olmadığı önemlidir sonuçta kayıt yoksa eğer güncelleme işlemi yapılmaz o kaydın ılk eklenmesi gerekir
3839
*/
3940
public static Integer findByEmployeeId(int id, Connection connection) {
4041
Integer recordId = null;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
package query;
22

3+
import entity.Employee;
4+
5+
import java.sql.Connection;
6+
import java.sql.Date;
7+
import java.sql.PreparedStatement;
8+
39
public class UpdateQueryApi {
10+
11+
public static void updateRecord(Employee employee, Connection connection) {
12+
try {
13+
14+
//employee id'de kayıt varsa eğer guncelleme işlemi yapılıcak
15+
if (SelectQueryApi.findByEmployeeId(employee.getId().intValue(), connection) != null) {
16+
PreparedStatement preparedStatement =
17+
connection.prepareStatement("UPDATE employee SET name = ?, lastName = ?, gender = ?, birthDate= ?,hireDate= ? WHERE id = ? ");
18+
19+
preparedStatement.setString(1, employee.getName());
20+
preparedStatement.setString(2, employee.getLastName());
21+
preparedStatement.setString(3, employee.getGender());
22+
preparedStatement.setDate(4, new Date(employee.getBirthDate().getTime()));
23+
preparedStatement.setDate(5, new Date(employee.getHireDate().getTime()));
24+
preparedStatement.setInt(6, employee.getId().intValue());
25+
int updatedRowCount = preparedStatement.executeUpdate();
26+
System.out.println(updatedRowCount + " rows updated!");
27+
}
28+
} catch (Exception e) {
29+
System.out.println(e.getMessage());
30+
}
31+
32+
}
433
}

0 commit comments

Comments
 (0)