0% found this document useful (0 votes)
7 views22 pages

Practical Series - Using A Database

The document discusses the importance of data in applications and how to manage application settings and preferences using files and libraries like Java's Preferences API and Qt's QSettings. It also covers database connectivity, particularly with SQLite, highlighting its advantages such as being lightweight and file-based, which simplifies data management without requiring complex installations. The document provides examples of how to implement database operations and manage settings in software development.

Uploaded by

uupssiizlll
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views22 pages

Practical Series - Using A Database

The document discusses the importance of data in applications and how to manage application settings and preferences using files and libraries like Java's Preferences API and Qt's QSettings. It also covers database connectivity, particularly with SQLite, highlighting its advantages such as being lightweight and file-based, which simplifies data management without requiring complex installations. The document provides examples of how to implement database operations and manage settings in software development.

Uploaded by

uupssiizlll
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical Series:

Working with Data


SE302: Principles of Software Engineering
Lecturer: Dr. İlker Korkmaz

(Slides were prepared by Dr. Kaya Oğuz)


Data
● Without the data, your application does not have much of a value.
● All the programs require some data;
○ Notepad, text files
○ Address book, contacts
○ Email client, text over a network connection,
○ Web browser, text and binary files over a network connection,
● Reading and writing files are covered under the I/O operations (input/output).
● Programming languages have libraries that can access the file system (using
operating system calls) to read and write to files.
Data
● This data can be the data your application is working on; for example the text
of this presentation.
● The data can also be the configuration of the application; for example the
“wrap lines” option in Notepad.
● Regardless, your application has to be able to read and write to files.
Application Settings, Preferences, Config, etc.
● A typical approach for storing application settings is to use a file, such as an
“ini” file, which stores the specific preferences, settings or the configuration of
the application.
● The usual place to store such a file is,
○ The installation directory, same directory with the EXE file, or one of the subdirectories,
○ A system-wide directory for settings, such as /etc on Linux.
○ A configuration directory within the HOME directory of the user; the $HOME (Unixes) or
%HOME% (Windows) variable are system variable which are set when the user logs in.
● Instead of rolling my own system, I prefer to use predefined libraries / classes.
Application Settings, Preferences, Config, etc.
● Java has the Preferences API, Qt has the QSettings class.
● For any of them, the idea is simply as follows;
○ You set a name for your application; this should be unique because some other application may
use the same name.
○ You define a set of typed “keys” and “values”. You have to know the type and names of the
keys to access the values.
○ You can check if a specific key has been set. This is useful when the application is run for the
first time.
● Here comes an example using Java.
Settings
● When the button is clicked,
the settings are saved (put) -
if they are not, default
values at lines 12 and 13 are
passed to getInt.
● When you run this, the
JFrame starts at size
400x300. When you restart
it after you click the button,
it will be 600x800…
Settings
On Windows, the
settings are stored on
the registry. Run
regedit, and go to
HKEY_CURRENT_USE
R\Software\JavaSoft\Pr
efs\ as shown on the
right.
Database Connectivity
● Instead of working with text files, databases can provide a lot of functionality
needed for your application, such as,
○ Searching,
○ Ordered listing,
○ Persistent storage.
● However, not every user enjoys installing large databases, such as MySQL or
MSSQL to their computer just to run your program.
● If the software will be used by a single user, and the updates to the database
are not that common, then you can try a file based SQL server.
● My favourite is SQLite…
Database Connectivity
● SQLite is very small; it only uses a single file which holds all your data; does not
require a background process; has “browsers” that can be used outside your
application.
● You don’t need to, but you can download an executable for your system at
[Link]
● Then, you can start it with

sqlite3 a_file_name.db

● This creates the file “a_file_name.db”, which is the database, without any
tables. The extension is not important.
Database Connectivity
● I believe the screenshot on the right
gives you an idea about SQLite, and
how easy it is to create a database (the
file), a table in the database (data), and
using regular SQL queries to instantly
get some data.
● The commands that start with a dot are
special; .tables list the existing tables,
.schema gives their schemas.
Database Connectivity
● You can use “browsers” instead of the
command line tool.
● My favourite is [Link]
● You can create, edit, update database
files easily.
● You can also run SQL queries.
Database Connectivity
● Naturally, your users will not care about the SQL - they only want to get their
data.
● You can use SQLite without these tools, however you need the required
libraries.
● You can get SQLite-JDBC jar files from [Link]
jdbc/downloads/
● You just need to add this jar, [Link] in my example, to your
project. Netbeans helps you to set up the libraries.
Database Connectivity
● I usually set a “database” class, which handles all of the database related
functions.
● This class includes the name of the database, and methods that correspond to
SQL queries, such as getUsers(), addUser(“kaya”), etc.
● This decouples SQL related requirements from other classes; all SQL queries
live in this class.
● As you’ll remember from the database course, you can “prepare” statements.
● When statements are prepared, the SQL manager knows how to handle them
and does not plan how they will be executed every time.
Database Connectivity
● The next 6 slides will show a database connectivity class. Let’s see what the
requirements for this class are…
● Connection settings for another server, such as MySQL, may vary.
● For a SQLite connection, we need the name of the file. This is the only
connection parameter. For MySQL you may need the host address, port,
username and password.
● We store the connection in a Connection object: [Link]
● We keep the prepared statements as members: [Link]
Database Connectivity
● We need to check if the file exists.
● The connection variable, conn, is set
to null.
● We will surround the connection
attempt with try / catch.
Database Connectivity
● [Link] loads the class -
in this case the DriverManager
for SQLite.
● If this is the first run, we create
the table.
● We prepare the statements.
● Now, let’s move on to the
methods that use these
statements.
Database Connectivity
● Although we have used “setString”,
there are other functions that set
integers, doubles, etc.
● Study the listing; you will remember
it from CE223: we get a result set
and loop through it until the end.
Database Connectivity
● The main function is updated so
that it includes two more buttons.
● I had to update the Layout, so that
we can see the buttons.
● The ActionListener functions are
given below.
Database Connectivity
● When we run, and click Add first,
then List, we get the list.
Database Connectivity
● Notice that the file is placed at the
“working directory” of the
application; in this case the
Netbeans project directory.
● Also, notice that when I open
[Link] using SQLite Browser, I
can see the original contents of
the table.
SQLite
● Although I’ve printed the list on the console, you have to be able to use Swing
widgets…
● Visit SQLite website for its documentation.
● Also, read this for the appropriate uses for SQLite:
[Link]
References
● SQLite web site - [Link]

You might also like