Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc / exception / JDBCExecutePreparedSQLException.java @ 40435

History | View | Annotate | Download (2.77 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.store.jdbc.exception;
32

    
33
import java.sql.SQLException;
34
import java.util.List;
35

    
36

    
37
/**
38
 * @author jmvivo
39
 *
40
 */
41
public class JDBCExecutePreparedSQLException extends JDBCException {
42

    
43
        /**
44
         *
45
         */
46
        private static final long serialVersionUID = 634889167216570034L;
47

    
48
        private final static String MESSAGE_FORMAT = "An JDBC exception was throw when execute SQL: '%(sql)' with params %(params)";
49
        private final static String MESSAGE_KEY = "_JDBCExecutePreparedSQLException";
50

    
51
        public JDBCExecutePreparedSQLException(String sql, Object[] parameters,
52
                        SQLException cause) {
53
                super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
54
                setValue("sql", sql);
55
                String params = "{unknow}";
56
                if (parameters != null) {
57

    
58
                        StringBuilder strb = new StringBuilder();
59
                        strb.append('[');
60
                        for (int i = 0; i < parameters.length - 1; i++) {
61
                                strb.append(getParamValue(parameters[i]));
62
                                strb.append(", ");
63
                        }
64
                        strb.append(getParamValue(parameters[parameters.length - 1]));
65
                        strb.append(']');
66
                        params = strb.toString();
67
                }
68
                setValue("params", params);
69
        }
70

    
71
        public JDBCExecutePreparedSQLException(String sql, List parameters,
72
                        SQLException cause) {
73
                super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
74
                setValue("sql", sql);
75
                String params = "{unknow}";
76
                if (parameters != null) {
77

    
78
                        StringBuilder strb = new StringBuilder();
79
                        strb.append('[');
80
                        for (int i = 0; i < parameters.size() - 1; i++) {
81
                                strb.append(getParamValue(parameters.get(i)));
82
                                strb.append(", ");
83
                        }
84
                        strb.append(getParamValue(parameters.get(parameters.size() - 1)));
85
                        strb.append(']');
86
                        params = strb.toString();
87
                }
88
                setValue("params", params);
89
        }
90

    
91
        private String getParamValue(Object param) {
92

    
93
                if (param instanceof String) {
94
                        return "'" + param + "'";
95
                }
96
                if (param == null) {
97
                        return "null";
98
                }
99
                return param.toString();
100
        }
101

    
102
}