1 package org.jerpa.component.mysql; 2 3 8 9 import java.sql.*; 10 import org.jerpa.component.*; 11 import org.jerpa.component.configuration.*; 12 import org.jerpa.component.logger.*; 13 import org.jerpa.util.*; 14 import org.jerpa.component.api.*; 15 16 public class MySQLComponent extends Component 17 implements SQLConnector 18 { 19 LoggerComponent logger = null; 20 21 Connection connection = null; 22 23 public MySQLComponent() 24 { 25 setName("MySQL Connector Component"); 26 setRole("org.jerpa.component.SQLConnector"); 27 setDesc("This is a connector for the MySQL database."); 28 setAuthor("Michael Kleinhenz"); 29 setCopyright("The JERPA Team. This is GNU Software."); 30 setVersion(new Version(0,0,1)); 31 setServiceable(new Boolean (true)); 32 33 configuration.addStringItem(new StringConfigurationItem("host","localhost")); 35 configuration.addStringItem(new StringConfigurationItem("database","jerpa")); 36 configuration.addStringItem(new StringConfigurationItem("user","dbuser")); 37 configuration.addStringItem(new StringConfigurationItem("password","secret")); 38 }; 39 40 public void init(ComponentManager _manager) 41 { 42 componentManager = _manager; 43 try 46 { 47 logger = (LoggerComponent)componentManager.lookup("org.jerpa.component.Logger"); 48 } 49 catch (ComponentException e) 50 { 51 System.out.println(e.getMessage()); 52 }; 53 54 logger.log(this,"Init called."); 56 57 try 59 { 60 Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 61 } 62 catch (Exception e) 63 { 64 logger.log(this,"Can't load database driver: " + e.getMessage()); 65 return; 66 } 67 68 try 70 { 71 String url = "jdbc:mysql" 72 + "://" + configuration.getStringItem("host").getValue() 73 + "/" + configuration.getStringItem("database").getValue() 74 + "?user=" + configuration.getStringItem("user").getValue() 75 + "&password=" + configuration.getStringItem("password").getValue(); 76 77 connection = DriverManager.getConnection(url); 78 } 79 catch (ConfigurationException e) 80 { 81 logger.log(this,"Can't load establish connection: " + e.getMessage()); 82 return; 83 } 84 catch (SQLException e) 85 { 86 logger.log(this,"Can't load establish connection: " + e.getMessage()); 87 return; 88 } 89 }; 90 91 public void finalize() 92 { 93 logger.log(this,"Finalize called."); 95 try 97 { 98 connection.close(); 99 } 100 catch (SQLException e) 101 { 102 logger.log(this,"Can't close connection: " + e.getMessage()); 103 } 104 }; 105 106 public Configuration getConfiguration() 107 { 108 return configuration; 109 }; 110 111 public void query(String _query) 112 { 113 logger.log(this,"Query called: " + _query); 114 }; 115 }; 116 117 118 | Popular Tags |