java.sql.SQLException.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(161)

本文整理了Java中java.sql.SQLException.<init>()方法的一些代码示例,展示了SQLException.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SQLException.<init>()方法的具体详情如下:
包路径:java.sql.SQLException
类名称:SQLException
方法名:<init>

SQLException.<init>介绍

[英]Creates an SQLException object. The reason string is set to null, the SQLState string is set to null and the error code is set to 0.
[中]

代码示例

代码示例来源:origin: alibaba/druid

@Override
public ResultSetMetaData getMetaData() throws SQLException {
  if (closed) {
    throw new SQLException("resultSet closed");
  }
  return (MockResultSetMetaData) metaData;
}

代码示例来源:origin: alibaba/druid

@Override
public boolean isBeforeFirst() throws SQLException {
  if (closed) {
    throw new SQLException();
  }
  return false;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
  if (iface.isInstance(this)) {
    return (T) this;
  }
  throw new SQLException("DataSource of type [" + getClass().getName() +
      "] cannot be unwrapped as [" + iface.getName() + "]");
}

代码示例来源:origin: alibaba/druid

public String decode(ConnectionProxy connection, String s) throws SQLException {
  try {
    CharsetConvert charsetConvert = (CharsetConvert) connection.getAttribute(ATTR_CHARSET_CONVERTER);
    return charsetConvert.decode(s);
  } catch (UnsupportedEncodingException e) {
    throw new SQLException(e.getMessage(), e);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Specifying a custom username and password doesn't make sense
 * with a single Connection. Returns the single Connection if given
 * the same username and password; throws a SQLException else.
 */
@Override
public Connection getConnection(String username, String password) throws SQLException {
  if (ObjectUtils.nullSafeEquals(username, getUsername()) &&
      ObjectUtils.nullSafeEquals(password, getPassword())) {
    return getConnection();
  }
  else {
    throw new SQLException("SingleConnectionDataSource does not support custom username and password");
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * PostgreSQL can return null.
 * SAP DB can apparently return empty SQL code.
 * Bug 729170
 */
@Test
public void malformedSqlStateCodes() {
  SQLException sex = new SQLException("Message", null, 1);
  testMalformedSqlStateCode(sex);
  sex = new SQLException("Message", "", 1);
  testMalformedSqlStateCode(sex);
  // One char's not allowed
  sex = new SQLException("Message", "I", 1);
  testMalformedSqlStateCode(sex);
}

代码示例来源:origin: spring-projects/spring-framework

private void checkTranslation(SQLExceptionTranslator sext, int errorCode, Class<?> exClass) {
  SQLException sex = new SQLException("", "", errorCode);
  DataAccessException ex = sext.translate("", "", sex);
  assertTrue(exClass.isInstance(ex));
  assertTrue(ex.getCause() == sex);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void badSqlGrammar() {
  SQLException sex = new SQLException("Message", "42001", 1);
  try {
    throw this.trans.translate("task", sql, sex);
  }
  catch (BadSqlGrammarException ex) {
    // OK
    assertTrue("SQL is correct", sql.equals(ex.getSql()));
    assertTrue("Exception matches", sex.equals(ex.getSQLException()));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void invalidSqlStateCode() {
  SQLException sex = new SQLException("Message", "NO SUCH CODE", 1);
  try {
    throw this.trans.translate("task", sql, sex);
  }
  catch (UncategorizedSQLException ex) {
    // OK
    assertTrue("SQL is correct", sql.equals(ex.getSql()));
    assertTrue("Exception matches", sex.equals(ex.getSQLException()));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCouldNotClose() throws Exception {
  SQLException sqlException = new SQLException("bar");
  given(this.connection.createStatement()).willReturn(this.statement);
  given(this.resultSet.next()).willReturn(false);
  willThrow(sqlException).given(this.resultSet).close();
  willThrow(sqlException).given(this.statement).close();
  willThrow(sqlException).given(this.connection).close();
  RowCountCallbackHandler rcch = new RowCountCallbackHandler();
  this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
  verify(this.connection).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCouldNotGetConnectionForOperationOrExceptionTranslator() throws SQLException {
  SQLException sqlException = new SQLException("foo", "07xxx");
  this.dataSource = mock(DataSource.class);
  given(this.dataSource.getConnection()).willThrow(sqlException);
  JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
  RowCountCallbackHandler rcch = new RowCountCallbackHandler();
  this.thrown.expect(CannotGetJdbcConnectionException.class);
  this.thrown.expect(exceptionCause(sameInstance(sqlException)));
  template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void isCheckedException() {
  assertTrue(ObjectUtils.isCheckedException(new Exception()));
  assertTrue(ObjectUtils.isCheckedException(new SQLException()));
  assertFalse(ObjectUtils.isCheckedException(new RuntimeException()));
  assertFalse(ObjectUtils.isCheckedException(new IllegalArgumentException("")));
  // Any Throwable other than RuntimeException and Error
  // has to be considered checked according to the JLS.
  assertTrue(ObjectUtils.isCheckedException(new Throwable()));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testSqlUpdateEncountersSqlException() throws Exception {
  SQLException sqlException = new SQLException("bad update");
  final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 4";
  given(this.statement.executeUpdate(sql)).willThrow(sqlException);
  given(this.connection.createStatement()).willReturn(this.statement);
  this.thrown.expect(exceptionCause(sameInstance(sqlException)));
  try {
    this.template.update(sql);
  }
  finally {
    verify(this.statement).close();
    verify(this.connection, atLeastOnce()).close();
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testGetFromDataSourceWithSQLException() throws Exception {
  SQLException expectedSQLException = new SQLException();
  DataSource dataSource = mock(DataSource.class);
  given(dataSource.getConnection()).willThrow(expectedSQLException);
  SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource);
  assertIsEmpty(sec);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCouldNotGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException {
  SQLException sqlException = new SQLException("foo", "07xxx");
  this.dataSource = mock(DataSource.class);
  given(this.dataSource.getConnection()).willThrow(sqlException);
  this.template = new JdbcTemplate();
  this.template.setDataSource(this.dataSource);
  this.template.afterPropertiesSet();
  RowCountCallbackHandler rcch = new RowCountCallbackHandler();
  this.thrown.expect(CannotGetJdbcConnectionException.class);
  this.thrown.expect(exceptionCause(sameInstance(sqlException)));
  this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
}

代码示例来源:origin: spring-projects/spring-framework

private void doTest(String sqlState, Class<?> dataAccessExceptionType) {
  SQLException ex = new SQLException(REASON, sqlState);
  SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
  DataAccessException dax = translator.translate(TASK, SQL, ex);
  assertNotNull("Translation must *never* result in a null DataAccessException being returned.", dax);
  assertEquals("Wrong DataAccessException type returned as the result of the translation", dataAccessExceptionType, dax.getClass());
  assertNotNull("The original SQLException must be preserved in the translated DataAccessException", dax.getCause());
  assertSame("The exact same original SQLException must be preserved in the translated DataAccessException", ex, dax.getCause());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void dataTruncationTranslation() {
  SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
  SQLException dataAccessEx = new SQLException("", "", 5);
  DataTruncation dataTruncation = new DataTruncation(1, true, true, 1, 1, dataAccessEx);
  DataAccessResourceFailureException daex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataTruncation);
  assertEquals(dataTruncation, daex.getCause());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testStoredProcedureExceptionTranslator() throws Exception {
  SQLException sqlException = new SQLException(
      "Syntax error or access violation exception", "42000");
  given(callableStatement.execute()).willThrow(sqlException);
  given(connection.prepareCall("{call " + StoredProcedureExceptionTranslator.SQL + "()}")
      ).willReturn(callableStatement);
  StoredProcedureExceptionTranslator sproc = new StoredProcedureExceptionTranslator(dataSource);
  thrown.expect(CustomDataException.class);
  sproc.execute();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testNoSuchStoredProcedure() throws Exception {
  SQLException sqlException = new SQLException(
      "Syntax error or access violation exception", "42000");
  given(callableStatement.execute()).willThrow(sqlException);
  given(connection.prepareCall("{call " + NoSuchStoredProcedure.SQL + "()}")).willReturn(
      callableStatement);
  NoSuchStoredProcedure sproc = new NoSuchStoredProcedure(dataSource);
  thrown.expect(BadSqlGrammarException.class);
  sproc.execute();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void batchExceptionTranslation() {
  SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
  SQLException badSqlEx = new SQLException("", "", 1);
  BatchUpdateException batchUpdateEx = new BatchUpdateException();
  batchUpdateEx.setNextException(badSqlEx);
  BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx);
  assertEquals("SQL", bsgex.getSql());
  assertEquals(badSqlEx, bsgex.getSQLException());
}

相关文章