📄 databaseexampletable.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.example;import edu.udo.cs.yale.tools.DatabaseHandler;import java.util.List;import java.sql.ResultSet;import java.sql.SQLException;public class DatabaseExampleTable extends ExampleTable { private DatabaseHandler databaseHandler; private String tableName; private int numOfColumns; private int size; private DatabaseExampleTable(List attributes, DatabaseHandler databaseHandler, ResultSet resultSet, String tableName, int numOfColumns) throws SQLException { super(attributes); this.databaseHandler = databaseHandler; this.tableName = tableName; this.numOfColumns = numOfColumns; this.size = 0; //ResultSet resultSet = databaseHandler.query("select * from " + tableName); int numberOfAttributes = attributes.size(); double[] minimum = new double[numberOfAttributes]; double[] maximum = new double[numberOfAttributes]; double[] average = new double[numberOfAttributes]; for (int n = 0; n < numberOfAttributes; n++) { minimum[n] = Double.POSITIVE_INFINITY; maximum[n] = Double.NEGATIVE_INFINITY; average[n] = 0.0d; } while (resultSet.next()) { for (int n = 0; n < numberOfAttributes; n++) { Attribute attribute = (Attribute)attributes.get(n); double value = DatabaseDataRow.readColumn(resultSet, attribute); if (!Double.isNaN(value)) { if (minimum[n] > value) minimum[n] = value; if (maximum[n] < value) maximum[n] = value; average[n] += value; } } size++; } for (int n = 0; n < numberOfAttributes; n++) { average[n] /= (double)size; Attribute attribute = (Attribute)attributes.get(n); attribute.setMinimum(minimum[n]); attribute.setMaximum(maximum[n]); attribute.setAverage(average[n]); } //resultSet.close(); } public static DatabaseExampleTable createDatabaseExampleTable(DatabaseHandler databaseHandler, String tableName) throws SQLException { ResultSet resultSet = databaseHandler.query("select * from " + tableName); List attributes = DatabaseHandler.createAttributes(resultSet); DatabaseExampleTable table = new DatabaseExampleTable(attributes, databaseHandler, resultSet, tableName, resultSet.getMetaData().getColumnCount()); resultSet.close(); return table; } public DataRowReader getDataReader() { try { String query = "SELECT "; boolean first = true; for (int i = 0; i < getNumberOfAttributes(); i++) { Attribute a = getAttribute(i); if (a != null) { if (!first) query += ", "; first = false; query += "\""+DatabaseHandler.getDatabaseName(a)+"\""; } } query += " from "+tableName; return new DatabaseDataRowReader(getAttributes(), databaseHandler.query(query)); } catch (SQLException e) { throw new RuntimeException("Error while creating database datarowreader: "+e, e); } } public int addAttribute(Attribute attribute) { int index = super.addAttribute(attribute); if (databaseHandler == null) return index; try { databaseHandler.addColumn(attribute, tableName); } catch (SQLException e) { throw new RuntimeException("Error while adding a column to database: " + e, e); } return index; } public void removeAttribute(Attribute attribute) { super.removeAttribute(attribute); try { databaseHandler.removeColumn(attribute, tableName); } catch (SQLException e) { throw new RuntimeException("Error while removing a column from database: " + e, e); } } public int getSize() { return size; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -