1+ /******************************************************************************************************************
2+ * File: CreateServices.java
3+ * Course: 17655
4+ * Project: Assignment A3
5+ * Copyright: Copyright (c) 2018 Carnegie Mellon University
6+ * Versions:
7+ * 1.0 February 2018 - Initial write of assignment 3 (ajl).
8+ *
9+ * Description: This class provides the concrete implementation of the create micro services. These services run
10+ * in their own process (JVM).
11+ *
12+ * Parameters: None
13+ *
14+ * Internal Methods:
15+ * String newOrder() - creates an order in the ms_orderinfo database from the supplied parameters.
16+ *
17+ * External Dependencies:
18+ * - rmiregistry must be running to start this server
19+ * = MySQL
20+ - orderinfo database
21+ ******************************************************************************************************************/
22+ import java .rmi .RemoteException ;
23+ import java .rmi .server .UnicastRemoteObject ;
24+ import java .rmi .registry .Registry ;
25+ import java .sql .*;
26+
27+ public class CreateServices extends UnicastRemoteObject implements CreateServicesAI
28+ {
29+ // Set up the JDBC driver name and database URL
30+ static final String JDBC_CONNECTOR = "com.mysql.jdbc.Driver" ;
31+ static final String DB_URL = Configuration .getJDBCConnection ();
32+
33+ // Set up the orderinfo database credentials
34+ static final String USER = "root" ;
35+ static final String PASS = Configuration .MYSQL_PASSWORD ;
36+
37+ // Do nothing constructor
38+ public CreateServices () throws RemoteException {}
39+
40+ // Main service loop
41+ public static void main (String args [])
42+ {
43+ // What we do is bind to rmiregistry, in this case localhost, port 1099. This is the default
44+ // RMI port. Note that I use rebind rather than bind. This is better as it lets you start
45+ // and restart without having to shut down the rmiregistry.
46+
47+ try
48+ {
49+ CreateServices obj = new CreateServices ();
50+
51+ Registry registry = Configuration .createRegistry ();
52+ registry .bind ("CreateServices" , obj );
53+
54+ String [] boundNames = registry .list ();
55+ System .out .println ("Registered services:" );
56+ for (String name : boundNames ) {
57+ System .out .println ("\t " + name );
58+ }
59+ // Bind this object instance to the name RetrieveServices in the rmiregistry
60+ // Naming.rebind("//" + Configuration.getRemoteHost() + ":1099/CreateServices", obj);
61+
62+ } catch (Exception e ) {
63+
64+ System .out .println ("CreateServices binding err: " + e .getMessage ());
65+ e .printStackTrace ();
66+ }
67+
68+ } // main
69+
70+
71+ // Inplmentation of the abstract classes in RetrieveServicesAI happens here.
72+
73+ // This method add the entry into the ms_orderinfo database
74+
75+ public String newOrder (String idate , String ifirst , String ilast , String iaddress , String iphone ) throws RemoteException
76+ {
77+ // Local declarations
78+
79+ Connection conn = null ; // connection to the orderinfo database
80+ Statement stmt = null ; // A Statement object is an interface that represents a SQL statement.
81+ String ReturnString = "Order Created" ; // Return string. If everything works you get an 'OK' message
82+ // if not you get an error string
83+ try
84+ {
85+ // Here we load and initialize the JDBC connector. Essentially a static class
86+ // that is used to provide access to the database from inside this class.
87+
88+ Class .forName (JDBC_CONNECTOR );
89+
90+ //Open the connection to the orderinfo database
91+
92+ //System.out.println("Connecting to database...");
93+ conn = DriverManager .getConnection (DB_URL ,USER ,PASS );
94+
95+ // Here we create the queery Execute a query. Not that the Statement class is part
96+ // of the Java.rmi.* package that enables you to submit SQL queries to the database
97+ // that we are connected to (via JDBC in this case).
98+
99+ stmt = conn .createStatement ();
100+
101+ String sql = "INSERT INTO orders(order_date, first_name, last_name, address, phone) VALUES (\" " +idate +"\" ,\" " +ifirst +"\" ,\" " +ilast +"\" ,\" " +iaddress +"\" ,\" " +iphone +"\" )" ;
102+
103+ // execute the update
104+
105+ stmt .executeUpdate (sql );
106+
107+ // clean up the environment
108+
109+ stmt .close ();
110+ conn .close ();
111+ stmt .close ();
112+ conn .close ();
113+
114+ } catch (Exception e ) {
115+
116+ ReturnString = e .toString ();
117+ }
118+
119+ return (ReturnString );
120+
121+ } //retrieve all orders
122+
123+ } // RetrieveServices
0 commit comments