1 11 12 17 package com.nilostep.xlsql.jdbc; 18 19 import com.nilostep.xlsql.sql.*; 20 21 import java.sql.*; 22 import java.util.*; 23 24 29 public class xlConnectionMySQL extends xlConnection { 30 32 private String context; 33 34 36 44 public xlConnectionMySQL(String url, 45 Properties info) throws SQLException { 46 try { 47 String sql_url; 48 String sql_database; 49 String sql_user; 50 String sql_password; 51 52 if (info.getProperty("sql.url") != null) { 53 sql_url = info.getProperty("sql.url"); 54 } else { 55 throw new SQLException("xlSQL: MySQL url empty"); 56 } 57 58 if (info.getProperty("sql.database") != null) { 59 sql_database = info.getProperty("sql.database"); 60 } else { 61 throw new SQLException("xlSQL: MySQL database empty"); 62 } 63 64 if (info.getProperty("sql.user") != null) { 65 sql_user = info.getProperty("sql.user"); 66 } else { 67 sql_user = ""; 68 } 69 70 if (info.getProperty("sql.password") != null) { 71 sql_password = info.getProperty("sql.password"); 72 } else { 73 sql_password = ""; 74 } 75 76 context = sql_database; 77 URL = url; 78 w = xlSqlWriterFactory.create("mysql"); 79 80 81 dbCon = DriverManager.getConnection(sql_url, sql_user, sql_password); 85 query = xlSqlSelectFactory.create("mysql", dbCon); 86 startup(); 87 xlsql = xlSqlFactory.create("mysql", datastore, context); 88 89 } catch (Exception e) { 90 throw new SQLException("xlSQL: connect to MySQL " + 91 e.getMessage()); 92 } 93 } 94 95 97 102 protected void finalize() throws Throwable { 103 shutdown(); 104 } 105 106 111 public void shutdown() throws Exception { 112 if (!closed) { 113 logger.info("Executing MySQL clean-up..."); 114 String [] schemas = datastore.getSchemas(); 115 Statement stm = dbCon.createStatement(); 116 stm.execute("USE " + context); 117 118 for (int i = 0; i < schemas.length; i++) { 119 stm.execute("DROP DATABASE " + schemas[i]); 120 } 121 dbCon.close(); 122 closed = true; 123 logger.info("MySQL clean-up done"); 124 } 125 } 126 } | Popular Tags |