Skip to content

Instantly share code, notes, and snippets.

@theresajayne
Created August 19, 2018 12:50
Show Gist options
  • Save theresajayne/9efcdc4a6b11cc27075f98bcc9a009c9 to your computer and use it in GitHub Desktop.
Save theresajayne/9efcdc4a6b11cc27075f98bcc9a009c9 to your computer and use it in GitHub Desktop.
public class ServersDaoImpl implements ServersDao {
@Inject
private Database db;
@Inject
private Logger logger;
@Override
public String[] getServers() {
return new String[0];
}
@Override
public void addServer(String server) {
try (Connection conn = db.openConnection()) {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO " + db.getPrefix() + "servers (servername) VALUES(?)");
stmt.setString(1, server);
stmt.execute();
} catch (SQLException sqle) {
logger.error("Error on AddServer " + sqle);
}
}
@Override
public void delServer(String server) {
try (Connection conn = db.openConnection()) {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM " + db.getPrefix() + "servers WHERE servername = ?)");
stmt.setString(1, server);
stmt.execute();
} catch (SQLException sqle) {
logger.error("Error on DeldServer " + sqle);
}
}
@Override
public List<String> listServers() {
List<String> result = new ArrayList<>();
try (Connection conn = db.openConnection()) {
PreparedStatement stmt = conn.prepareStatement("SELECT servername FROM " + db.getPrefix() + "servers ");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
result.add(rs.getString("servername"));
}
rs.close();
} catch (SQLException sqle) {
logger.error("Error on AddServer " + sqle);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment