SQLError.java
上传用户:tanyanyong
上传日期:2013-06-23
资源大小:1355k
文件大小:14k
源码类别:

电子政务应用

开发平台:

MultiPlatform

  1. /*
  2.    Copyright (C) 2002 MySQL AB
  3.       This program is free software; you can redistribute it and/or modify
  4.       it under the terms of the GNU General Public License as published by
  5.       the Free Software Foundation; either version 2 of the License, or
  6.       (at your option) any later version.
  7.       This program is distributed in the hope that it will be useful,
  8.       but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.       GNU General Public License for more details.
  11.       You should have received a copy of the GNU General Public License
  12.       along with this program; if not, write to the Free Software
  13.       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14.  */
  15. package com.mysql.jdbc;
  16. import java.util.Hashtable;
  17. /**
  18.  * SQLError is a utility class that maps MySQL error codes to X/Open error
  19.  * codes as is required by the JDBC spec.
  20.  *
  21.  * @author Mark Matthews
  22.  * @version $Id: SQLError.java,v 1.6.2.4 2004/01/13 16:55:43 mmatthew Exp $
  23.  */
  24. public class SQLError {
  25.     
  26.     public static final String SQL_STATE_TIMEOUT_EXPIRED = "S1T00";
  27.     public static final String SQL_STATE_DRIVER_NOT_CAPABLE = "S1C00";
  28.     public static final String SQL_STATE_ILLEGAL_ARGUMENT = "S1009";
  29.     public static final String SQL_STATE_INVALID_COLUMN_NUMBER = "S1002";
  30.     public static final String SQL_STATE_MEMORY_ALLOCATION_FAILURE = "S1001";
  31.     public static final String SQL_STATE_GENERAL_ERROR = "S1000";
  32.     public static final String SQL_STATE_NO_DEFAULT_FOR_COLUMN = "S0023";
  33.     public static final String SQL_STATE_COLUMN_NOT_FOUND = "S0022";
  34.     public static final String SQL_STATE_COLUMN_ALREADY_EXISTS = "S0021";
  35.     public static final String SQL_STATE_INDEX_NOT_FOUND = "S0012";
  36.     public static final String SQL_STATE_INDEX_ALREADY_EXISTS = "S0011";
  37.     public static final String SQL_STATE_BASE_TABLE_NOT_FOUND = "S0002";
  38.     public static final String SQL_STATE_BASE_TABLE_OR_VIEW_ALREADY_EXISTS = "S0001";
  39.     public static final String SQL_STATE_BASE_TABLE_OR_VIEW_NOT_FOUND = "42S02";
  40.     public static final String SQL_STATE_SYNTAX_ERROR = "42000";
  41.     public static final String SQL_STATE_DATETIME_FIELD_OVERFLOW = "22008";
  42.     public static final String SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE = "22003";
  43.     public static final String SQL_STATE_INSERT_VALUE_LIST_NO_MATCH_COL_LIST = "21S01";
  44.     public static final String SQL_STATE_DIVISION_BY_ZERO = "22012";
  45.     public static final String SQL_STATE_INVALID_AUTH_SPEC = "28000";
  46.     public static final String SQL_STATE_DEADLOCK = "41000";
  47.     public static final String SQL_STATE_CONNECTION_FAIL_DURING_TX = "08007";
  48.     public static final String SQL_STATE_CONNECTION_REJECTED = "08004";
  49.     public static final String SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE = "08001";
  50.     public static final String SQL_STATE_CONNECTION_IN_USE = "08002";
  51.     public static final String SQL_STATE_CONNECTION_NOT_OPEN = "08003";
  52.     public static final String SQL_STATE_WRONG_NO_OF_PARAMETERS = "07001";
  53.     public static final String SQL_STATE_MORE_THAN_ONE_ROW_UPDATED_OR_DELETED = "01S04";
  54.     public static final String SQL_STATE_NO_ROWS_UPDATED_OR_DELETED = "01S03";
  55.     public static final String SQL_STATE_ERROR_IN_ROW = "01S01";
  56.     public static final String SQL_STATE_COMMUNICATION_LINK_FAILURE = "08S01";
  57.     public static final String SQL_STATE_INVALID_CONNECTION_ATTRIBUTE = "01S00";
  58.     public static final String SQL_STATE_PRIVILEGE_NOT_REVOKED = "01006";
  59.     public static final String SQL_STATE_DATE_TRUNCATED = "01004";
  60.     public static final String SQL_STATE_DISCONNECT_ERROR = "01002";
  61.     
  62.     private static Hashtable mysqlToSqlState;
  63.     private static Hashtable sqlStateMessages;
  64.     static {
  65.         sqlStateMessages = new Hashtable();
  66.         sqlStateMessages.put(SQL_STATE_DISCONNECT_ERROR, "Disconnect error");
  67.         sqlStateMessages.put(SQL_STATE_DATE_TRUNCATED, "Data truncated");
  68.         sqlStateMessages.put(SQL_STATE_PRIVILEGE_NOT_REVOKED,
  69.             "Privilege not revoked");
  70.         sqlStateMessages.put(SQL_STATE_INVALID_CONNECTION_ATTRIBUTE,
  71.             "Invalid connection string attribute");
  72.         sqlStateMessages.put(SQL_STATE_ERROR_IN_ROW, "Error in row");
  73.         sqlStateMessages.put(SQL_STATE_NO_ROWS_UPDATED_OR_DELETED,
  74.             "No rows updated or deleted");
  75.         sqlStateMessages.put(SQL_STATE_MORE_THAN_ONE_ROW_UPDATED_OR_DELETED,
  76.             "More than one row updated or deleted");
  77.         sqlStateMessages.put(SQL_STATE_WRONG_NO_OF_PARAMETERS,
  78.             "Wrong number of parameters");
  79.         sqlStateMessages.put(SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE,
  80.             "Unable to connect to data source");
  81.         sqlStateMessages.put(SQL_STATE_CONNECTION_IN_USE, "Connection in use");
  82.         sqlStateMessages.put(SQL_STATE_CONNECTION_NOT_OPEN,
  83.             "Connection not open");
  84.         sqlStateMessages.put(SQL_STATE_CONNECTION_REJECTED,
  85.             "Data source rejected establishment of connection");
  86.         sqlStateMessages.put(SQL_STATE_CONNECTION_FAIL_DURING_TX,
  87.             "Connection failure during transaction");
  88.         sqlStateMessages.put(SQL_STATE_COMMUNICATION_LINK_FAILURE,
  89.             "Communication link failure");
  90.         sqlStateMessages.put(SQL_STATE_INSERT_VALUE_LIST_NO_MATCH_COL_LIST,
  91.             "Insert value list does not match column list");
  92.         sqlStateMessages.put(SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE,
  93.             "Numeric value out of range");
  94.         sqlStateMessages.put(SQL_STATE_DATETIME_FIELD_OVERFLOW,
  95.             "Datetime field overflow");
  96.         sqlStateMessages.put(SQL_STATE_DIVISION_BY_ZERO, "Division by zero");
  97.         sqlStateMessages.put(SQL_STATE_DEADLOCK,
  98.             "Deadlock found when trying to get lock; Try restarting transaction");
  99.         sqlStateMessages.put(SQL_STATE_INVALID_AUTH_SPEC,
  100.             "Invalid authorization specification");
  101.         sqlStateMessages.put(SQL_STATE_SYNTAX_ERROR,
  102.             "Syntax error or access violation");
  103.         sqlStateMessages.put(SQL_STATE_BASE_TABLE_OR_VIEW_NOT_FOUND,
  104.             "Base table or view not found");
  105.         sqlStateMessages.put(SQL_STATE_BASE_TABLE_OR_VIEW_ALREADY_EXISTS,
  106.             "Base table or view already exists");
  107.         sqlStateMessages.put(SQL_STATE_BASE_TABLE_NOT_FOUND,
  108.             "Base table not found");
  109.         sqlStateMessages.put(SQL_STATE_INDEX_ALREADY_EXISTS,
  110.             "Index already exists");
  111.         sqlStateMessages.put(SQL_STATE_INDEX_NOT_FOUND, "Index not found");
  112.         sqlStateMessages.put(SQL_STATE_COLUMN_ALREADY_EXISTS,
  113.             "Column already exists");
  114.         sqlStateMessages.put(SQL_STATE_COLUMN_NOT_FOUND, "Column not found");
  115.         sqlStateMessages.put(SQL_STATE_NO_DEFAULT_FOR_COLUMN,
  116.             "No default for column");
  117.         sqlStateMessages.put(SQL_STATE_GENERAL_ERROR, "General error");
  118.         sqlStateMessages.put(SQL_STATE_MEMORY_ALLOCATION_FAILURE,
  119.             "Memory allocation failure");
  120.         sqlStateMessages.put(SQL_STATE_INVALID_COLUMN_NUMBER,
  121.             "Invalid column number");
  122.         sqlStateMessages.put(SQL_STATE_ILLEGAL_ARGUMENT,
  123.             "Invalid argument value");
  124.         sqlStateMessages.put(SQL_STATE_DRIVER_NOT_CAPABLE, "Driver not capable");
  125.         sqlStateMessages.put(SQL_STATE_TIMEOUT_EXPIRED, "Timeout expired");
  126.         //
  127.         // Map MySQL error codes to X/Open error codes
  128.         //
  129.         mysqlToSqlState = new Hashtable();
  130.         //
  131.         // Communications Errors
  132.         //
  133.         // ER_CON_COUNT_ERROR 1040
  134.         // ER_BAD_HOST_ERROR 1042
  135.         // ER_HANDSHAKE_ERROR 1043
  136.         // ER_UNKNOWN_COM_ERROR 1047
  137.         // ER_IPSOCK_ERROR 1081
  138.         //
  139.         mysqlToSqlState.put(new Integer(1040), SQL_STATE_CONNECTION_REJECTED);
  140.         mysqlToSqlState.put(new Integer(1042), SQL_STATE_CONNECTION_REJECTED);
  141.         mysqlToSqlState.put(new Integer(1043), SQL_STATE_CONNECTION_REJECTED);
  142.         mysqlToSqlState.put(new Integer(1047),
  143.             SQL_STATE_COMMUNICATION_LINK_FAILURE);
  144.         mysqlToSqlState.put(new Integer(1081),
  145.             SQL_STATE_COMMUNICATION_LINK_FAILURE);
  146.         // ER_HOST_IS_BLOCKED 1129
  147.         // ER_HOST_NOT_PRIVILEGED 1130
  148.         mysqlToSqlState.put(new Integer(1129), SQL_STATE_CONNECTION_REJECTED);
  149.         mysqlToSqlState.put(new Integer(1130), SQL_STATE_CONNECTION_REJECTED);
  150.         //
  151.         // Authentication Errors
  152.         //
  153.         // ER_ACCESS_DENIED_ERROR 1045
  154.         //
  155.         mysqlToSqlState.put(new Integer(1045), SQL_STATE_INVALID_AUTH_SPEC);
  156.         //
  157.         // Resource errors
  158.         //
  159.         // ER_CANT_CREATE_FILE 1004
  160.         // ER_CANT_CREATE_TABLE 1005
  161.         // ER_CANT_LOCK 1015
  162.         // ER_DISK_FULL 1021
  163.         // ER_OUT_OF_RESOURCES 1041
  164.         //
  165.         // Out-of-memory errors
  166.         //
  167.         // ER_OUTOFMEMORY 1037
  168.         // ER_OUT_OF_SORTMEMORY 1038
  169.         //
  170.         mysqlToSqlState.put(new Integer(1037),
  171.             SQL_STATE_MEMORY_ALLOCATION_FAILURE);
  172.         mysqlToSqlState.put(new Integer(1038),
  173.             SQL_STATE_MEMORY_ALLOCATION_FAILURE);
  174.         //
  175.         // Syntax Errors
  176.         //
  177.         // ER_PARSE_ERROR 1064
  178.         // ER_EMPTY_QUERY 1065
  179.         //
  180.         mysqlToSqlState.put(new Integer(1064), SQL_STATE_SYNTAX_ERROR);
  181.         mysqlToSqlState.put(new Integer(1065), SQL_STATE_SYNTAX_ERROR);
  182.         //
  183.         // Invalid argument errors
  184.         //
  185.         // ER_WRONG_FIELD_WITH_GROUP 1055
  186.         // ER_WRONG_GROUP_FIELD 1056
  187.         // ER_WRONG_SUM_SELECT 1057
  188.         // ER_TOO_LONG_IDENT 1059
  189.         // ER_DUP_FIELDNAME 1060
  190.         // ER_DUP_KEYNAME 1061
  191.         // ER_DUP_ENTRY 1062
  192.         // ER_WRONG_FIELD_SPEC 1063
  193.         // ER_NONUNIQ_TABLE 1066
  194.         // ER_INVALID_DEFAULT 1067
  195.         // ER_MULTIPLE_PRI_KEY 1068
  196.         // ER_TOO_MANY_KEYS 1069
  197.         // ER_TOO_MANY_KEY_PARTS 1070
  198.         // ER_TOO_LONG_KEY 1071
  199.         // ER_KEY_COLUMN_DOES_NOT_EXIST 1072
  200.         // ER_BLOB_USED_AS_KEY 1073
  201.         // ER_TOO_BIG_FIELDLENGTH 1074
  202.         // ER_WRONG_AUTO_KEY 1075
  203.         // ER_NO_SUCH_INDEX 1082
  204.         // ER_WRONG_FIELD_TERMINATORS 1083
  205.         // ER_BLOBS_AND_NO_TERMINATED 1084
  206.         //
  207.         mysqlToSqlState.put(new Integer(1055), SQL_STATE_ILLEGAL_ARGUMENT);
  208.         mysqlToSqlState.put(new Integer(1056), SQL_STATE_ILLEGAL_ARGUMENT);
  209.         mysqlToSqlState.put(new Integer(1057), SQL_STATE_ILLEGAL_ARGUMENT);
  210.         mysqlToSqlState.put(new Integer(1059), SQL_STATE_ILLEGAL_ARGUMENT);
  211.         mysqlToSqlState.put(new Integer(1060), SQL_STATE_ILLEGAL_ARGUMENT);
  212.         mysqlToSqlState.put(new Integer(1061), SQL_STATE_ILLEGAL_ARGUMENT);
  213.         mysqlToSqlState.put(new Integer(1062), SQL_STATE_ILLEGAL_ARGUMENT);
  214.         mysqlToSqlState.put(new Integer(1063), SQL_STATE_ILLEGAL_ARGUMENT);
  215.         mysqlToSqlState.put(new Integer(1066), SQL_STATE_ILLEGAL_ARGUMENT);
  216.         mysqlToSqlState.put(new Integer(1067), SQL_STATE_ILLEGAL_ARGUMENT);
  217.         mysqlToSqlState.put(new Integer(1068), SQL_STATE_ILLEGAL_ARGUMENT);
  218.         mysqlToSqlState.put(new Integer(1069), SQL_STATE_ILLEGAL_ARGUMENT);
  219.         mysqlToSqlState.put(new Integer(1070), SQL_STATE_ILLEGAL_ARGUMENT);
  220.         mysqlToSqlState.put(new Integer(1071), SQL_STATE_ILLEGAL_ARGUMENT);
  221.         mysqlToSqlState.put(new Integer(1072), SQL_STATE_ILLEGAL_ARGUMENT);
  222.         mysqlToSqlState.put(new Integer(1073), SQL_STATE_ILLEGAL_ARGUMENT);
  223.         mysqlToSqlState.put(new Integer(1074), SQL_STATE_ILLEGAL_ARGUMENT);
  224.         mysqlToSqlState.put(new Integer(1075), SQL_STATE_ILLEGAL_ARGUMENT);
  225.         mysqlToSqlState.put(new Integer(1082), SQL_STATE_ILLEGAL_ARGUMENT);
  226.         mysqlToSqlState.put(new Integer(1083), SQL_STATE_ILLEGAL_ARGUMENT);
  227.         mysqlToSqlState.put(new Integer(1084), SQL_STATE_ILLEGAL_ARGUMENT);
  228.         //
  229.         // ER_WRONG_VALUE_COUNT 1058
  230.         //
  231.         mysqlToSqlState.put(new Integer(1058),
  232.             SQL_STATE_INSERT_VALUE_LIST_NO_MATCH_COL_LIST);
  233.         // ER_CANT_CREATE_DB 1006
  234.         // ER_DB_CREATE_EXISTS 1007
  235.         // ER_DB_DROP_EXISTS 1008
  236.         // ER_DB_DROP_DELETE 1009
  237.         // ER_DB_DROP_RMDIR 1010
  238.         // ER_CANT_DELETE_FILE 1011
  239.         // ER_CANT_FIND_SYSTEM_REC 1012
  240.         // ER_CANT_GET_STAT 1013
  241.         // ER_CANT_GET_WD 1014
  242.         // ER_UNEXPECTED_EOF 1039
  243.         // ER_CANT_OPEN_FILE 1016
  244.         // ER_FILE_NOT_FOUND 1017
  245.         // ER_CANT_READ_DIR 1018
  246.         // ER_CANT_SET_WD 1019
  247.         // ER_CHECKREAD 1020
  248.         // ER_DUP_KEY 1022
  249.         // ER_ERROR_ON_CLOSE 1023
  250.         // ER_ERROR_ON_READ 1024
  251.         // ER_ERROR_ON_RENAME 1025
  252.         // ER_ERROR_ON_WRITE 1026
  253.         // ER_FILE_USED 1027
  254.         // ER_FILSORT_ABORT 1028
  255.         // ER_FORM_NOT_FOUND 1029
  256.         // ER_GET_ERRNO 1030
  257.         // ER_ILLEGAL_HA 1031
  258.         // ER_KEY_NOT_FOUND 1032
  259.         // ER_NOT_FORM_FILE 1033
  260.         // ER_DBACCESS_DENIED_ERROR 1044
  261.         // ER_NO_DB_ERROR 1046
  262.         // ER_BAD_NULL_ERROR 1048
  263.         // ER_BAD_DB_ERROR 1049
  264.         // ER_TABLE_EXISTS_ERROR 1050
  265.         // ER_BAD_TABLE_ERROR 1051
  266.         mysqlToSqlState.put(new Integer(1051),
  267.             SQL_STATE_BASE_TABLE_OR_VIEW_NOT_FOUND);
  268.         // ER_NON_UNIQ_ERROR 1052
  269.         // ER_BAD_FIELD_ERROR 1054
  270.         mysqlToSqlState.put(new Integer(1054), SQL_STATE_COLUMN_NOT_FOUND);
  271.         // ER_TEXTFILE_NOT_READABLE 1085
  272.         // ER_FILE_EXISTS_ERROR 1086
  273.         // ER_LOAD_INFO 1087
  274.         // ER_ALTER_INFO 1088
  275.         // ER_WRONG_SUB_KEY 1089
  276.         // ER_CANT_REMOVE_ALL_FIELDS 1090
  277.         // ER_CANT_DROP_FIELD_OR_KEY 1091
  278.         // ER_INSERT_INFO 1092
  279.         // ER_INSERT_TABLE_USED 1093
  280.         // ER_LOCK_DEADLOCK 1213
  281.         mysqlToSqlState.put(new Integer(1205), SQL_STATE_DEADLOCK);
  282.         mysqlToSqlState.put(new Integer(1213), SQL_STATE_DEADLOCK);
  283.     }
  284.     static String get(String stateCode) {
  285.         return (String) sqlStateMessages.get(stateCode);
  286.     }
  287.     /**
  288.      * Map MySQL error codes to X/Open error codes
  289.      *
  290.      * @param errno the MySQL error code
  291.      *
  292.      * @return the corresponding X/Open error code
  293.      */
  294.     static String mysqlToXOpen(int errno) {
  295.         Integer err = new Integer(errno);
  296.         if (mysqlToSqlState.containsKey(err)) {
  297.             return (String) mysqlToSqlState.get(err);
  298.         } else {
  299.             return SQL_STATE_GENERAL_ERROR;
  300.         }
  301.     }
  302. }