Skip to content

Commit d5fe872

Browse files
committed
added lastrecordıd function
new record id autogenerated
1 parent 368f37d commit d5fe872

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

db-example/src/main/java/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import config.DbConnectionManagement;
22
import entity.Employee;
33
import query.InsertQueryApi;
4+
import query.SelectQueryApi;
45

56
import java.sql.Connection;
67
import java.util.Date;
@@ -12,7 +13,7 @@ public static void main(String[] args) {
1213
try {
1314
Employee tempEmployee = new Employee("Ahmet", "Aksoy", "M", new Date(), new Date());
1415
Connection connection = DbConnectionManagement.connectToDatabase();
15-
InsertQueryApi.insertRecord(tempEmployee, connection);
16+
InsertQueryApi.insertOneRecord(tempEmployee, connection);
1617
DbConnectionManagement.killDbConnection(connection);
1718
} catch (SQLException e) {
1819
e.printStackTrace();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ public class InsertQueryApi {
1313
/*
1414
Bu fonksiyon dışarıdan parametre olarak verılmıs olan kullanıcıyı veritabanına kayıt eder
1515
*/
16-
public static void insertRecord(Employee employee, Connection connection) {
16+
public static void insertOneRecord(Employee employee, Connection connection) {
1717
try {
1818
if (employee != null) {//gönderilen kaydın boş olup olmamasına bakıldı
1919
if (ConnectionValidate.validateConnection(connection)) {//baglantının basarılı ıle true
2020
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO employee (id,name,lastName,gender,birthDate,hireDate) VALUES (?,?,?,?,?,?)");
2121
/*
2222
TODO burada son kayıdı bulup idsini alıp yenı kayıt ekleren son kaydın id'sini +1 yapmak gereklı auto +1 yükselicek her personel kaydı
2323
*/
24+
//son eklenen kaydın ıdsını aldı 1 arttırıp yeni kayıt oluşturdu
25+
preparedStatement.setLong(1, SelectQueryApi.resultLastRecordId(connection) + 1);
2426

25-
preparedStatement.setLong(1, 0);//TODO +1 işlemi burada gerekli
2627
preparedStatement.setString(2, employee.getName());
2728
preparedStatement.setString(3, employee.getLastName());
2829
preparedStatement.setString(4, employee.getGender());

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@
55

66
import java.sql.Connection;
77
import java.sql.PreparedStatement;
8+
import java.sql.ResultSet;
9+
import java.sql.Statement;
810

911
public class SelectQueryApi {
1012

1113

14+
/*
15+
bu fonksiyon dışarıdan gelen tablo içindeki en son kaydın recordId'sını döner
16+
fonksiyonun yazılma nedeni kayıt eklenirken son id'li kaydın recordId'sını 1 arttırabılmek için
17+
*/
18+
public static int resultLastRecordId(Connection connection) {
19+
int lastId = 0;
20+
try {
21+
Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
22+
ResultSet resultSet = statement.executeQuery("SELECT id FROM employee");
23+
resultSet.last();
24+
lastId = resultSet.getInt(1);
25+
} catch (Exception e) {
26+
System.out.println(e.getMessage());
27+
}
28+
return lastId;
29+
}
1230

1331

1432
}

0 commit comments

Comments
 (0)