Database programming scenario
Suppose we have this scenario. There is a chocolate seller who sells chocolates and keeps
an account of all his sales. Whenever he makes sales he updates his account topic. He keeps
information about the date of sale, quantity, price of each chocolate and total sales for the
day. Since he has a need to keep his data permanent, he will also be using a database.
The software program he may need to keep accounting of his sales data and reports of his
sales, he may be using a software system which will have a user interface, a software pro-
gram to enter sales data and a database to store sales data.
On his user screen he may have an icon or a button to start this program. Inside this program
there may be a user screen where he can enter the quantity of sales done. When he clicks
the save button then the program runs and the sales data is saved in the database.
When you want a software program to execute SQL queries then you will need to embed
SQL statements inside your software program. You need to write programs and execute on
the database. The database engine will then run and execute the program and do the required
operation in the database.
Here are the steps you need to take needed for database programming:
Import database packages - most object oriented languages have their own
libraries which create necessary infrastructure so that you can connect to
the database. These libraries you need to import into your source code.
For example in Java the database connection management libraries include
JDBC/ODBC driver registration - You need to register the correct database
driver so that you will be able to connect to the driver. For example if your
database is mySQL then you need to register a driver related to connecting
to a mySQL database engine.
Open a connection - In your code you need to explicitly make a statement to
open a connection to the database.
Execute a query - You can write a SQL query embedded in your source code.
When your source code runs, this query also runs and it executes the queries
on the database and database gets updated.
Cleaning up - Once your query get executed, you need to close the connec-
tion to the database