Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. package at.fhooe.mc.vis;
  2.  
  3. import java.rmi.RemoteException;
  4. import java.rmi.server.UnicastRemoteObject;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.Properties;
  12. import java.util.Vector;
  13.  
  14. /**
  15.  * The BeerServer. Stores different kinds of beer in a DB and manages them.
  16.  */
  17. public class BeerServer extends UnicastRemoteObject implements RMIBeerInterface {
  18.  
  19.     private static String USERNAME = "mc";
  20.     private static String PASSWORD = "mc";
  21.     private static String SERVERNAME = "localhost";
  22.     private static String PORT = "3306";
  23.     private static String DBNAME = "beerdb";
  24.  
  25.     private static final long serialVersionUID = 1L;
  26.     private Connection m_conn;
  27.  
  28.     /**
  29.      * Constructor
  30.      *
  31.      * @throws RemoteException
  32.      */
  33.     protected BeerServer() throws RemoteException {
  34.         super();
  35.         m_conn = null;
  36.         connect();
  37.     }
  38.  
  39.     protected void connect() {
  40.         Properties connectionProperties = new Properties();
  41.         connectionProperties.put("user", USERNAME);
  42.         connectionProperties.put("password", PASSWORD);
  43.  
  44.         try {
  45.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  46.         } catch (Exception _e) {
  47.             _e.printStackTrace();
  48.         }
  49.  
  50.         try {
  51.             // connect
  52.             m_conn = DriverManager.getConnection("jdbc:mysql://" + SERVERNAME
  53.                     + ":" + PORT + "/" + DBNAME, connectionProperties);
  54.  
  55.         } catch (SQLException e) {
  56.             e.printStackTrace();
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * Returns a String array containing all BeerType Object's names in the
  62.      * storage.
  63.      */
  64.     @Override
  65.     public String[] requestBeerTypes() throws RemoteException {
  66.         Statement stmt;
  67.         String[] output = null;
  68.         Vector<String> buffer = new Vector<String>();
  69.         try {
  70.             stmt = m_conn.createStatement();
  71.  
  72.             // query db
  73.             ResultSet result = stmt.executeQuery("SELECT name FROM beers");
  74.  
  75.             // process result
  76.             while (result.next()) {
  77.                 buffer.add(result.getString(1));
  78.             }
  79.         } catch (SQLException e) {
  80.             e.printStackTrace();
  81.         }
  82.        
  83.         return buffer.toArray(new String[buffer.size()]);
  84.     }
  85.  
  86.     /**
  87.      * Returns how many crates of the requested BeerType are available.
  88.      */
  89.     @Override
  90.     public int requestContingent(String _type) throws RemoteException {
  91.         int output = -1;
  92.         Statement stmt;
  93.         try {
  94.             stmt = m_conn.createStatement();
  95.  
  96.             // query db
  97.             ResultSet result = stmt
  98.                     .executeQuery("SELECT amount FROM beers WHERE name = \""
  99.                             + _type + "\"");
  100.             result.next();
  101.             output = result.getInt(1);
  102.  
  103.         } catch (SQLException e) {
  104.             e.printStackTrace();
  105.         }
  106.         return output;
  107.     }
  108.  
  109.     /**
  110.      * Returns the price of the requested BeerType.
  111.      */
  112.     @Override
  113.     public double requestPrice(String _type) throws RemoteException {
  114.         float output = (float) -1.0;
  115.         Statement stmt;
  116.         try {
  117.             stmt = m_conn.createStatement();
  118.  
  119.             // query db
  120.             ResultSet result = stmt
  121.                     .executeQuery("SELECT price FROM beers WHERE name = \""
  122.                             + _type + "\"");
  123.             result.next();
  124.             output = result.getFloat(1);
  125.  
  126.         } catch (SQLException e) {
  127.             e.printStackTrace();
  128.         }
  129.         return output;
  130.     }
  131.  
  132.     public void close() {
  133.         try {
  134.             m_conn.close();
  135.         } catch (SQLException e) {
  136.             e.printStackTrace();
  137.         }
  138.     }
  139.     /**
  140.      * Fills the Server with some data.
  141.      */
  142.     /*
  143.      * private void createDummyData() { addBeer(new BeerType("Stiegl", 20,
  144.      * (float) 15.3)); addBeer(new BeerType("Freistaedter", 10, (float) 12.3));
  145.      * addBeer(new BeerType("Zwettler", 15, (float) 14.3)); addBeer(new
  146.      * BeerType("Ottakringer", 20, (float) 13.3)); addBeer(new
  147.      * BeerType("Zipfer", 18, (float) 16.3));
  148.      *
  149.      * }
  150.      */
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement