import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class DatabaseHandler implements iData { iDatabase database; public DatabaseHandler(iDatabase database) { this.database = database; } public List getEmployees() { List dolgozok = new ArrayList<>(); try { dolgozok = tryGetEmployees(); }catch(SQLException e) { System.err.println("Hiba! A lekérdezés sikertelen!"); System.err.println(e.getMessage()); } return dolgozok; } public List tryGetEmployees() throws SQLException { List dolgozok = new ArrayList<>(); String sql = "select az, nev, telepules, fizetes from dolgozok"; Connection conn = database.connectDb(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { Dolgozo dolgozo = new Dolgozo(); dolgozo.az = rs.getInt("az"); dolgozo.nev = rs.getString("nev"); dolgozo.telepules = rs.getString("telepules"); dolgozo.fizetes = rs.getDouble("fizetes"); dolgozok.add(dolgozo); } database.closeDb(conn); return dolgozok; } public void tryInsertEmployee(String name, String city) throws SQLException { Connection conn = database.connectDb(); String sql = "insert into dolgozok " + "(nev, telepules) values " + "(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, city); pstmt.execute(); database.closeDb(conn); } public void insertEmployee(String name, String city) { try { tryInsertEmployee(name, city); } catch (SQLException ex) { System.err.println("Hiba! A beszúrás sikertelen!"); } } }