Java : SQLException (JDBC) con ejemplos
SQLException (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de SQLException.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Una excepción que proporciona información sobre un error de acceso a la base de datos u otros errores. (Traducción automática)
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 ()
Construye un objeto SQLException. (Traducción automática)
final var e = new SQLException();
System.out.println(e); // java.sql.SQLException
SQLException (String reason)
Construye un objeto SQLException con un motivo determinado. (Traducción automática)
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)
Construye un objeto SQLException con un motivo y SQLState determinados. (Traducción automática)
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)
Construye un objeto SQLException con un motivo determinado, SQLState y sellerCode. (Traducción automática)
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)
Construye un objeto SQLException con un motivo determinado, SQLState, sellerCode y causa. (Traducción automática)
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)
Construye un objeto SQLException con un motivo, SQLState y causa determinados. (Traducción automática)
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)
Construye un objeto SQLException con un motivo y una causa determinados. (Traducción automática)
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)
Construye un objeto SQLException con una causa determinada. (Traducción automática)
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 ()
Recupera el código de excepción específico del proveedor para este objeto SQLException. (Traducción automática)
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 ()
Recupera la excepción encadenada a este objeto SQLException mediante setNextException(SQLException ex). (Traducción automática)
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 ()
Recupera el SQLState para este objeto SQLException. (Traducción automática)
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 ()
Devuelve un iterador sobre las SQLExceptions encadenadas. (Traducción automática)
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)
Agrega un objeto SQLException al final de la cadena. (Traducción automática)
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
Consulte el siguiente enlace.
Methods declared in Iterable
Related posts
- Ejemplos de API