package at.fhooe.mc.vis; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Properties; import java.util.Vector; /** * The BeerServer. Stores different kinds of beer in a DB and manages them. */ public class BeerServer extends UnicastRemoteObject implements RMIBeerInterface { private static String USERNAME = "mc"; private static String PASSWORD = "mc"; private static String SERVERNAME = "localhost"; private static String PORT = "3306"; private static String DBNAME = "beerdb"; private static final long serialVersionUID = 1L; private Connection m_conn; /** * Constructor * * @throws RemoteException */ protected BeerServer() throws RemoteException { super(); m_conn = null; connect(); } protected void connect() { Properties connectionProperties = new Properties(); connectionProperties.put("user", USERNAME); connectionProperties.put("password", PASSWORD); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception _e) { _e.printStackTrace(); } try { // connect m_conn = DriverManager.getConnection("jdbc:mysql://" + SERVERNAME + ":" + PORT + "/" + DBNAME, connectionProperties); } catch (SQLException e) { e.printStackTrace(); } } /** * Returns a String array containing all BeerType Object's names in the * storage. */ @Override public String[] requestBeerTypes() throws RemoteException { Statement stmt; String[] output = null; Vector buffer = new Vector(); try { stmt = m_conn.createStatement(); // query db ResultSet result = stmt.executeQuery("SELECT name FROM beers"); // process result while (result.next()) { buffer.add(result.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } return buffer.toArray(new String[buffer.size()]); } /** * Returns how many crates of the requested BeerType are available. */ @Override public int requestContingent(String _type) throws RemoteException { int output = -1; Statement stmt; try { stmt = m_conn.createStatement(); // query db ResultSet result = stmt .executeQuery("SELECT amount FROM beers WHERE name = \"" + _type + "\""); result.next(); output = result.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return output; } /** * Returns the price of the requested BeerType. */ @Override public double requestPrice(String _type) throws RemoteException { float output = (float) -1.0; Statement stmt; try { stmt = m_conn.createStatement(); // query db ResultSet result = stmt .executeQuery("SELECT price FROM beers WHERE name = \"" + _type + "\""); result.next(); output = result.getFloat(1); } catch (SQLException e) { e.printStackTrace(); } return output; } public void close() { try { m_conn.close(); } catch (SQLException e) { e.printStackTrace(); } } /** * Fills the Server with some data. */ /* * private void createDummyData() { addBeer(new BeerType("Stiegl", 20, * (float) 15.3)); addBeer(new BeerType("Freistaedter", 10, (float) 12.3)); * addBeer(new BeerType("Zwettler", 15, (float) 14.3)); addBeer(new * BeerType("Ottakringer", 20, (float) 13.3)); addBeer(new * BeerType("Zipfer", 18, (float) 16.3)); * * } */ }