====== Using databases in projects ====== A database (DB) is useful for managing data that needs to be persisted on the disk. For instance, assume you want to keep a phone book for a contact management application. The contents of the phone book needs to be persisted, as your application needs to remember the contacts that were recorded earlier. I.e., when you restart your app, it should be able to remember what was recorded earlier. Now assume you decided to keep the list of phone book entries in a file. This means that you have to implement your own routines for inserting, removing, searching, parsing, and formatting entries. This is a lot of work. Also, what if your program crashes in the middle of modifying the file? (say you lost electricity) Your file may get corrupted. A DB solves all these problems for you. A DB consists of a number of tables, each table containing a set of rows with a specific list of columns. For instance, a table that contains a phone book may have the following columns: name, surname, address, phone. A DB enables you to insert, remove, and search for entries in database tables, including combining data from multiple tables. To use a DB, you need to first install a DB software - usually called a DB server. There are two popular choices for this: * JavaDB - Comes pre-installed with JDK 6 * MySQL - World's most popular open source database To manipulate data stored in the DB, a language called SQL is used. SQL is a declarative language, meaning that it specifies what needs to be retrieved and not how it is to be retrieved. For example, the following SQL statement, ''SELECT name, surname, phone FROM phone_book where name='Ahmet''', retrieves the ''name'', ''surname'', and ''phone'' columns for all rows where the name is ''Ahmet'', from the ''phone_book'' table. To use SQL statements from within JAVA, an API called JDBC needs to be used. JDBC comes with the JDK, and contains classes required to execute SQL statements and retrieve the results. For more information: * [[http://www.oracle.com/technetwork/java/javadb/overview/index.html|Java DB]] and [[http://www.mysql.com/|MySQL]] * [[http://www.w3schools.com/sql/default.asp|SQL]] * [[http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html|JDBC]]