parent
74e5c8fe8d
commit
fc8a227df8
@ -0,0 +1,19 @@
|
|||||||
|
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
||||||
|
|
||||||
|
import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class CreateTable {
|
||||||
|
public boolean create(DataBaseConnection db, String createTableStatement) {
|
||||||
|
try (Connection connection = db.connect()) {
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
statement.execute(createTableStatement);
|
||||||
|
return true;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class TransactionalInsert {
|
||||||
|
public ResultSet run(Connection connection, PreparedStatement insertStatement) throws SQLException {
|
||||||
|
try {
|
||||||
|
ResultSet result = insertStatement.executeQuery();
|
||||||
|
connection.commit();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
connection.rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class TransactionalUpdate {
|
||||||
|
public int run(Connection connection, PreparedStatement updateStatement) throws SQLException {
|
||||||
|
try {
|
||||||
|
int rowCount = updateStatement.executeUpdate();
|
||||||
|
connection.commit();
|
||||||
|
|
||||||
|
return rowCount;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
connection.rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue