Java : SQLException (JDBC) with Examples
SQLException (Java SE 21 & JDK 21) with Examples.
You will find code examples on most SQLException methods.
Summary
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 ()
final var e = new SQLException();
System.out.println(e); // java.sql.SQLException
SQLException (String 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)
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)
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)
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)
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)
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)
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 ()
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 ()
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 ()
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 ()
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)
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