Ignore:
Timestamp:
05/04/16 10:52:04 (9 years ago)
Author:
sherbold
Message:
  • MySQL result storage can now work with credential files
  • code documentation
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/MySQLResultStorage.java

    r69 r71  
     1// Copyright 2015 Georg-August-Universit�t G�ttingen, Germany 
     2// 
     3//   Licensed under the Apache License, Version 2.0 (the "License"); 
     4//   you may not use this file except in compliance with the License. 
     5//   You may obtain a copy of the License at 
     6// 
     7//       http://www.apache.org/licenses/LICENSE-2.0 
     8// 
     9//   Unless required by applicable law or agreed to in writing, software 
     10//   distributed under the License is distributed on an "AS IS" BASIS, 
     11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
     12//   See the License for the specific language governing permissions and 
     13//   limitations under the License. 
    114 
    215package de.ugoe.cs.cpdp.eval; 
    316 
     17import java.io.FileInputStream; 
     18import java.io.IOException; 
    419import java.sql.Connection; 
    520import java.sql.DriverManager; 
     
    722import java.sql.SQLException; 
    823import java.sql.Statement; 
     24import java.util.Properties; 
    925 
     26import de.lmu.ifi.dbs.elki.logging.Logging.Level; 
    1027import de.ugoe.cs.util.console.Console; 
    1128 
     29/** 
     30 * <p> 
     31 * Implements a storage of experiment results in a MySQL database. 
     32 * </p> 
     33 *  
     34 * @author Steffen Herbold 
     35 */ 
    1236public class MySQLResultStorage implements IResultStorage { 
     37 
     38    /** 
     39     * Connection to the database 
     40     */ 
    1341    private Connection con = null; 
    14      
     42 
     43    /** 
     44     * <p> 
     45     * Creates a new results storage. Tries to read a properties file mysql.cred located in the 
     46     * working directory. If this file is not found, the default database configuration is used: 
     47     * <ul> 
     48     * <li>dbHost = localhost</li> 
     49     * <li>dbPort = 3306</li> 
     50     * <li>dbName = crosspare</li> 
     51     * <li>dbUser = crosspare</li> 
     52     * <li>dbPass = benchmark</li> 
     53     * </p> 
     54     */ 
    1555    public MySQLResultStorage() { 
    16         this("localhost", "3306", "crosspare", "crosspare", "benchmark"); 
     56        Properties dbProperties = new Properties(); 
     57        try { 
     58            dbProperties.load(new FileInputStream("mysql.cred")); 
     59        } 
     60        catch (IOException e) { 
     61            Console.printerr("Could not load mysql.cred file: " + e.getMessage()); 
     62            Console.printerr("Must be a properties file located in working directory."); 
     63            Console 
     64                .traceln(Level.WARNING, 
     65                         "Using default DB configuration since mysql.cred file could not be loaded"); 
     66        } 
     67        String dbHost = dbProperties.getProperty("db.host", "localhost"); 
     68        String dbPort = dbProperties.getProperty("db.port", "3306"); 
     69        String dbName = dbProperties.getProperty("db.name", "crosspare"); 
     70        String dbUser = dbProperties.getProperty("db.user", "crosspare"); 
     71        String dbPass = dbProperties.getProperty("db.pass", "benchmark"); 
     72        connectToDB(dbHost, dbPort, dbName, dbUser, dbPass); 
    1773    } 
    18      
    19     public MySQLResultStorage(String dbHost, String dbPort, String dbName, String dbUser, String dbPass) { 
     74 
     75    /** 
     76     * <p> 
     77     * Sets up the database connection 
     78     * </p> 
     79     * 
     80     * @param dbHost 
     81     *            host of the database 
     82     * @param dbPort 
     83     *            port of the database 
     84     * @param dbName 
     85     *            name of the database 
     86     * @param dbUser 
     87     *            user of the database 
     88     * @param dbPass 
     89     *            password of the user 
     90     */ 
     91    private void connectToDB(String dbHost, 
     92                             String dbPort, 
     93                             String dbName, 
     94                             String dbUser, 
     95                             String dbPass) 
     96    { 
    2097        try { 
    2198            Class.forName("com.mysql.jdbc.Driver"); 
    2299            con = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + 
    23                     dbName + "?" + "user=" + dbUser + "&" + "password=" + dbPass); 
     100                dbName + "?" + "user=" + dbUser + "&" + "password=" + dbPass); 
    24101        } 
    25102        catch (ClassNotFoundException e) { 
     
    34111    } 
    35112 
     113    /* 
     114     * (non-Javadoc) 
     115     *  
     116     * @see de.ugoe.cs.cpdp.eval.IResultStorage#addResult(de.ugoe.cs.cpdp.eval.ExperimentResult) 
     117     */ 
     118    @Override 
    36119    public void addResult(ExperimentResult result) { 
    37120        StringBuilder sql = new StringBuilder(); 
    38121        sql.append("INSERT INTO crosspare.results VALUES (NULL,"); 
    39         sql.append("\'" + result.getConfigurationName()+"\',"); 
    40         sql.append("\'" + result.getProductName()+"\',"); 
    41         sql.append("\'" + result.getClassifier()+"\',"); 
    42         sql.append(result.getSizeTestData()+","); 
    43         sql.append(result.getSizeTrainingData()+","); 
    44         sql.append(result.getSuccHe()+","); 
    45         sql.append(result.getSuccZi()+","); 
    46         sql.append(result.getSuccG75()+","); 
    47         sql.append(result.getSuccG60()+","); 
    48         sql.append(result.getError()+","); 
    49         sql.append(result.getRecall()+","); 
    50         sql.append(result.getPrecision()+","); 
    51         sql.append(result.getFscore()+","); 
    52         sql.append(result.getGscore()+","); 
    53         sql.append(result.getMcc()+","); 
    54         sql.append(result.getAuc()+","); 
    55         sql.append(result.getAucec()+","); 
    56         sql.append(result.getTpr()+","); 
    57         sql.append(result.getTnr()+","); 
    58         sql.append(result.getFpr()+","); 
    59         sql.append(result.getFnr()+","); 
    60         sql.append(result.getTp()+","); 
    61         sql.append(result.getFn()+","); 
    62         sql.append(result.getTn()+","); 
    63         sql.append(result.getFp()+");"); 
    64          
     122        sql.append("\'" + result.getConfigurationName() + "\',"); 
     123        sql.append("\'" + result.getProductName() + "\',"); 
     124        sql.append("\'" + result.getClassifier() + "\',"); 
     125        sql.append(result.getSizeTestData() + ","); 
     126        sql.append(result.getSizeTrainingData() + ","); 
     127        sql.append(result.getSuccHe() + ","); 
     128        sql.append(result.getSuccZi() + ","); 
     129        sql.append(result.getSuccG75() + ","); 
     130        sql.append(result.getSuccG60() + ","); 
     131        sql.append(result.getError() + ","); 
     132        sql.append(result.getRecall() + ","); 
     133        sql.append(result.getPrecision() + ","); 
     134        sql.append(result.getFscore() + ","); 
     135        sql.append(result.getGscore() + ","); 
     136        sql.append(result.getMcc() + ","); 
     137        sql.append(result.getAuc() + ","); 
     138        sql.append(result.getAucec() + ","); 
     139        sql.append(result.getTpr() + ","); 
     140        sql.append(result.getTnr() + ","); 
     141        sql.append(result.getFpr() + ","); 
     142        sql.append(result.getFnr() + ","); 
     143        sql.append(result.getTp() + ","); 
     144        sql.append(result.getFn() + ","); 
     145        sql.append(result.getTn() + ","); 
     146        sql.append(result.getFp() + ");"); 
     147 
    65148        Statement stmt; 
    66149        try { 
     
    76159        } 
    77160    } 
    78      
     161 
     162    /* 
     163     * (non-Javadoc) 
     164     *  
     165     * @see de.ugoe.cs.cpdp.eval.IResultStorage#containsResult(java.lang.String, java.lang.String) 
     166     */ 
    79167    @Override 
    80168    public boolean containsResult(String experimentName, String productName) { 
    81         String sql = "SELECT COUNT(*) as cnt FROM crosspare.results WHERE configurationName=\'" + experimentName + "\' AND productName=\'" + productName + "\';"; 
     169        String sql = "SELECT COUNT(*) as cnt FROM crosspare.results WHERE configurationName=\'" + 
     170            experimentName + "\' AND productName=\'" + productName + "\';"; 
    82171        Statement stmt; 
    83172        boolean contained = false; 
     
    87176            results.next(); 
    88177            int count = results.getInt("cnt"); 
    89             contained = count>0; 
     178            contained = count > 0; 
    90179        } 
    91180        catch (SQLException e) { 
Note: See TracChangeset for help on using the changeset viewer.