1 20 package org.jahia.admin.database; 21 22 import java.sql.Connection ; 23 import java.sql.DatabaseMetaData ; 24 import java.sql.DriverManager ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.HashMap ; 29 30 31 32 46 public class DatabaseConnection 47 { 48 private static org.apache.log4j.Logger logger = 49 org.apache.log4j.Logger.getLogger(DatabaseConnection.class); 50 51 private Statement theStatement; 52 private Connection theConnection; 53 54 55 56 60 public DatabaseConnection() 61 { 62 } 65 66 67 94 public HashMap databaseTest( String script, 95 String driver, 96 String url, 97 String username, 98 String password, 99 String runtimeSQL, 100 boolean checkUTF8Compliance, 101 boolean createTables) 102 { 103 HashMap hashMap = new HashMap (); 104 hashMap.put("testDatabaseTablesAlreadyExists", Boolean.FALSE); 105 hashMap.put("testDatabaseConnectionError", Boolean.FALSE); 106 hashMap.put("testDatabaseConnectionMessage", ""); 107 108 logger.debug("Trying to open a database connection..."); 110 int testStatus = 2; 111 112 try { 113 databaseOpen(driver, url, username, password); 114 testStatus = 0; 115 } catch (ClassNotFoundException cnfe) { 116 hashMap.put("testDatabaseConnectionError", Boolean.TRUE); hashMap.put("testDatabaseConnectionMessage", "Driver class not found: " + driver + cnfe.getLocalizedMessage()); 118 testStatus = 1; 119 } catch (SQLException sqle) { 120 hashMap.put("testDatabaseConnectionError", Boolean.TRUE); hashMap.put("testDatabaseConnectionMessage", 122 "Error while connecting to the database with url=[" + 123 url + "] user=[" + username + "] password=[" + password + 124 "]: " + 125 sqle.getLocalizedMessage()); 126 testStatus = 2; 127 } 128 129 if(testStatus == 0) 131 { 132 String runtimeTableName = runtimeSQL.substring(runtimeSQL.indexOf("jahia_"), runtimeSQL.indexOf("(")).trim(); 134 String dbProductName = ""; 135 136 logger.debug( "Try to talk with the database using table ["+runtimeTableName+"]..."); 138 139 if (createTables) { 140 databaseQuery( "DROP TABLE " + runtimeTableName, true); 141 142 testStatus = databaseQuery( runtimeSQL.toString(), false); 144 } 145 if ((testStatus == 0) && (checkUTF8Compliance)){ 147 String testField = "Latin : ���� / Cyrillic : \u0419\u0416 / Chineeze : \u8bed\u8a00"; 152 testStatus += databaseQuery("INSERT INTO " + runtimeTableName + 154 "(testfield) VALUES('" + testField + "')", false); 155 try { 156 ResultSet rs = theStatement.executeQuery("SELECT testfield FROM jahia_db_test"); 157 if (rs.next()) { 158 String testFieldResult = rs.getString("testfield"); 159 if (!testFieldResult.equals(testField)) { 160 testStatus++; 161 hashMap.put("testDatabaseConnectionError", Boolean.TRUE); 162 hashMap.put("testDatabaseConnectionMessage", "This database seems to be not UTF-8 compliant"); 163 return hashMap; 164 } 165 } 166 } catch (SQLException sqle) { 167 logger.debug("Error executing query [SELECT testfield FROM jahia_db_test]", sqle); 168 } 169 } 170 171 if (createTables) { 172 testStatus += databaseQuery( "DROP TABLE " + runtimeTableName, false); 173 } 174 175 if(testStatus == 0) { 177 try { 178 DatabaseMetaData dbMetaData = theConnection.getMetaData(); 179 dbProductName = dbMetaData.getDatabaseProductName().trim(); 180 } catch (SQLException sqle) { } 182 183 if(script.equals("sqlserver.script") && !dbProductName.equals("Microsoft SQL Server")) { 185 testStatus = 1; 186 } else if(script.equals("msaccess.script") && !dbProductName.equals("ACCESS")) { 187 testStatus = 1; 188 } else { 189 if(databaseQuery("SELECT * FROM " + runtimeTableName, true) == 0) { hashMap.put("testDatabaseTablesAlreadyExists", Boolean.TRUE); 193 } 194 } 195 } 196 } 197 198 if(testStatus == 0) { 200 logger.debug( "Database responded successfully."); 201 } else { 202 if (!hashMap.containsKey("testDatabaseConnectionError")) { 203 hashMap.put("testDatabaseConnectionError", Boolean.TRUE); 204 } 205 if (!hashMap.containsKey("testDatabaseConnectionMessage")) { 206 hashMap.put("testDatabaseConnectionMessage", 207 "Can't talk with the database. Check your settings."); 208 } 209 } 210 211 return hashMap; 212 } 214 215 216 229 public void databaseOpen( String driver, 230 String url, 231 String username, 232 String password ) 233 throws ClassNotFoundException , SQLException 234 { 235 databaseClose(); 237 238 Class.forName(driver); 240 theConnection = DriverManager.getConnection(url, username, password); 241 theStatement = theConnection.createStatement(); 242 } 244 245 246 254 public int databaseQuery( String sqlCode, boolean quietErrors) 255 { 256 try { 257 theStatement.execute( sqlCode ); 258 return 0; 259 } catch (Exception e) { 260 if (!quietErrors) { 261 logger.debug("Error executing database query [" + sqlCode + "]", e); 262 } 263 return 1; 264 } 265 } 267 268 269 277 public void query( String sqlCode ) 278 throws Exception 279 { 280 theStatement.execute( sqlCode ); 281 } 283 284 285 291 public void databaseClose() 292 { 293 try { 294 theStatement.close(); 295 } catch (SQLException sqle) { 296 } catch (NullPointerException sqle) { 297 } 298 } 300 301 302 308 public Connection getConnection() 309 { 310 return theConnection; 311 } 313 314 315 321 public Statement getStatement() 322 { 323 return theStatement; 324 } 326 327 } | Popular Tags |