Exemplo n.º 1
0
 @Override
 public void useDatabase(String alias, String path) {
   File f;
   try {
     f = FileWriterImpl.getInstace().fileCreate(path);
   } catch (IOException ex) {
     Logger.getLogger(PHPGenericWriterImpl.class.getName()).log(Level.SEVERE, null, ex);
     return;
   }
   // get database config
   DatabaseConfig db = DBManagerImpl.getInstace().getDatabaseConfig(alias);
   // include of connection details
   FileWriterImpl.getInstace().writeLine("include '" + db.getConfigFileName() + "';", f);
   // PDO connection to database
   String phpCode =
       "/* Connect to an ODBC database using driver invocation */\n"
           + "$_dsn = 'mysql:dbname=$_db_config_databaseName;host=$_db_config_host';\n"
           + "\n"
           + "try {\n"
           + "    $dbh = new PDO($_dsn, $_db_config_user, $_db_config_password);\n"
           + "} catch (PDOException $e) {\n"
           + "    echo 'Connection failed: ' . $e->getMessage();\n"
           + "}";
   FileWriterImpl.getInstace().writeLine(phpCode, f);
 }
Exemplo n.º 2
0
  @Override
  public void addDatabase(
      String alias, String host, String port, String user, String passsword, String databaseName)
      throws DBManagerException {
    String filename = "config/db_" + alias + ".php";
    header(filename);
    File f;
    try {
      f = FileWriterImpl.getInstace().fileCreate(filename);
    } catch (IOException ex) {
      Logger.getLogger(PHPGenericWriterImpl.class.getName()).log(Level.SEVERE, null, ex);
      return;
    }

    FileWriterImpl.getInstace().writeLine("$_db_config_host=\"" + host + ";", f);
    FileWriterImpl.getInstace().writeLine("$_db_config_port=\"" + port + ";", f);
    FileWriterImpl.getInstace().writeLine("$_db_config_user=\"" + user + ";", f);
    FileWriterImpl.getInstace().writeLine("$_db_config_password=\"" + passsword + ";", f);
    FileWriterImpl.getInstace().writeLine("$_db_config_databaseName=\"" + databaseName + ";", f);
    EOF(filename);

    DatabaseConfig databaseConfig = new DatabaseConfig();
    databaseConfig.setAlias(alias);
    databaseConfig.setConfigFileName(filename);
    databaseConfig.setDatabaseName(databaseName);
    databaseConfig.setPassword(passsword);
    databaseConfig.setPort(port);
    databaseConfig.setURL(host);
    databaseConfig.setUser(user);
    DBManagerImpl.getInstace().addDatabaseConfig(databaseConfig);
  }