1

Now I am creating a simple banking project for learning purpose where I need to do a lot of search, update and insert operations for a simple action. For example, if I want to create a transaction from a sample user id, in the "Create Trasaction" Screen, after inputting the details and pressing "submit" button, my application will do the following actions.

1) Insert a row in login session table with values: IP address, user id and timing.

2) To check if the particular user id has access to create a transaction option from user access table.

3) To check if the accounts being debited/credited belong to the same branch code as the home branch code of the creating user.

3) To check if the input inventory (if any) i.e. DD, Cheque is valid or not from inventory table.

4) To check if the account being debited/credited has freeze or not.

5) To check if the account being debited has enough available balance or not.

6) Check the account status Active/Inactive or Dormant.

7) Check and create service tax if applicable i.e. another search from S.Tax table and insert into accounts transaction table

and finally,

8) Insert a row into the accounts transaction table if the criteria pass.

Now I do not feel comfortable to write so many preparedstatement code in my Servlet for only creating a transactions. There will be other operations in my application too. So I was wondering if there is a way we can simply write these SQL statements and pass the SQL file to the Servlet anyway. Or maybe we can write a function in PL/SQL and pass the function to the servelt. Are these ways possible?

Please note, I am using J2EE and Oracle database.

1 Answer 1

2

I did this once with a project I was doing some years back and I actually achieved something close to what you are looking for I created a properties file in this format:

trans.getTransactons=select * from whateverTable where onesqlquery
trans.getTranId=select tran_id from whatevertable where anothersqlquery

So that when you write your classes you just load the Properties from the file and the query is populated from the property: for example: This Loads the Property fle

public class QueriesLoader {

    Properties prop;

    public QueriesLoader() {
    }

    public Properties getProp() {
        prop = new Properties();
        ClassLoader classLoader = getClass().getClassLoader();
        try {
            InputStream url = classLoader.getResourceAsStream("path/to/your/propertiesFile/databasequeries.properties");
            prop.load(url);
        } catch (IOException asd) {
            System.out.println(asd.getMessage());
        }
        return prop;
    }
}

And then in you Database Access Objects

public ArrayList getAllTransactions() {
        ArrayList arr = new ArrayList();
        try {
            String sql = que.getProp().getProperty("trans.getTransactons");
            PreparedStatement ps = DBConnection.getDbConnection().prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                arr.add(rs.getString(1));
            }
            DBConnection.closeConn(DBConnection.getDbConnection());

        } catch (IOException asd) {
            log.debug(Level.FATAL, asd);
        } catch (SQLException asd) {
            log.debug(Level.FATAL, asd);
        }
        return arr;
    }

And I ended up not writing a single Query Inside my classes. I hope this Helps you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.