0% found this document useful (0 votes)
24 views

Lecture 5 - Exception Handling, JDBC, Database connection

The document provides an overview of exception handling in Java, emphasizing the use of try-catch blocks and the importance of managing exceptions for robust program execution. It also covers JDBC (Java Database Connectivity), detailing its role in database interactions and the necessity of a driver for database connections. Additionally, it includes instructions for setting up PostgreSQL and using PreparedStatements to prevent SQL injection.

Uploaded by

tom210979
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)
24 views

Lecture 5 - Exception Handling, JDBC, Database connection

The document provides an overview of exception handling in Java, emphasizing the use of try-catch blocks and the importance of managing exceptions for robust program execution. It also covers JDBC (Java Database Connectivity), detailing its role in database interactions and the necessity of a driver for database connections. Additionally, it includes instructions for setting up PostgreSQL and using PreparedStatements to prevent SQL injection.

Uploaded by

tom210979
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
You are on page 1/ 17

Askar Khaimuldin

Senior-lecturer
[email protected]
Astana IT University
▪ Exception Handling
▪ Try-catch and throw
▪ JDBC
▪ Database connection (PostgreSQL)
▪ An exception (or exceptional event) is a problem that arises during the execution
of a program
▪ Normal flow of the program is disrupted, and the program terminates abnormally,
which is not recommended, therefore, these exceptions are to be handled
▪ Exception handling enables applications to resolve (or handle) exceptions
▪ A program can continue executing as if no problem had been encountered
▪ Robust and fault-tolerant programs
▪ Graceful termination
▪ try-catch blocks are used to handle exceptions
▪ A code which might throw an exception is written inside
try block
▪ System-generated exceptions are automatically thrown
by the Java run-time system (e.g., dividing by zero)
▪ finally block contains the code that absolutely must be
executed after a try block
▪ It is possible to use several catch blocks defining an
exact Exception form
▪ *It is considered as “bad practice” to catch Errors
▪ Use throw keyword to manually throw an exception
▪ If a method throws a checked exception (and does not catch it), then it must declare
the fact in a throws clause

▪ One method can throw different types of Exceptions

▪ Example:
In this example only one user was
added to group, since users[1]
was null and users[2] has the
same name as users[0].

Note that users are checked for


the equality by their name in
equals method of User class
▪ JDBC (Java Database Connectivity) is the Java API that manages connecting to a
database, issuing queries and commands, and handling result sets obtained from the
database
▪ There is no need to install the JDBC API explicitly or include it as a third-party library in
your project, because, by default, it comes with every JDK/JRE
▪ It is present in “java.sql”package
▪ The only thing you need to get started with JDBC is a driver for your specific database
▪ The DriverManager class of the java.sql
package manages different types of JDBC
drivers
▪ This class loads the driver classes
▪ In addition to this whenever a new
connection establishes it chooses and
loads the suitable driver from the
previously loaded ones
1.Define 3.Create
2.Establish
the the 4.Execute 5.Process 6.Close the
the
connection Statement a query the result connection
connection
URL object
▪ For the next example you need PostgreSQL to be installed
▪ Download jdbc driver from https://jdbc.postgresql.org/download.html
▪ Create database ‘SimpleDB’ with only one table users(id, name, surname, gender)
▪ The next important thing will be to add the JDBC drivers to the build path of the project.
▪ To do this, right click on Project folder -> Open module settings, this should open a new window
▪ Go to libraries under Project settings and click on add to locate and add the JDBC jar files to the
project
▪ With PreparedStatement you can create SQL with parameters that are dynamically
passed from the Java program to DBMS
▪ Suppose you need to execute the query "SELECT * from EMP WHERE empno=…"
multiple times for each employee from the array empNumbers[]

Prepared statements are


resilient against SQL
injection
Github repo (click here)
Java: How to Program (Early Objects), 11th Edition, by Paul Deitel and Harvey Deitel,
Pearson
▪ Chapter 11 and 24

You might also like