Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class Pakollinen2 {
- private static final String DRIVER = "org.postgresql.Driver";
- private static final String PROTOCOL = "jdbc:postgresql:";
- private static final String SERVER = "localhost";
- private static final int PORT = 5432;
- private static final String DATABASE = "teht2";
- private static final String USER = "postgres";
- private static final String PASSWORD = "kiiwisato";
- public static void main(String args[]) throws SQLException {
- try {
- Class.forName(DRIVER);
- }
- catch (ClassNotFoundException exception) {
- System.out.println("Problem loading the driver, exiting.");
- return;
- }
- // Getting inputs using In-class from Laki1, Laki2 and Oope by Jorma Laurikkala
- System.out.println("Please enter the account from which the transaction will be funded:");
- int account = In.readInt();
- System.out.println("Please enter the account which you want to transact the money to:");
- int account2 = In.readInt();
- System.out.println("Please enter the amount:");
- double amount = In.readDouble();
- double account1balance = 0;
- double account2balance = 0;
- boolean foundAccount1 = false;
- boolean foundAccount2 = false;
- PreparedStatement test = null;
- PreparedStatement stmt = null;
- PreparedStatement subtract = null;
- PreparedStatement stmt2 = null;
- PreparedStatement add = null;
- Connection con = null;
- try {
- con = DriverManager.getConnection(PROTOCOL + "//" + SERVER + ":" + PORT +
- "/" + DATABASE, USER, PASSWORD);
- con.setAutoCommit(false);
- // Testing that account exists in database.
- test = con.prepareStatement("SELECT tilinumero FROM tilit WHERE tilinumero = ?");
- test.clearParameters();
- test.setInt(1, account);
- ResultSet ts = test.executeQuery();
- if(ts.next())
- {
- foundAccount1 = true;
- }
- // Getting accounts balance.
- stmt = con.prepareStatement("SELECT saldo FROM TILIT WHERE tilinumero = ?");
- stmt.clearParameters();
- stmt.setInt(1, account);
- ResultSet rs = stmt.executeQuery();
- while(rs.next())
- {
- account1balance = rs.getDouble(1);
- }
- // Updating accounts balance.
- account1balance = (account1balance - amount);
- subtract = con.prepareStatement("UPDATE tilit SET saldo = ? WHERE tilinumero = ?");
- subtract.clearParameters();
- subtract.setInt(2, account);
- subtract.setDouble(1, account1balance);
- subtract.executeUpdate();
- // Testing that account exists in database.
- test.clearParameters();
- test.setInt(1, account2);
- ts = test.executeQuery();
- if(ts.next())
- {
- foundAccount2 = true;
- }
- // Getting accounts balance.
- stmt2 = con.prepareStatement("SELECT saldo FROM TILIT WHERE tilinumero = ?");
- stmt2.clearParameters();
- stmt2.setInt(1, account2);
- ResultSet rs2 = stmt2.executeQuery();
- while(rs2.next())
- {
- account2balance = rs2.getDouble(1);
- }
- // Updating accounts balance.
- account2balance = (account2balance + amount);
- add = con.prepareStatement("UPDATE tilit SET saldo = ? WHERE tilinumero = ?");
- add.clearParameters();
- add.setInt(2, account2);
- add.setDouble(1, account2balance);
- add.executeUpdate();
- // If both accounts existed, commiting.
- if(foundAccount1 && foundAccount2)
- {
- con.commit();
- System.out.println("Transaction complete!");
- }
- // Else errormsg and rollback.
- else if (!foundAccount1 && !foundAccount2)
- {
- con.rollback();
- System.out.println("Accounts " + account + " and " + account2 + " do not exist!");
- }
- else if (!foundAccount1)
- {
- con.rollback();
- System.out.println("Account " + account + " does not exist!");
- }
- else if (!foundAccount2)
- {
- con.rollback();
- System.out.println("Account " + account2 + " does not exist!");
- }
- test.close();
- stmt.close();
- stmt2.close();
- subtract.close();
- add.close();
- con.setAutoCommit(true);
- }
- catch (SQLException exception) {
- System.out.println("Error1: " + exception.getMessage());
- con.rollback();
- }
- if(con != null)
- {
- try {
- con.close();
- }
- catch (SQLException exception) {
- System.out.println("Error closing connection to database. Exiting");
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement