svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / ConnectionJDBC.java @ 33286
History | View | Annotate | Download (3.92 KB)
1 | 11971 | caballero | |
---|---|---|---|
2 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
3 | *
|
||
4 | * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * Generalitat Valenciana
|
||
23 | * Conselleria d'Infraestructures i Transport
|
||
24 | * Av. Blasco Ib??ez, 50
|
||
25 | * 46010 VALENCIA
|
||
26 | * SPAIN
|
||
27 | *
|
||
28 | * +34 963862235
|
||
29 | * gvsig@gva.es
|
||
30 | * www.gvsig.gva.es
|
||
31 | *
|
||
32 | * or
|
||
33 | *
|
||
34 | * IVER T.I. S.A
|
||
35 | * Salamanca 50
|
||
36 | * 46005 Valencia
|
||
37 | * Spain
|
||
38 | *
|
||
39 | * +34 963163400
|
||
40 | * dac@iver.es
|
||
41 | */
|
||
42 | |||
43 | package com.iver.cit.gvsig.fmap.drivers; |
||
44 | |||
45 | import java.sql.Connection; |
||
46 | import java.sql.DriverManager; |
||
47 | import java.sql.SQLException; |
||
48 | |||
49 | |||
50 | /**
|
||
51 | * DOCUMENT ME!
|
||
52 | *
|
||
53 | * @author Vicente Caballero Navarro
|
||
54 | */
|
||
55 | public class ConnectionJDBC implements IConnection { |
||
56 | private Connection connection; |
||
57 | 29875 | jmvivo | private String connectionStr; |
58 | private String user; |
||
59 | private String _pw; |
||
60 | 11971 | caballero | |
61 | 33286 | jpiera | // to cache the quote string to avoid make a query each time
|
62 | private String identifierQuoteString = null; |
||
63 | |||
64 | 11971 | caballero | public ConnectionJDBC() {
|
65 | |||
66 | } |
||
67 | |||
68 | public Connection getConnection() { |
||
69 | 29875 | jmvivo | try {
|
70 | // try to getConnection if is closed
|
||
71 | if (connection != null && connection.isClosed()){ |
||
72 | Connection tmpCon = null; |
||
73 | tmpCon = DriverManager.getConnection(connectionStr, user, _pw);
|
||
74 | if (tmpCon != null && !tmpCon.isClosed()){ |
||
75 | connection = tmpCon; |
||
76 | connection.setAutoCommit(false);
|
||
77 | } |
||
78 | } |
||
79 | } catch (SQLException e) { |
||
80 | // FIXME
|
||
81 | e.printStackTrace(); |
||
82 | } |
||
83 | 11971 | caballero | return connection;
|
84 | } |
||
85 | |||
86 | public void close() throws DBException { |
||
87 | try {
|
||
88 | connection.close(); |
||
89 | } catch (SQLException e) { |
||
90 | throw new DBException(e); |
||
91 | } |
||
92 | |||
93 | } |
||
94 | |||
95 | public boolean isClosed() throws DBException { |
||
96 | try {
|
||
97 | return connection.isClosed();
|
||
98 | } catch (SQLException e) { |
||
99 | throw new DBException(e); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public String getCatalogName() throws DBException { |
||
104 | try {
|
||
105 | return connection.getCatalog();
|
||
106 | } catch (SQLException e) { |
||
107 | throw new DBException(e); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | public String getNameServer() throws DBException { |
||
112 | try {
|
||
113 | return connection.getMetaData().getDatabaseProductName();
|
||
114 | } catch (SQLException e) { |
||
115 | throw new DBException(e); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | public String getURL() throws DBException { |
||
120 | try {
|
||
121 | return connection.getMetaData().getURL();
|
||
122 | } catch (SQLException e) { |
||
123 | throw new DBException(e); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | public void setDataConnection(String connectionStr, String user, String _pw) throws DBException { |
||
128 | try {
|
||
129 | connection = DriverManager.getConnection(connectionStr, user, _pw);
|
||
130 | connection.setAutoCommit(false);
|
||
131 | 29875 | jmvivo | this.connectionStr = connectionStr;
|
132 | this.user = user;
|
||
133 | this._pw = _pw;
|
||
134 | 11971 | caballero | } catch (SQLException e) { |
135 | throw new DBException(e); |
||
136 | } |
||
137 | |||
138 | } |
||
139 | |||
140 | 33286 | jpiera | /**
|
141 | *
|
||
142 | * @return the quote string for this gbdms. A empty string is returned if there are any error
|
||
143 | * or if quoting is not supported
|
||
144 | */
|
||
145 | public String getIdentifierQuoteString() { |
||
146 | /* if there is not an error the quote string is cached, if there is an error an empty string
|
||
147 | * is returned to avoid obligate the user to check this method.
|
||
148 | */
|
||
149 | |||
150 | String quote = identifierQuoteString;
|
||
151 | if (quote == null) { |
||
152 | try {
|
||
153 | quote = connection.getMetaData().getIdentifierQuoteString().trim(); |
||
154 | identifierQuoteString = quote; |
||
155 | |||
156 | } catch (SQLException e) { |
||
157 | quote = "";
|
||
158 | } |
||
159 | } |
||
160 | return quote;
|
||
161 | } |
||
162 | |||
163 | 11971 | caballero | public String getTypeConnection() { |
164 | return "jdbc"; |
||
165 | } |
||
166 | } |