![]() |
VOOZH | about |
JDBC is a Java API that allows interaction with databases. JDBC exceptions like SQLException commonly occur due to connection issues, SQL syntax errors or data type mismatches and are handled using standard Java exception handling.
When we communicate with databases ,some problems occur like:
The below code describes how we handle exceptions.
Below are some common exceptions in JDBC
1. SQLException: it is a Checked Exception. it is occur when we have defined wrong sql syntax like Incorrect SQL syntax, Invalid table or column name.
Incorrect SQL syntax:
Example:
SELECT * FORM person; // 'FROM' is misspelled
Error:
java.sql.SQLSyntaxErrorException: Syntax error in SQL statement
Invalid table or column names
Example:
SELECT * FROM perso; // 'person' is misspelled
Error:
java.sql.SQLSyntaxErrorException: Table 'person' doesn't exist
2. ClassNotFoundException: It is a checked exception. This occur when the JDBC driver class is not found.
Example:
Class.forName("com.mysql.cj.jdbc.Driver");
If the MySQL driver is missing from the classpath.
3. SQLTimeoutException: It is a subclass of SQLException. It occurs when a query takes too long and exceeds the configured timeout limit.
Example:
statement.setQueryTimeout(seconds)
Example: The below-described code describes how to handle Jdbc Exception.
Output: