source: trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/MySQLResultStorage.java @ 75

Last change on this file since 75 was 71, checked in by sherbold, 9 years ago
  • MySQL result storage can now work with credential files
  • code documentation
  • Property svn:mime-type set to text/plain
File size: 7.0 KB
Line 
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.
14
15package de.ugoe.cs.cpdp.eval;
16
17import java.io.FileInputStream;
18import java.io.IOException;
19import java.sql.Connection;
20import java.sql.DriverManager;
21import java.sql.ResultSet;
22import java.sql.SQLException;
23import java.sql.Statement;
24import java.util.Properties;
25
26import de.lmu.ifi.dbs.elki.logging.Logging.Level;
27import de.ugoe.cs.util.console.Console;
28
29/**
30 * <p>
31 * Implements a storage of experiment results in a MySQL database.
32 * </p>
33 *
34 * @author Steffen Herbold
35 */
36public class MySQLResultStorage implements IResultStorage {
37
38    /**
39     * Connection to the database
40     */
41    private Connection con = null;
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     */
55    public MySQLResultStorage() {
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);
73    }
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    {
97        try {
98            Class.forName("com.mysql.jdbc.Driver");
99            con = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" +
100                dbName + "?" + "user=" + dbUser + "&" + "password=" + dbPass);
101        }
102        catch (ClassNotFoundException e) {
103            Console.printerr("JDBC driver not found");
104        }
105        catch (SQLException e) {
106            Console.printerr("Problem with MySQL connection: ");
107            Console.printerr("SQLException: " + e.getMessage());
108            Console.printerr("SQLState: " + e.getSQLState());
109            Console.printerr("VendorError: " + e.getErrorCode());
110        }
111    }
112
113    /*
114     * (non-Javadoc)
115     *
116     * @see de.ugoe.cs.cpdp.eval.IResultStorage#addResult(de.ugoe.cs.cpdp.eval.ExperimentResult)
117     */
118    @Override
119    public void addResult(ExperimentResult result) {
120        StringBuilder sql = new StringBuilder();
121        sql.append("INSERT INTO crosspare.results VALUES (NULL,");
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
148        Statement stmt;
149        try {
150            stmt = con.createStatement();
151            stmt.executeUpdate(sql.toString().replace("NaN", "NULL"));
152        }
153        catch (SQLException e) {
154            Console.printerr("Problem with MySQL connection: ");
155            Console.printerr("SQLException: " + e.getMessage());
156            Console.printerr("SQLState: " + e.getSQLState());
157            Console.printerr("VendorError: " + e.getErrorCode());
158            return;
159        }
160    }
161
162    /*
163     * (non-Javadoc)
164     *
165     * @see de.ugoe.cs.cpdp.eval.IResultStorage#containsResult(java.lang.String, java.lang.String)
166     */
167    @Override
168    public boolean containsResult(String experimentName, String productName) {
169        String sql = "SELECT COUNT(*) as cnt FROM crosspare.results WHERE configurationName=\'" +
170            experimentName + "\' AND productName=\'" + productName + "\';";
171        Statement stmt;
172        boolean contained = false;
173        try {
174            stmt = con.createStatement();
175            ResultSet results = stmt.executeQuery(sql);
176            results.next();
177            int count = results.getInt("cnt");
178            contained = count > 0;
179        }
180        catch (SQLException e) {
181            Console.printerr("Problem with MySQL connection: \n");
182            Console.printerr("SQLException: " + e.getMessage() + "\n");
183            Console.printerr("SQLState: " + e.getSQLState() + "\n");
184            Console.printerr("VendorError: " + e.getErrorCode() + "\n");
185        }
186        return contained;
187    }
188}
Note: See TracBrowser for help on using the repository browser.