View Javadoc

1   package salto.tool.sql;
2   
3   import java.io.File;
4   import java.net.URL;
5   import java.net.URLClassLoader;
6   import java.sql.Connection;
7   import java.sql.Driver;
8   import java.sql.PreparedStatement;
9   import java.sql.ResultSet;
10  import java.sql.ResultSetMetaData;
11  import java.sql.SQLException;
12  import java.sql.Statement;
13  import java.util.ArrayList;
14  import java.util.HashMap;
15  import java.util.Properties;
16  
17  /***
18   * 
19   * @author : E. Loiez
20   */
21  public class ConnectionMgr {
22  	private static HashMap map = new HashMap();
23  
24  	/***
25  	 * Constructor for ConnectionMgr.
26  	 */
27  	public ConnectionMgr() {
28  	}
29  
30  	public static void remove(ConnectionInfo connInfo) {
31  		try {
32  			disconnect(connInfo);
33  		} catch (Exception e) {
34  		}
35  		map.remove(connInfo.getName());
36  	}
37  
38  	public static void disconnectAll() {
39  		String[] connKeys = (String[]) map.keySet().toArray(new String[map.size()]);
40  		for (int i = 0; i < connKeys.length; i++) {
41  			ConnectionInfo ci = (ConnectionInfo) map.remove(connKeys[i]);
42  			try {
43  				disconnect(ci);
44  			} catch (SQLException e) { // ignor�
45  			}
46  		}
47  	}
48  
49  	public static void disconnect(ConnectionInfo connInfo) throws SQLException {
50  		Connection conn = connInfo.getConn();
51  		if (conn != null && !conn.isClosed()) {
52  			conn.close();
53  		}
54  	}
55  
56  	public static void connect(ConnectionInfo connInfo) throws SQLException, DatabaseException {
57  		Connection conn = connInfo.getConn();
58  		ClassLoader loader = null;
59  		if (conn == null || conn.isClosed()) {
60  			Class cl = null;
61  			try {
62  				if (connInfo.getDriverJarFile() != null && !"".equals(connInfo.getDriverJarFile())) {
63  					loader = new URLClassLoader(new URL[] { new File(connInfo.getDriverJarFile()).toURL() });
64  				}
65  				if (loader != null) {
66  					cl = Class.forName(connInfo.getDriver(), true, loader);
67  				} else {
68  					cl = Class.forName(connInfo.getDriver());
69  				}
70  			} catch (Exception e) {
71  				throw new DatabaseException("The driver '" + connInfo.getDriver() + "' cannot be found in the classpath !");
72  				// the driver has not yet been set
73  			}
74  			// some driver needs to be explicitly registered
75  			// but some drivers do not have an implicit constructor
76  			try {
77  				Driver driver = (Driver) cl.newInstance();
78  				Properties props = new Properties();
79  				if (connInfo.getUser() != null && !"".equals(connInfo.getUser())) {
80  					props.put("user", connInfo.getUser());
81  					props.put("password", connInfo.getPwd());
82  				}
83  				conn = driver.connect(connInfo.getUrl(), props);
84  				connInfo.setConn(conn);
85  			} catch (Exception e) {
86  				throw new DatabaseException("Error while connecting the database : " + e);
87  			}
88  			map.put(connInfo.getName(), connInfo);
89  		}
90  	}
91  
92  	public static final Object[][] retrieve(Connection conn, String query) throws SQLException {
93  		Statement stmt = null;
94  		ResultSet rs = null;
95  		try {
96  			stmt = conn.createStatement();
97  			rs = stmt.executeQuery(query);
98  
99  			if (rs == null) {
100 				return null;
101 			}
102 			ResultSetMetaData rsmd = rs.getMetaData();
103 			int nbCol = rsmd.getColumnCount();
104 
105 			String[] colNames = new String[nbCol];
106 			for (int i = nbCol; i >= 1; i--)
107 				colNames[i - 1] = rsmd.getColumnName(i);
108 
109 			ArrayList vResult = new ArrayList();
110 			while (rs.next()) {
111 				Object[] obj = new Object[nbCol];
112 				for (int j = nbCol; j > 0; j--)
113 					obj[j - 1] = rs.getObject(j);
114 				vResult.add(obj);
115 			}
116 			// conversion en tableaux
117 			Object[][] res = new Object[vResult.size() + 1][nbCol];
118 			res[0] = colNames;
119 			for (int i = 0; i < vResult.size(); i++)
120 				res[i + 1] = (Object[]) vResult.get(i);
121 			return res;
122 
123 		} finally // code de nettoyage ex�cut� quel que soit le r�sultat de la
124 					// clause try
125 		{
126 			try {
127 				if (stmt != null)
128 					stmt.close();
129 				if (rs != null)
130 					rs.close();
131 				// if (conn != null) conn.close();
132 			} catch (Exception e) {
133 			}
134 		}
135 	}
136 
137 	public static final int update(Connection conn, String query) throws SQLException {
138 
139 		Statement stmt = null;
140 
141 		try {
142 			stmt = conn.createStatement();
143 			return stmt.executeUpdate(query);
144 		} catch (SQLException e) {
145 			try {
146 				conn.rollback();
147 			} catch (Exception ex) {
148 			}
149 			throw e;
150 		} finally {
151 			try {
152 				if (stmt != null)
153 					stmt.close();
154 			} catch (Exception e) {
155 				System.out.println("smt close");
156 			}
157 		}
158 	}
159 
160 	public static final int update(Connection conn, String query, Object[][] datas) throws SQLException {
161 
162 		PreparedStatement stmt = null;
163 
164 		int nbUpdate = 0;
165 		try {
166 			stmt = conn.prepareStatement(query);
167 			for (int i = 0; i < datas.length; i++) {
168 				for (int j = 0; j < datas[i].length; j++) {
169 					stmt.setObject(j + 1, datas[i][j]);
170 				}
171 				nbUpdate += stmt.executeUpdate();
172 			}
173 			return nbUpdate;
174 		} catch (SQLException e) {
175 			try {
176 				conn.rollback();
177 			} catch (Exception ex) {
178 			}
179 			throw e;
180 		} finally {
181 			try {
182 				if (stmt != null)
183 					stmt.close();
184 			} catch (Exception e) {
185 				System.out.println("smt close");
186 			}
187 		}
188 	}
189 
190 }