package jdbc.drink.mapper; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import jdbc.drink.domain.Drink; import dk.au.hum.imv.persistence.db.JDBCConnectionFactory; public class mapperTest { public static Drink getAntalByNavn(String navn) { Drink result = null; Connection con = null; try { con = JDBCConnectionFactory.getInstance().getNewConnection(); String findNavn = "SELECT antal FROM drink WHERE navn = ?"; PreparedStatement prep = con.prepareStatement(findNavn); prep.setString(1, navn); ResultSet res = prep.executeQuery(); while (res.next()) { result = createDrinklistFromResultSet(res); } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCConnectionFactory.getInstance().closeConnection(con); } return result; } private static Drink createDrinklistFromResultSet(ResultSet res) throws SQLException { Drink result = null; String navn = res.getString("drink.navn"); int antal = res.getInt("drink.antal"); long id = res.getLong("drink.id"); result = new Drink(navn, antal, id); return result; } }
<% Drink findDrink = mapperTest.getAntalByNavn("Smirnoff"); %> <p> <%=findDrink%> </p>
SELECT COUNT(*) FROM drink WHERE navn = ?
<% ArrayList<Drink> antal = mapperTest.getAllAntal(); for (Drink drink : antal) { %> <br><p><%=drink.getAntal()%></p> <% } %> <br> <hr> <% Drink findDrink = mapperTest.getAntalByNavn("Smirnoff"); %> <p> <%=findDrink.getAntal()%> </p>
package jdbc.drink.mapper; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import jdbc.drink.domain.Drink; import dk.au.hum.imv.persistence.db.JDBCConnectionFactory; public class mapperTest { public static Drink getAntalByNavn(String navn) { Drink result = null; Connection con = null; try { con = JDBCConnectionFactory.getInstance().getNewConnection(); String findNavn = "SELECT * FROM drink WHERE navn = ?"; PreparedStatement prep = con.prepareStatement(findNavn); prep.setString(1, navn); ResultSet res = prep.executeQuery(); while (res.next()) { result = createDrinklistFromResultSet(res); } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCConnectionFactory.getInstance().closeConnection(con); } return result; } public static ArrayList<Drink> getAllAntal() /// VIRKER IKKE! { ArrayList<Drink> result = new ArrayList<Drink>(); Connection con = null; try { con = JDBCConnectionFactory.getInstance().getNewConnection(); String sql = "SELECT * FROM drink"; PreparedStatement prep = con.prepareStatement(sql); ResultSet res = prep.executeQuery(); while (res.next()) { Drink drink = createDrinklistFromResultSet(res); result.add(drink); } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCConnectionFactory.getInstance().closeConnection(con); } return result; } private static Drink createDrinklistFromResultSet(ResultSet res) throws SQLException { Drink result = null; String navn = res.getString("drink.navn"); int antal = res.getInt("drink.antal"); long id = res.getLong("drink.id"); result = new Drink(navn, antal, id); return result; } }