Wednesday, January 24, 2018

Java: Databases and Data Storage

When writing code in any language, it is almost always important to be able to store lots of data. Depending on the scenario, there are many ways of keeping your data safe in Java.

A very popular data organization and storage method is to use an external database, such as a MySQL server. Your program can then connect to this server and send it data, as well as retrieve data from it in huge quantities. This is great for things such as storing lots of user accounts, details on a library of files, or other various data in large amounts. To demonstrate this, I created a simple function that when run, will connect to the specified database and will create a table to store data in.

The database can be thought of as a library full of books, each book being a table. Within each table, there are spreadsheets of data, with columns to label the data, and individual rows of data spanning across the columns. This is just like spreadsheets in programs such as Excel!

Data storage is such an important part of programming, that throughout Barry Burd's career, the general consensus about programming from his students was that "We don't need to make attractive-looking layouts. No glitzy GUIs for us. We need to access databases. Yup, just show us how to write Java programs that talk to databases" (Burd 445).

While this piece of code only creates a new table, the same code can be reused to add, remove and get data from the database as well because of the SQL standard that most databases follow in order to simplify things.

Another way of storing data is to use collections. However, collections won't persist across restarts of the program. By using this in conjunction with databases, the program can be much faster and easier to use. The program can start by storing data in a collection, and it can periodically update the database to make sure it's data matches what is inside the local collections. This will ensure that the data is saved, but by reducing the amount of times the database has to be accessed, it makes the program faster. Comparing this to the library from earlier, it wouldn't be very efficient to drive to the library and write down the details of a user into a book every time they submitted a change. Instead, by driving to the library only at the end of each day, lots of time and money is saved.

Here, I wrote a simple class with a collection called an ArrayList. To put it simply, an ArrayList is just a list of values. The collection, called names, stores a list of all usernames. There's a function to add a name, to remove a name and to check if a name is already in the collection. While this is just a simple example, the concept can scale very well and be easily expanded to suit the needs of almost any program. The benefit to collections over simpler data storage methods such as arrays, is that if you're keeping track of your users and you have an array, the array has a set maximum value. Similarly to a hotel with a limited number of rooms. For example, if you have 100 rooms, "All is well until, one day, customer number 101 shows up. As your program runs, you enter data for customer 101, hoping desperately that the array with 100 components can expand to fit your growing needs. No suck luck" (Burd 322). Collections expand on this simple concept and are scalable, easily expanding and shrinking to fit your needs and to minimize the usage of the limited space on the computer.

Have you ever tried storing lots of data somewhere and keep it nicely organized? Or even tried to organize your room and keep it that way for longer than a month? Is it a challenge, or does it just come naturally to you?

Works Cited:
Burd, Barry. Java for Dummies. 7th ed., John Wiley & Sons, 2017.

2 comments:

  1. Well done Josh! I'm impressed that you showed how you created a collection to store data such as list of all usernames and explaining what an array is. I haven't really looked into coding java because i thought it was too sophisticated but this post has me hooked. Are you an expert at Coding? because this it seems that you got the gist of it and that this is a piece of cake to you.

    ReplyDelete
    Replies
    1. Hi Christian! I have been programming for many years, although I wouldn't consider myself an expert! There is always more to learn and to improve on. I have never actually read tutorials on programming and mostly learned by trial and error and quick Google searches, I thought it was a good idea to get one and see what kinds of things I can continue improving on, or what kinds of things I have been doing correctly and should continue doing!

      Delete