Java : SQLException (JDBC) with Examples
SQLException (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SQLException methods.
Summary
An exception that provides information on a database access error or other errors.
Note :
- The code examples in this page use Apache Derby as the JDBC driver. Apache Derby was once included in older JDKs, but is not included in the latest JDK.
final var url = "jdbc:derby:memory:db";
try (final var connection = DriverManager.getConnection(url)) {
System.out.println("Connected!");
} catch (SQLException e) {
System.out.println(e);
}
// Result
// ↓
//java.sql.SQLException: Database 'memory:db' not found.
final var url = "jdbc:derby:memory:db;create=true";
try (final var connection = DriverManager.getConnection(url)) {
System.out.println("Connected!");
} catch (SQLException e) {
System.out.println(e);
}
// Result
// ↓
//Connected!
Constructors
SQLException ()
Constructs a SQLException object.
final var e = new SQLException();
System.out.println(e); // java.sql.SQLException
SQLException (String reason)
Constructs a SQLException object with a given reason.
final var e = new SQLException("Reason!");
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
SQLException (String reason, String SQLState)
Constructs a SQLException object with a given reason and SQLState.
final var e = new SQLException("Reason!", "State!");
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
System.out.println(e.getSQLState()); // State!
SQLException (String reason, String SQLState, int vendorCode)
Constructs a SQLException object with a given reason, SQLState and vendorCode.
final var e = new SQLException("Reason!", "State!", 123);
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
System.out.println(e.getSQLState()); // State!
System.out.println(e.getErrorCode()); // 123
SQLException (String reason, String sqlState, int vendorCode, Throwable cause)
Constructs a SQLException object with a given reason, SQLState, vendorCode and cause.
final var cause = new SQLException("XYZ");
final var e = new SQLException("Reason!", "State!", 123, cause);
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
System.out.println(e.getSQLState()); // State!
System.out.println(e.getErrorCode()); // 123
System.out.println(e.getCause()); // java.sql.SQLException: XYZ
SQLException (String reason, String sqlState, Throwable cause)
Constructs a SQLException object with a given reason, SQLState and cause.
final var cause = new SQLException("XYZ");
final var e = new SQLException("Reason!", "State!", cause);
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
System.out.println(e.getSQLState()); // State!
System.out.println(e.getCause()); // java.sql.SQLException: XYZ
SQLException (String reason, Throwable cause)
Constructs a SQLException object with a given reason and cause.
final var cause = new SQLException("XYZ");
final var e = new SQLException("Reason!", cause);
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
System.out.println(e.getCause()); // java.sql.SQLException: XYZ
SQLException (Throwable cause)
Constructs a SQLException object with a given cause.
final var cause = new SQLException("XYZ");
final var e = new SQLException(cause);
System.out.println(e); // java.sql.SQLException: java.sql.SQLException: XYZ
System.out.println(e.getCause()); // java.sql.SQLException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
Methods
int getErrorCode ()
Retrieves the vendor-specific exception code for this SQLException object.
final var e = new SQLException("Reason!", "State!", 123);
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
System.out.println(e.getSQLState()); // State!
System.out.println(e.getErrorCode()); // 123
SQLException getNextException ()
Retrieves the exception chained to this SQLException object by setNextException(SQLException ex).
final var e1 = new SQLException("E1");
System.out.println(e1); // java.sql.SQLException: E1
final var e2 = new SQLException("E2");
System.out.println(e2); // java.sql.SQLException: E2
System.out.println(e1.getNextException()); // null
e1.setNextException(e2);
System.out.println(e1.getNextException()); // java.sql.SQLException: E2
String getSQLState ()
Retrieves the SQLState for this SQLException object.
final var e = new SQLException("Reason!", "State!");
System.out.println(e); // java.sql.SQLException: Reason!
System.out.println(e.getMessage()); // Reason!
System.out.println(e.getSQLState()); // State!
Iterator<Throwable> iterator ()
Returns an iterator over the chained SQLExceptions.
final var e1 = new SQLException("E1");
final var e2 = new SQLException("E2");
final var e3 = new SQLException("E3");
e1.setNextException(e2);
e2.setNextException(e3);
final var it = e1.iterator();
System.out.println("-- iterator --");
while (it.hasNext()) {
final var e = it.next();
System.out.println(e.getMessage());
}
// Result
// ↓
//-- iterator --
//E1
//E2
//E3
void setNextException (SQLException ex)
Adds an SQLException object to the end of the chain.
final var e1 = new SQLException("E1");
System.out.println(e1); // java.sql.SQLException: E1
final var e2 = new SQLException("E2");
System.out.println(e2); // java.sql.SQLException: E2
System.out.println(e1.getNextException()); // null
e1.setNextException(e2);
System.out.println(e1.getNextException()); // java.sql.SQLException: E2
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.
Methods declared in Iterable
Related posts
- API Examples