Handling Exceptions in JDBC
This program demonstrates handling exceptions in JDBC operations.
Problem Statement
Write a Java program that handles SQLException during database connection.
Source Code
| 1 | import java.sql.*; |
| 2 | public class JDBCExceptionExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq JDBC Exception Example"); |
| 5 | try { |
| 6 | Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db","user","pass"); |
| 7 | } catch(SQLException e) { |
| 8 | System.out.println("Eduinq Exception: Database Connection Failed - "+e.getMessage()); |
| 9 | } |
| 10 | } |
| 11 | } |
Program Output
Eduinq JDBC Exception Example Eduinq Exception: Database Connection Failed - Access denied for user 'user'
Explanation
JDBC operations often throw SQLException. - Try-catch ensures graceful handling of database errors. - Provides meaningful error messages. - In enterprise systems, JDBC exception handling ensures reliable data access. Conclusion: Shows handling of JDBC exceptions.