Lab 02
Lab 02
Lab 02
JAVA TECHNOLOGY
(503111)
LAB 2
LAB OBJECTIVES
Use the Java Database Connectivity API (JDBC) - a JAVA standard library that allows connecting and
working with database systems. The Microsoft SQL Server database is used for example purposes in
this lab (other database systems can also be used in a similar way). After completing these exercises,
students will know how to use JDBC to connect and interact with mssql databases, perform basic
operations such as reading data, adding/inserting data, updating or delete data in the database.
JDBC SETUP
To use the JDBC API, we first need to download the driver corresponding to the database system we
want to use. These drivers are usually in the form of *.jar files, they can be downloaded at the
database vendor's website (Microsoft, Oracle, Mysql, etc).
• Once you have downloaded the *.jar files, you need to include them in the classpath of
your java project. There are different ways to do this, if you work with Eclipse you can
create a folder named libs, then copy the *.jar libraries into this folder and add them to
the classpath by selecting the library , right-click and choose Build Path --> Add to Build
Path (Figure 1).
• The second way to include the jdbc library to the project is to use the Maven dependency.
To do this, you need to create a java maven project (on Eclipse or Intellij Idea) then copy
the <dependency> script of the jdbc version corresponding to the database into the
pom.xml file (Figure 2).
Figure 1. Add *.jar files to classpath of a java project using Eclipse IDE
Figure 2. Setup mssql-jdbc dependency in a maven project with Intellij Idea tool
https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
To connect to a database server in JDBC, a connection string needs to be specified. This connection
string contains important information of the server such as: hostname/ip of the server, username
and password to login to the server, the name of the database to work with and some other necessary
information.
jdbc:sqlserver://hostname;user=admin;password=111111
com.microsoft.sqlserver.jdbc.SQL
MS SQL ;databaseName=Lab02;encrypt=true;trustServerCertificat
ServerDriver
e=true;
Assuming microsoft sql server is selected to make the connection, then the program to connect to
mssql server will be written similar to the following:
In case of successful connection, nothing happens. If the connection fails, an exception with details
will be printed to the console.
2. Make queries
Execute a statement with arguments using a Prepared Statement object. Although the statement does not return
the data of the table, we also need to know how many rows have been added/deleted/updated as a result
PRACTICAL EXERCISES
Write a java program that uses jdbc to connect to any database system (mysql or ms sqlserver). The
program must accept the connection url from the command line arguments, you must not hardcode
the url in the source code. This connection url contains all the information to the server and the
necessary login information.
When the connection is successful, the program performs the following initial functions:
Next, the program displays a menu for the user to choose the functions they want to. The user enters
the number to select the function he/she want to run, the program executes that function, then
prints the results to the console (if any) and then display the original menu again until the user selects
the "exit" menu item to stop the program. The list of functions that the program supports includes:
1. Read product list
2. Read a product by input id
3. Add a new product, the result is the product id (auto increment)
4. Update a product
5. Delete a product
6. Exit