?? sqlerror.java
字號:
MysqlErrorNumbers.ER_WRONG_NAME_FOR_INDEX), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_WRONG_NAME_FOR_CATALOG), "42000"); mysqlToSql99State.put(new Integer( MysqlErrorNumbers.ER_UNKNOWN_STORAGE_ENGINE), "42000"); } /** * Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances. * * If 'forTruncationOnly' is true, only looks for truncation warnings, and * actually throws DataTruncation as an exception. * * @param connection * the connection to use for getting warnings. * * @return the SQLWarning chain (or null if no warnings) * * @throws SQLException * if the warnings could not be retrieved */ static SQLWarning convertShowWarningsToSQLWarnings(Connection connection) throws SQLException { return convertShowWarningsToSQLWarnings(connection, 0, false); } /** * Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances. * * If 'forTruncationOnly' is true, only looks for truncation warnings, and * actually throws DataTruncation as an exception. * * @param connection * the connection to use for getting warnings. * @param warningCountIfKnown * the warning count (if known), otherwise set it to 0. * @param forTruncationOnly * if this method should only scan for data truncation warnings * * @return the SQLWarning chain (or null if no warnings) * * @throws SQLException * if the warnings could not be retrieved, or if data truncation * is being scanned for and truncations were found. */ static SQLWarning convertShowWarningsToSQLWarnings(Connection connection, int warningCountIfKnown, boolean forTruncationOnly) throws SQLException { java.sql.Statement stmt = null; java.sql.ResultSet warnRs = null; SQLWarning currentWarning = null; try { if (warningCountIfKnown < 100) { stmt = connection.createStatement(); if (stmt.getMaxRows() != 0) { stmt.setMaxRows(0); } } else { // stream large warning counts stmt = connection.createStatement( java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(Integer.MIN_VALUE); } /* * +---------+------+---------------------------------------------+ | * Level | Code | Message | * +---------+------+---------------------------------------------+ | * Warning | 1265 | Data truncated for column 'field1' at row 1 | * +---------+------+---------------------------------------------+ */ warnRs = stmt.executeQuery("SHOW WARNINGS"); //$NON-NLS-1$ while (warnRs.next()) { int code = warnRs.getInt("Code"); //$NON-NLS-1$ if (forTruncationOnly) { if (code == 1265 || code == 1264) { DataTruncation newTruncation = new MysqlDataTruncation( warnRs.getString("Message"), 0, false, false, 0, 0); //$NON-NLS-1$ if (currentWarning == null) { currentWarning = newTruncation; } else { currentWarning.setNextWarning(newTruncation); } } } else { String level = warnRs.getString("Level"); //$NON-NLS-1$ String message = warnRs.getString("Message"); //$NON-NLS-1$ SQLWarning newWarning = new SQLWarning(message, SQLError .mysqlToSqlState(code, connection .getUseSqlStateCodes()), code); if (currentWarning == null) { currentWarning = newWarning; } else { currentWarning.setNextWarning(newWarning); } } } if (forTruncationOnly && (currentWarning != null)) { throw currentWarning; } return currentWarning; } finally { SQLException reThrow = null; if (warnRs != null) { try { warnRs.close(); } catch (SQLException sqlEx) { reThrow = sqlEx; } } if (stmt != null) { try { stmt.close(); } catch (SQLException sqlEx) { // ideally, we'd use chained exceptions here, // but we still support JDK-1.2.x with this driver // which doesn't have them.... reThrow = sqlEx; } } if (reThrow != null) { throw reThrow; } } } public static void dumpSqlStatesMappingsAsXml() throws Exception { TreeMap allErrorNumbers = new TreeMap(); Map mysqlErrorNumbersToNames = new HashMap(); Integer errorNumber = null; // // First create a list of all 'known' error numbers that // are mapped. // for (Iterator mysqlErrorNumbers = mysqlToSql99State.keySet().iterator(); mysqlErrorNumbers .hasNext();) { errorNumber = (Integer) mysqlErrorNumbers.next(); allErrorNumbers.put(errorNumber, errorNumber); } for (Iterator mysqlErrorNumbers = mysqlToSqlState.keySet().iterator(); mysqlErrorNumbers .hasNext();) { errorNumber = (Integer) mysqlErrorNumbers.next(); allErrorNumbers.put(errorNumber, errorNumber); } // // Now create a list of the actual MySQL error numbers we know about // java.lang.reflect.Field[] possibleFields = MysqlErrorNumbers.class .getDeclaredFields(); for (int i = 0; i < possibleFields.length; i++) { String fieldName = possibleFields[i].getName(); if (fieldName.startsWith("ER_")) { mysqlErrorNumbersToNames.put(possibleFields[i].get(null), fieldName); } } System.out.println("<ErrorMappings>"); for (Iterator allErrorNumbersIter = allErrorNumbers.keySet().iterator(); allErrorNumbersIter .hasNext();) { errorNumber = (Integer) allErrorNumbersIter.next(); String sql92State = mysqlToSql99(errorNumber.intValue()); String oldSqlState = mysqlToXOpen(errorNumber.intValue()); System.out.println(" <ErrorMapping mysqlErrorNumber=\"" + errorNumber + "\" mysqlErrorName=\"" + mysqlErrorNumbersToNames.get(errorNumber) + "\" legacySqlState=\"" + ((oldSqlState == null) ? "" : oldSqlState) + "\" sql92SqlState=\"" + ((sql92State == null) ? "" : sql92State) + "\"/>"); } System.out.println("</ErrorMappings>"); } static String get(String stateCode) { return (String) sqlStateMessages.get(stateCode); } private static String mysqlToSql99(int errno) { Integer err = new Integer(errno); if (mysqlToSql99State.containsKey(err)) { return (String) mysqlToSql99State.get(err); } return "HY000"; } /** * Map MySQL error codes to X/Open or SQL-92 error codes * * @param errno * the MySQL error code * * @return the corresponding X/Open or SQL-92 error code */ static String mysqlToSqlState(int errno, boolean useSql92States) { if (useSql92States) { return mysqlToSql99(errno); } return mysqlToXOpen(errno); } private static String mysqlToXOpen(int errno) { Integer err = new Integer(errno); if (mysqlToSqlState.containsKey(err)) { return (String) mysqlToSqlState.get(err); } return SQL_STATE_GENERAL_ERROR; } /* * SQL State Class SQLNonTransientException Subclass 08 * SQLNonTransientConnectionException 22 SQLDataException 23 * SQLIntegrityConstraintViolationException N/A * SQLInvalidAuthorizationException 42 SQLSyntaxErrorException * * SQL State Class SQLTransientException Subclass 08 * SQLTransientConnectionException 40 SQLTransactionRollbackException N/A * SQLTimeoutException */ public static SQLException createSQLException(String message, String sqlState) { if (sqlState != null) { if (sqlState.startsWith("08")) { return new MySQLNonTransientConnectionException(message, sqlState); } if (sqlState.startsWith("22")) { return new MySQLDataException(message, sqlState); } if (sqlState.startsWith("23")) { return new MySQLIntegrityConstraintViolationException(message, sqlState); } if (sqlState.startsWith("42")) { return new MySQLSyntaxErrorException(message, sqlState); } if (sqlState.startsWith("40")) { return new MySQLTransactionRollbackException(message, sqlState); } } return new SQLException(message, sqlState); } public static SQLException createSQLException(String message) { return new SQLException(message); } public static SQLException createSQLException(String message, String sqlState, int vendorErrorCode) { if (sqlState != null) { if (sqlState.startsWith("08")) { return new MySQLNonTransientConnectionException(message, sqlState, vendorErrorCode); } if (sqlState.startsWith("22")) { return new MySQLDataException(message, sqlState, vendorErrorCode); } if (sqlState.startsWith("23")) { return new MySQLIntegrityConstraintViolationException(message, sqlState, vendorErrorCode); } if (sqlState.startsWith("42")) { return new MySQLSyntaxErrorException(message, sqlState, vendorErrorCode); } if (sqlState.startsWith("40")) { return new MySQLTransactionRollbackException(message, sqlState, vendorErrorCode); } } return new SQLException(message, sqlState, vendorErrorCode); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -