@Override public boolean checkIfRowExists(int n, double gamma, double C,int total_rows,int training_rows,int run){ try{ String sql = "SELECT id FROM "+this.tablename+" WHERE gamma = ? AND C = ? AND total_rows = ? AND training_rows = ? AND run = ?"; PreparedStatement preparedStatement = (PreparedStatement) conn.prepareStatement(sql); preparedStatement.setDouble(1, gamma); preparedStatement.setDouble(2, C); preparedStatement.setInt(3, total_rows); preparedStatement.setInt(4, training_rows); preparedStatement.setInt(5, run); ResultSet rs = (ResultSet) preparedStatement.executeQuery(); return rs.next(); }catch(SQLException ex){ // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); System.exit(1); } return false; }
/** * Checks if the required database is available * @return True, if the database is available */ private boolean dbIsAvailable() { boolean isDbAvailable = true; String sql = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" + dbName + "'"; ResultSet res = this.getSqlResult4ExecuteQuery(sql); if (res==null) { return false; } else { try { res.last(); if ( res.getRow()==0 ) { isDbAvailable = false; } else { isDbAvailable = true; } } catch (SQLException e) { e.printStackTrace(); this.dbError.setErrNumber( e.getErrorCode() ); this.dbError.setHead( "Fehler beim Auslesen der Datensätze!" ); this.dbError.setText( e.getLocalizedMessage() ); this.dbError.setErr(true); this.dbError.show(); isDbAvailable = false; } } try { res.close(); } catch (SQLException sqle) { sqle.printStackTrace(); } return isDbAvailable; }
/** * This method returns the number of rows from a ResultSet-Object * @param rs * @return Number of rows from a result set, after SQL execution */ public int getRowCount(ResultSet rs){ int numResults = 0; try{ rs.last(); numResults = rs.getRow(); rs.beforeFirst(); } catch(SQLException e){ e.printStackTrace(); } return numResults; }
/** * This method is automatically called when you use a factory or the * constructor <ObjectRelational(ResultSet resultSet)> * * @param resultSet * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException */ public void initialize(ResultSet resultSet) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchMethodException, SecurityException, InvocationTargetException { fields = this.getClass().getDeclaredFields(); for (Field field : fields) { if (!Modifier.isStatic(field.getModifiers())) { String columnName = getColumnName(field.getName()); setFieldValue(field, resultSet.getObject(columnName)); } } }
@SuppressWarnings("unchecked") public List<T> getList(ResultSet resultSet) throws SQLException, IllegalArgumentException, IllegalAccessException, InstantiationException, NoSuchMethodException, SecurityException, InvocationTargetException { List<T> list = new ArrayList<T>(); while (resultSet.next()) { objectRelational = t.newInstance(); objectRelational.initialize(resultSet); list.add((T) objectRelational); } return list; }
public List<String> getTableNames() { List<String> tables = new ArrayList<String>(); try { ResultSet resultSet = (ResultSet) databaseMetaData.getTables(null, schema, "%", null); while (resultSet.next()) { tables.add(resultSet.getString(3)); } } catch (SQLException e) { e.printStackTrace(); } return tables; }
public List<String> getColumnNames(String table) { List<String> columns = new ArrayList<String>(); try { ResultSet resultSet = (ResultSet) databaseMetaData.getColumns(null, schema, table, null); while (resultSet.next()) { columns.add(resultSet.getString("COLUMN_NAME")); } } catch (SQLException e) { e.printStackTrace(); } return columns; }
public List<String> getSQLDataTypeNames(String table) { List<String> columns = new ArrayList<String>(); try { ResultSet resultSet = (ResultSet) databaseMetaData.getColumns(null, schema, table, null); while (resultSet.next()) { columns.add(resultSet.getString("TYPE_NAME")); } } catch (SQLException e) { e.printStackTrace(); } return columns; }
public String getPrimaryKey(String table) { String pk = null; try { ResultSet resultSet = (ResultSet) databaseMetaData.getPrimaryKeys( null, schema, table); if (resultSet.next()) { pk = resultSet.getString("COLUMN_NAME"); } } catch (SQLException e) { e.printStackTrace(); } return pk; }
public Map<String, String> getForeignKeys(String table) { Map<String, String> fks = new HashMap<String, String>(); try { ResultSet resultSet = (ResultSet) databaseMetaData.getExportedKeys( null, schema, table); while (resultSet.next()) { fks.put(resultSet.getString("FKTABLE_NAME"), resultSet.getString("FKCOLUMN_NAME")); } } catch (SQLException e) { e.printStackTrace(); } return fks; }
public ResultSet innerJoin(String tableA, String tableB, String columnA, String columnB) { return join(tableA, tableB, columnA, columnB, "INNER"); }
public ResultSet innerJoin(String tableA, String tableB, String column) { return innerJoin(tableA, tableB, column, column); }
public ResultSet leftJoin(String tableA, String tableB, String columnA, String columnB) { return join(tableA, tableB, columnA, columnB, "LEFT"); }
public ResultSet leftJoin(String tableA, String tableB, String column) { return leftJoin(tableA, tableB, column, column); }
public ResultSet rightJoin(String tableA, String tableB, String columnA, String columnB) { return join(tableA, tableB, columnA, columnB, "RIGHT"); }
public ResultSet rightJoin(String tableA, String tableB, String column) { return rightJoin(tableA, tableB, column, column); }
/** * Returns a ResultSet - Object for a SQL-Statement * @param sqlStmt the SQL statement * @return com.mysql.jdbc.ResultSet */ public ResultSet getSqlResult4ExecuteQuery(String sqlStmt) { return this.getSqlResult4ExecuteQuery(sqlStmt, true); }