public MetadataBlob[] select(String selectString) { PreparedStatement stmt = null; try { stmt = conn.prepareStatement( "select writingKey, mapkey, blobdata from metadatablobs " + selectString + ";"); ResultSet rs = stmt.executeQuery(); List<MetadataBlob> list = new ArrayList<MetadataBlob>(); while (rs.next()) { MetadataBlob f = new MetadataBlob( rs.getString("writingkey"), rs.getString("mapkey"), rs.getString("blobdata")); list.add(f); } return list.toArray(new MetadataBlob[0]); } catch (SQLException sqe) { System.err.println("Error selecting: " + selectString); sqe.printStackTrace(); return null; } finally { if (stmt != null) try { stmt.close(); } catch (SQLException sqe2) { sqe2.printStackTrace(); } } }
private synchronized void init() throws SQLException { if (isClosed) return; // do tables exists? Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(TABLE_NAMES_SELECT_STMT); ArrayList<String> missingTables = new ArrayList(TABLES.keySet()); while (rs.next()) { String tableName = rs.getString("name"); missingTables.remove(tableName); } for (String missingTable : missingTables) { try { Statement createStmt = conn.createStatement(); // System.out.println("Adding table "+ missingTable); createStmt.executeUpdate(TABLES.get(missingTable)); createStmt.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } } }
public RowData[] select() { PreparedStatement stmt = null; try { stmt = conn.prepareStatement(selectStatement()); ResultSet rs = stmt.executeQuery(); List<RowData> list = new ArrayList<>(); while (rs.next()) { String username = rs.getString("name"); String b64string = rs.getString(b64DataName()); list.add(new UserData(username, b64string)); } return list.toArray(new RowData[0]); } catch (SQLException sqe) { sqe.printStackTrace(); return null; } finally { if (stmt != null) try { stmt.close(); } catch (SQLException sqe2) { sqe2.printStackTrace(); } } }
@Override public String getUsername(byte[] encodedKey) { String b64key = Base64.getEncoder().encodeToString(encodedKey); try { try (PreparedStatement preparedStatement = conn.prepareStatement("select name from users where publickey = ? limit 1")) { preparedStatement.setString(1, b64key); ResultSet resultSet = preparedStatement.executeQuery(); boolean next = resultSet.next(); if (!next) return ""; return resultSet.getString(1); } } catch (SQLException sqle) { throw new IllegalStateException(sqle); } }
@Override public byte[] getAllUsernamesGzip() throws IOException { try (PreparedStatement stmt = conn.prepareStatement("select name from users")) { ResultSet rs = stmt.executeQuery(); List<String> list = new ArrayList<>(); while (rs.next()) { String username = rs.getString("name"); list.add(username); } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try (DataOutputStream dout = new DataOutputStream(new GZIPOutputStream(bout))) { for (String uname : list) Serialize.serialize(uname, dout); } return bout.toByteArray(); } catch (SQLException sqe) { throw new IOException(sqe); } }