Java Console App with AWS and JDBC as a database connector

Last week was very intense and full of new learnings. I had created a simple console bank application using Java. All my data stored in the cloud, and I used AWS. AWS database connected to Java through JDBC. It is the first time I use a cloud database in my Java application. It means that not all things run smoothly at first. In this post, I will discuss the problems I run into and their possible solutions.

Problem 1. 

Did you know that to run any app that has a connection to the database needs an environmental variable set up? And this has to be done for every class that has a main method. 

Users can put this variable in the ConnectionFactory file and do not worry about the setting environmental variable, but one can ask how safe it is. This move is not safe if we need to store this program on Github. When I set up the remote database, I have to indicate URL, USER, and PASSWORD in the connection factory. Once the file is pushed to GitHub there is a sensitive information leak. It means that anybody can go to my AWS account and use that database etc. So how to set up Environmental Variables for the class where we need to use a cloud database.

In SpringTool

Run Configurations ...

You should see this screen



Next select Environment. On the left side, we pick .java files for which we need to set up environmental variables.

Press Add. Provide with name and value. I name them DB_URL, DB_USER, and DB_PASSWORD.
If I connect to my AWS cloud database, I use a URL from the AWS account that is under the database endpoint connection. USER and PASSWORD are credentials that were set up when we create our database on AWS.  
Environmental variables set up.

Problem 2. 

Database handling in Java. In my application, I separated controllers, services (business logic), and repositories (database handling). In my repositories, I created files that will be managing data. I use simple queries to manipulate data create, update, select. All the logic once the data is retrieved is residing on the service layer. 
Steps to manipulate data. 

1) I have to create a connection to my ConnectionFactory file.
2) Creating methods to create, update, or select.
In the methods, I must initialize my Connection by calling the getConnection method. In my case, I use only one connection, while in real-world applications, I will have to manage multiple threads because there will be more than one connection.
Once the connection initialized, I have created a string 

`String sql = "select * from account where account_user=?;";`
account is my table name, and account_user is the name of the column in the table account

I used parameter for an account_user, and that is why I have to use 

`PreparedStatement getUser=conn.prepareStatement(sql);
getUser.setInt(1, customerID);
                  ResultSet res=getUser.executeQuery();`

PreparedSatement design for parameters in SQL string as well as increase performance. 
`setInt(1, customerID);` will set our parameter with value we pass, in my case it is customerID. The result will be store in the object of a specific type. 
`executeQuery()` - will run this statement and pull data from the database.

A tip to keep in mind. 
If I set my auto-commit to false, I will change it to true once the transaction complete.
`conn.setAutoCommit(false); `
`conn.setAutoCommit(true);`

I set AutoCommit to false only when I update or create objects. If the values I am passing to the database are not accurate, the transaction will not occur.

Comments

Popular posts from this blog

Release 0.6

Hacktoberfest - Issue #1, Pull Request #1

Hacktoberfest