Skip to content
Snippets Groups Projects
Commit 47aa5a14 authored by Goik Martin's avatar Goik Martin
Browse files

Reading account sums

parent ae5a5fa6
No related branches found
No related tags found
No related merge requests found
package de.hdm_stuttgart.mi.sda2.account;
import java.sql.Connection;
import java.sql.SQLException;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import de.hdm_stuttgart.mi.sda2.account.sql.DbHandler;
public class BrowserDriver extends Application {
final DbHandler dbh = new DbHandler();
final TextField accountSum = new TextField();
final Button
refreshBtn = new Button("Refresh");
public static void main( String[] args ) {
launch(args);
}
public BrowserDriver() throws SQLException {
dbh.connect(Connection.TRANSACTION_READ_UNCOMMITTED, true);
refreshBtn.setOnAction(event-> {
accountSum.setText("" + dbh.getAccountSum());
});
accountSum.setEditable(false);
accountSum.setText("" + dbh.getAccountSum());
}
@Override
public void start(final Stage primaryStage) throws SQLException {
primaryStage.setTitle("Browsing accounts");
final GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
int currentRowIndex = 0;
final Text scenetitle = new Text("Sum of balances");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, currentRowIndex, 2, 1);
currentRowIndex++;
grid.add(new Label("Balances sum:"), 0, currentRowIndex);
grid.add(accountSum, 1, currentRowIndex);
currentRowIndex++;
grid.add(refreshBtn, 1, currentRowIndex);
Scene scene = new Scene(grid, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
super.stop();
dbh.close();
}
}
...@@ -16,7 +16,7 @@ import javafx.scene.layout.Priority; ...@@ -16,7 +16,7 @@ import javafx.scene.layout.Priority;
* *
*/ */
public class ExceptionDialog { public class ExceptionDialog {
/** /**
* Showing an error box and terminating without any further error processing * Showing an error box and terminating without any further error processing
* *
...@@ -26,7 +26,7 @@ public class ExceptionDialog { ...@@ -26,7 +26,7 @@ public class ExceptionDialog {
* *
*/ */
public static void showExceptionAndExit(final String msg, final Exception ex, int exitCode) { public static void showExceptionAndExit(final String msg, final Exception ex, int exitCode) {
Alert alert = new Alert(AlertType.ERROR); Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Unrecoverable error"); alert.setTitle("Unrecoverable error");
alert.setHeaderText("Application will be terminated!"); alert.setHeaderText("Application will be terminated!");
...@@ -34,29 +34,32 @@ public class ExceptionDialog { ...@@ -34,29 +34,32 @@ public class ExceptionDialog {
// Create expandable Exception. // Create expandable Exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();
Label label = new Label("You may copy and forward this exception stacktrace to an expert:"); if (null != ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();
Label label = new Label("You may copy and forward this exception stacktrace to an expert:");
TextArea textArea = new TextArea(exceptionText); TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false); textArea.setEditable(false);
textArea.setWrapText(true); textArea.setWrapText(true);
textArea.setMaxWidth(Double.MAX_VALUE); textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE); textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS); GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS); GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane expContent = new GridPane(); GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE); expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0); expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1); expContent.add(textArea, 0, 1);
// Set expandable Exception into the dialog pane. // Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent); alert.getDialogPane().setExpandableContent(expContent);
}
alert.showAndWait(); alert.showAndWait();
System.exit(exitCode); System.exit(exitCode);
......
...@@ -3,6 +3,7 @@ package de.hdm_stuttgart.mi.sda2.account.sql; ...@@ -3,6 +3,7 @@ package de.hdm_stuttgart.mi.sda2.account.sql;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
...@@ -16,8 +17,13 @@ public class DbHandler { ...@@ -16,8 +17,13 @@ public class DbHandler {
Connection conn = null; Connection conn = null;
boolean autoCommit; boolean autoCommit;
final static String updateAccountSql = "UPDATE Account SET balance = balance + ? WHERE number = ?";
PreparedStatement updateAccount; PreparedStatement updateAccount;
final static String accountSumSql = "SELECT SUM(balance) FROM Account";
PreparedStatement accountSum;
private String getConnectionName() { private String getConnectionName() {
return Conf.get("DbHandler.user") + '@' + Conf.get("DbHandler.jdbcUrl"); return Conf.get("DbHandler.user") + '@' + Conf.get("DbHandler.jdbcUrl");
} }
...@@ -31,7 +37,10 @@ public class DbHandler { ...@@ -31,7 +37,10 @@ public class DbHandler {
Conf.get("DbHandler.password")); Conf.get("DbHandler.password"));
conn.setAutoCommit(autoCommit); conn.setAutoCommit(autoCommit);
conn.setTransactionIsolation(isolationLevel); conn.setTransactionIsolation(isolationLevel);
updateAccount = conn.prepareStatement("UPDATE Account SET balance = balance + ? WHERE number = ?");
updateAccount = conn.prepareStatement(updateAccountSql);
accountSum = conn.prepareStatement(accountSumSql);
log.info("Connection '" + getConnectionName() + "' established" ); log.info("Connection '" + getConnectionName() + "' established" );
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
...@@ -69,6 +78,22 @@ public class DbHandler { ...@@ -69,6 +78,22 @@ public class DbHandler {
} }
} }
public int getAccountSum() {
try {
final ResultSet rs = accountSum.executeQuery();
if (rs.next()) {
final int ret = rs.getInt(1);
rs.close();
return ret;
} else {
ExceptionDialog.showExceptionAndExit("Weird problem summing up balances problem: ", null, 1);
}
} catch (SQLException e) {
ExceptionDialog.showExceptionAndExit("Committing transaction failed: ", e, 1);
}
return 0;
}
public void changeBy(int accountNo, int amount) { public void changeBy(int accountNo, int amount) {
try { try {
updateAccount.setInt(1, amount); updateAccount.setInt(1, amount);
......
package de.hdm_stuttgart.mi.sda2.account; package de.hdm_stuttgart.mi.sda2.account;
import java.sql.Connection;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -13,7 +15,7 @@ public class DbTest { ...@@ -13,7 +15,7 @@ public class DbTest {
public void testApp() { public void testApp() {
DbHandler dbh = new DbHandler(); DbHandler dbh = new DbHandler();
dbh.connect(); dbh.connect(Connection.TRANSACTION_SERIALIZABLE, false);
dbh.changeBy(1, 20); dbh.changeBy(1, 20);
dbh.changeBy(2, -20); dbh.changeBy(2, -20);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment