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

Last change on this file since 90 was 86, checked in by sherbold, 9 years ago
  • switched workspace encoding to UTF-8 and fixed broken characters
  • Property svn:mime-type set to text/plain
File size: 7.3 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            Properties connectionProperties = new Properties();
99            connectionProperties.put("user", dbUser);
100            connectionProperties.put("password", dbPass);
101            connectionProperties.put("autoReconnect", "true");
102            connectionProperties.put("maxReconnects", "10000");
103            Class.forName("com.mysql.jdbc.Driver");
104            con = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" +
105                dbName, connectionProperties);
106        }
107        catch (ClassNotFoundException e) {
108            Console.printerr("JDBC driver not found");
109        }
110        catch (SQLException e) {
111            Console.printerr("Problem with MySQL connection: ");
112            Console.printerr("SQLException: " + e.getMessage());
113            Console.printerr("SQLState: " + e.getSQLState());
114            Console.printerr("VendorError: " + e.getErrorCode());
115        }
116    }
117
118    /*
119     * (non-Javadoc)
120     *
121     * @see de.ugoe.cs.cpdp.eval.IResultStorage#addResult(de.ugoe.cs.cpdp.eval.ExperimentResult)
122     */
123    @Override
124    public void addResult(ExperimentResult result) {
125        StringBuilder sql = new StringBuilder();
126        sql.append("INSERT INTO crosspare.results VALUES (NULL,");
127        sql.append("\'" + result.getConfigurationName() + "\',");
128        sql.append("\'" + result.getProductName() + "\',");
129        sql.append("\'" + result.getClassifier() + "\',");
130        sql.append(result.getSizeTestData() + ",");
131        sql.append(result.getSizeTrainingData() + ",");
132        sql.append(result.getSuccHe() + ",");
133        sql.append(result.getSuccZi() + ",");
134        sql.append(result.getSuccG75() + ",");
135        sql.append(result.getSuccG60() + ",");
136        sql.append(result.getError() + ",");
137        sql.append(result.getRecall() + ",");
138        sql.append(result.getPrecision() + ",");
139        sql.append(result.getFscore() + ",");
140        sql.append(result.getGscore() + ",");
141        sql.append(result.getMcc() + ",");
142        sql.append(result.getAuc() + ",");
143        sql.append(result.getAucec() + ",");
144        sql.append(result.getTpr() + ",");
145        sql.append(result.getTnr() + ",");
146        sql.append(result.getFpr() + ",");
147        sql.append(result.getFnr() + ",");
148        sql.append(result.getTp() + ",");
149        sql.append(result.getFn() + ",");
150        sql.append(result.getTn() + ",");
151        sql.append(result.getFp() + ");");
152
153        Statement stmt;
154        try {
155            stmt = con.createStatement();
156            stmt.executeUpdate(sql.toString().replace("NaN", "NULL"));
157        }
158        catch (SQLException e) {
159            Console.printerr("Problem with MySQL connection: ");
160            Console.printerr("SQLException: " + e.getMessage());
161            Console.printerr("SQLState: " + e.getSQLState());
162            Console.printerr("VendorError: " + e.getErrorCode());
163            return;
164        }
165    }
166
167    /*
168     * (non-Javadoc)
169     *
170     * @see de.ugoe.cs.cpdp.eval.IResultStorage#containsResult(java.lang.String, java.lang.String)
171     */
172    @Override
173    public boolean containsResult(String experimentName, String productName) {
174        String sql = "SELECT COUNT(*) as cnt FROM crosspare.results WHERE configurationName=\'" +
175            experimentName + "\' AND productName=\'" + productName + "\';";
176        Statement stmt;
177        boolean contained = false;
178        try {
179            stmt = con.createStatement();
180            ResultSet results = stmt.executeQuery(sql);
181            results.next();
182            int count = results.getInt("cnt");
183            contained = count > 0;
184        }
185        catch (SQLException e) {
186            Console.printerr("Problem with MySQL connection: \n");
187            Console.printerr("SQLException: " + e.getMessage() + "\n");
188            Console.printerr("SQLState: " + e.getSQLState() + "\n");
189            Console.printerr("VendorError: " + e.getErrorCode() + "\n");
190        }
191        return contained;
192    }
193}
Note: See TracBrowser for help on using the repository browser.