mirror of
https://github.com/postgres/postgres.git
synced 2025-08-30 06:01:21 +03:00
pgjindent jdbc files. First time jdbc files were formatted.
This commit is contained in:
@@ -9,280 +9,340 @@ import org.omg.CosNaming.*;
|
||||
*
|
||||
* It has no GUI, just a text frontend to keep it simple.
|
||||
*
|
||||
* $Id: StockClient.java,v 1.1 1999/01/25 21:22:03 scrappy Exp $
|
||||
* $Id: StockClient.java,v 1.2 2001/10/25 05:59:58 momjian Exp $
|
||||
*/
|
||||
public class StockClient
|
||||
{
|
||||
org.omg.CosNaming.NamingContext nameService;
|
||||
|
||||
stock.StockDispenser dispenser;
|
||||
stock.StockItem item;
|
||||
|
||||
BufferedReader in;
|
||||
|
||||
public StockClient(String[] args) {
|
||||
try {
|
||||
// We need this for our IO
|
||||
in = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
// Initialize the orb
|
||||
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
|
||||
|
||||
// Get a reference to the Naming Service
|
||||
org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService");
|
||||
if(nameServiceObj==null) {
|
||||
System.err.println("nameServiceObj == null");
|
||||
return;
|
||||
}
|
||||
|
||||
nameService = org.omg.CosNaming.NamingContextHelper.narrow(nameServiceObj);
|
||||
if(nameService==null) {
|
||||
System.err.println("nameService == null");
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve the dispenser
|
||||
NameComponent[] dispName = {
|
||||
new NameComponent("StockDispenser","Stock")
|
||||
};
|
||||
dispenser = stock.StockDispenserHelper.narrow(nameService.resolve(dispName));
|
||||
if(dispenser==null) {
|
||||
System.err.println("dispenser == null");
|
||||
return;
|
||||
}
|
||||
|
||||
// Now run the front end.
|
||||
run();
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new StockClient(args);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
// First reserve a StockItem
|
||||
try {
|
||||
item = dispenser.reserveItem();
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
mainMenu();
|
||||
|
||||
// finally free the StockItem
|
||||
try {
|
||||
dispenser.releaseItem(item);
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private void mainMenu() {
|
||||
boolean run=true;
|
||||
while(run) {
|
||||
System.out.println("\nCORBA Stock System\n");
|
||||
System.out.println(" 1 Display stock item");
|
||||
System.out.println(" 2 Remove item from stock");
|
||||
System.out.println(" 3 Put item into stock");
|
||||
System.out.println(" 4 Order item");
|
||||
System.out.println(" 5 Display all items");
|
||||
System.out.println(" 0 Exit");
|
||||
int i = getMenu("Main",5);
|
||||
switch(i)
|
||||
org.omg.CosNaming.NamingContext nameService;
|
||||
|
||||
stock.StockDispenser dispenser;
|
||||
stock.StockItem item;
|
||||
|
||||
BufferedReader in;
|
||||
|
||||
public StockClient(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
case 0:
|
||||
run=false;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
displayItem();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bookOut();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
bookIn();
|
||||
break;
|
||||
|
||||
case 4:
|
||||
order(0);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
displayAll();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void displayItem() {
|
||||
try {
|
||||
int id = getMenu("\nStockID to display",item.getLastID());
|
||||
if(id>0) {
|
||||
item.fetchItem(id);
|
||||
System.out.println("========================================");
|
||||
|
||||
String status = "";
|
||||
if(!item.isItemValid())
|
||||
status=" ** Superceded **";
|
||||
|
||||
int av = item.getAvailable();
|
||||
|
||||
System.out.println(" Stock ID: "+id+status+
|
||||
"\nItems Available: "+av+
|
||||
"\nItems on order: "+item.getOrdered()+
|
||||
"\n Description: "+item.getDescription());
|
||||
System.out.println("========================================");
|
||||
|
||||
if(av>0)
|
||||
if(yn("Take this item out of stock?")) {
|
||||
int rem=1;
|
||||
if(av>1)
|
||||
rem=getMenu("How many?",av);
|
||||
if(rem>0)
|
||||
item.removeStock(rem);
|
||||
}
|
||||
|
||||
}
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void bookOut() {
|
||||
try {
|
||||
int id = getMenu("\nStockID to take out",item.getLastID());
|
||||
if(id>0) {
|
||||
item.fetchItem(id);
|
||||
int av = item.getAvailable();
|
||||
if(av>0)
|
||||
if(yn("Take this item out of stock?")) {
|
||||
int rem=1;
|
||||
if(av>1)
|
||||
rem=getMenu("How many?",av);
|
||||
if(rem>0)
|
||||
item.removeStock(rem);
|
||||
}
|
||||
else {
|
||||
System.out.println("This item is not in stock.");
|
||||
int order = item.getOrdered();
|
||||
if(order>0)
|
||||
System.out.println("There are "+item.getOrdered()+" items on order.");
|
||||
else {
|
||||
if(item.isItemValid()) {
|
||||
System.out.println("You will need to order some more "+item.getDescription());
|
||||
order(id);
|
||||
} else
|
||||
System.out.println("This item is now obsolete");
|
||||
// We need this for our IO
|
||||
in = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
// Initialize the orb
|
||||
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
|
||||
|
||||
// Get a reference to the Naming Service
|
||||
org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService");
|
||||
if (nameServiceObj == null)
|
||||
{
|
||||
System.err.println("nameServiceObj == null");
|
||||
return ;
|
||||
}
|
||||
}
|
||||
} else
|
||||
System.out.println(item.getDescription()+"\nThis item is out of stock");
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// book an item into stock
|
||||
private void bookIn() {
|
||||
try {
|
||||
int id = getMenu("\nStockID to book in",item.getLastID());
|
||||
item.fetchItem(id);
|
||||
System.out.println(item.getDescription());
|
||||
|
||||
if(item.getOrdered()>0) {
|
||||
int am = getMenu("How many do you want to book in",item.getOrdered());
|
||||
if(am>0)
|
||||
item.addNewStock(am);
|
||||
} else
|
||||
System.out.println("You don't have any of this item on ordered");
|
||||
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Order an item
|
||||
private void order(int id) {
|
||||
try {
|
||||
if(id==0)
|
||||
id = getMenu("\nStockID to order",item.getLastID());
|
||||
item.fetchItem(id);
|
||||
System.out.println(item.getDescription());
|
||||
int am = getMenu("How many do you want to order",999);
|
||||
if(am>0)
|
||||
item.orderStock(am);
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void displayAll() {
|
||||
try {
|
||||
boolean cont=true;
|
||||
int nr=item.getLastID();
|
||||
String header = "\nId\tAvail\tOrdered\tDescription";
|
||||
System.out.println(header);
|
||||
for(int i=1;i<=nr && cont;i++) {
|
||||
item.fetchItem(i);
|
||||
System.out.println(""+i+"\t"+item.getAvailable()+"\t"+item.getOrdered()+"\t"+item.getDescription());
|
||||
if((i%20)==0) {
|
||||
if((cont=yn("Continue?")))
|
||||
System.out.println(header);
|
||||
|
||||
nameService = org.omg.CosNaming.NamingContextHelper.narrow(nameServiceObj);
|
||||
if (nameService == null)
|
||||
{
|
||||
System.err.println("nameService == null");
|
||||
return ;
|
||||
}
|
||||
|
||||
// Resolve the dispenser
|
||||
NameComponent[] dispName = {
|
||||
new NameComponent("StockDispenser", "Stock")
|
||||
};
|
||||
dispenser = stock.StockDispenserHelper.narrow(nameService.resolve(dispName));
|
||||
if (dispenser == null)
|
||||
{
|
||||
System.err.println("dispenser == null");
|
||||
return ;
|
||||
}
|
||||
|
||||
// Now run the front end.
|
||||
run();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private int getMenu(String title,int max) {
|
||||
int v=-1;
|
||||
while(v<0 || v>max) {
|
||||
System.out.print(title);
|
||||
System.out.print(" [0-"+max+"]: ");
|
||||
System.out.flush();
|
||||
try {
|
||||
v = Integer.parseInt(in.readLine());
|
||||
} catch(Exception nfe) {
|
||||
v=-1;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
new StockClient(args);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
private boolean yn(String title) {
|
||||
try {
|
||||
while(true) {
|
||||
System.out.print(title);
|
||||
System.out.flush();
|
||||
String s = in.readLine();
|
||||
if(s.startsWith("y") || s.startsWith("Y"))
|
||||
return true;
|
||||
if(s.startsWith("n") || s.startsWith("N"))
|
||||
return false;
|
||||
}
|
||||
} catch(Exception nfe) {
|
||||
System.out.println(nfe.toString());
|
||||
nfe.printStackTrace();
|
||||
System.exit(1);
|
||||
|
||||
public void run()
|
||||
{
|
||||
// First reserve a StockItem
|
||||
try
|
||||
{
|
||||
item = dispenser.reserveItem();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
mainMenu();
|
||||
|
||||
// finally free the StockItem
|
||||
try
|
||||
{
|
||||
dispenser.releaseItem(item);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private void mainMenu()
|
||||
{
|
||||
boolean run = true;
|
||||
while (run)
|
||||
{
|
||||
System.out.println("\nCORBA Stock System\n");
|
||||
System.out.println(" 1 Display stock item");
|
||||
System.out.println(" 2 Remove item from stock");
|
||||
System.out.println(" 3 Put item into stock");
|
||||
System.out.println(" 4 Order item");
|
||||
System.out.println(" 5 Display all items");
|
||||
System.out.println(" 0 Exit");
|
||||
int i = getMenu("Main", 5);
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
run = false;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
displayItem();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bookOut();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
bookIn();
|
||||
break;
|
||||
|
||||
case 4:
|
||||
order(0);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
displayAll();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void displayItem()
|
||||
{
|
||||
try
|
||||
{
|
||||
int id = getMenu("\nStockID to display", item.getLastID());
|
||||
if (id > 0)
|
||||
{
|
||||
item.fetchItem(id);
|
||||
System.out.println("========================================");
|
||||
|
||||
String status = "";
|
||||
if (!item.isItemValid())
|
||||
status = " ** Superceded **";
|
||||
|
||||
int av = item.getAvailable();
|
||||
|
||||
System.out.println(" Stock ID: " + id + status +
|
||||
"\nItems Available: " + av +
|
||||
"\nItems on order: " + item.getOrdered() +
|
||||
"\n Description: " + item.getDescription());
|
||||
System.out.println("========================================");
|
||||
|
||||
if (av > 0)
|
||||
if (yn("Take this item out of stock?"))
|
||||
{
|
||||
int rem = 1;
|
||||
if (av > 1)
|
||||
rem = getMenu("How many?", av);
|
||||
if (rem > 0)
|
||||
item.removeStock(rem);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void bookOut()
|
||||
{
|
||||
try
|
||||
{
|
||||
int id = getMenu("\nStockID to take out", item.getLastID());
|
||||
if (id > 0)
|
||||
{
|
||||
item.fetchItem(id);
|
||||
int av = item.getAvailable();
|
||||
if (av > 0)
|
||||
if (yn("Take this item out of stock?"))
|
||||
{
|
||||
int rem = 1;
|
||||
if (av > 1)
|
||||
rem = getMenu("How many?", av);
|
||||
if (rem > 0)
|
||||
item.removeStock(rem);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("This item is not in stock.");
|
||||
int order = item.getOrdered();
|
||||
if (order > 0)
|
||||
System.out.println("There are " + item.getOrdered() + " items on order.");
|
||||
else
|
||||
{
|
||||
if (item.isItemValid())
|
||||
{
|
||||
System.out.println("You will need to order some more " + item.getDescription());
|
||||
order(id);
|
||||
}
|
||||
else
|
||||
System.out.println("This item is now obsolete");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
System.out.println(item.getDescription() + "\nThis item is out of stock");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// book an item into stock
|
||||
private void bookIn()
|
||||
{
|
||||
try
|
||||
{
|
||||
int id = getMenu("\nStockID to book in", item.getLastID());
|
||||
item.fetchItem(id);
|
||||
System.out.println(item.getDescription());
|
||||
|
||||
if (item.getOrdered() > 0)
|
||||
{
|
||||
int am = getMenu("How many do you want to book in", item.getOrdered());
|
||||
if (am > 0)
|
||||
item.addNewStock(am);
|
||||
}
|
||||
else
|
||||
System.out.println("You don't have any of this item on ordered");
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Order an item
|
||||
private void order(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id == 0)
|
||||
id = getMenu("\nStockID to order", item.getLastID());
|
||||
item.fetchItem(id);
|
||||
System.out.println(item.getDescription());
|
||||
int am = getMenu("How many do you want to order", 999);
|
||||
if (am > 0)
|
||||
item.orderStock(am);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void displayAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
boolean cont = true;
|
||||
int nr = item.getLastID();
|
||||
String header = "\nId\tAvail\tOrdered\tDescription";
|
||||
System.out.println(header);
|
||||
for (int i = 1;i <= nr && cont;i++)
|
||||
{
|
||||
item.fetchItem(i);
|
||||
System.out.println("" + i + "\t" + item.getAvailable() + "\t" + item.getOrdered() + "\t" + item.getDescription());
|
||||
if ((i % 20) == 0)
|
||||
{
|
||||
if ((cont = yn("Continue?")))
|
||||
System.out.println(header);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private int getMenu(String title, int max)
|
||||
{
|
||||
int v = -1;
|
||||
while (v < 0 || v > max)
|
||||
{
|
||||
System.out.print(title);
|
||||
System.out.print(" [0-" + max + "]: ");
|
||||
System.out.flush();
|
||||
try
|
||||
{
|
||||
v = Integer.parseInt(in.readLine());
|
||||
}
|
||||
catch (Exception nfe)
|
||||
{
|
||||
v = -1;
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
private boolean yn(String title)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
System.out.print(title);
|
||||
System.out.flush();
|
||||
String s = in.readLine();
|
||||
if (s.startsWith("y") || s.startsWith("Y"))
|
||||
return true;
|
||||
if (s.startsWith("n") || s.startsWith("N"))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception nfe)
|
||||
{
|
||||
System.out.println(nfe.toString());
|
||||
nfe.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -13,105 +13,122 @@ import java.sql.*;
|
||||
* that an object could be changed by another client, and we need to ensure that
|
||||
* the returned data is live and accurate.
|
||||
*
|
||||
* $Id: StockDB.java,v 1.2 2000/04/26 05:32:01 peter Exp $
|
||||
* $Id: StockDB.java,v 1.3 2001/10/25 05:59:58 momjian Exp $
|
||||
*/
|
||||
public class StockDB
|
||||
{
|
||||
Connection con;
|
||||
Statement st;
|
||||
|
||||
// the current stock number
|
||||
int id = -1;
|
||||
|
||||
public void connect(String url,String usr,String pwd) throws Exception {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
System.out.println("Connecting to "+url);
|
||||
con = DriverManager.getConnection(url,usr,pwd);
|
||||
st = con.createStatement();
|
||||
}
|
||||
|
||||
public void closeConnection() throws Exception {
|
||||
con.close();
|
||||
}
|
||||
|
||||
public void fetchItem(int id) throws Exception {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int newItem() throws Exception {
|
||||
// tba
|
||||
return -1;
|
||||
}
|
||||
|
||||
public String getDescription() throws SQLException {
|
||||
ResultSet rs = st.executeQuery("select description from stock where id="+id);
|
||||
if(rs!=null) {
|
||||
rs.next();
|
||||
String s = rs.getString(1);
|
||||
rs.close();
|
||||
return s;
|
||||
Connection con;
|
||||
Statement st;
|
||||
|
||||
// the current stock number
|
||||
int id = -1;
|
||||
|
||||
public void connect(String url, String usr, String pwd) throws Exception
|
||||
{
|
||||
Class.forName("org.postgresql.Driver");
|
||||
System.out.println("Connecting to " + url);
|
||||
con = DriverManager.getConnection(url, usr, pwd);
|
||||
st = con.createStatement();
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
public int getAvailable() throws SQLException {
|
||||
ResultSet rs = st.executeQuery("select avail from stock where id="+id);
|
||||
if(rs!=null) {
|
||||
rs.next();
|
||||
int v = rs.getInt(1);
|
||||
rs.close();
|
||||
return v;
|
||||
|
||||
public void closeConnection() throws Exception
|
||||
{
|
||||
con.close();
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
public int getOrdered() throws SQLException {
|
||||
ResultSet rs = st.executeQuery("select ordered from stock where id="+id);
|
||||
if(rs!=null) {
|
||||
rs.next();
|
||||
int v = rs.getInt(1);
|
||||
rs.close();
|
||||
return v;
|
||||
|
||||
public void fetchItem(int id) throws Exception
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
public boolean isItemValid() throws SQLException {
|
||||
ResultSet rs = st.executeQuery("select valid from stock where id="+id);
|
||||
if(rs!=null) {
|
||||
rs.next();
|
||||
boolean b = rs.getBoolean(1);
|
||||
rs.close();
|
||||
return b;
|
||||
|
||||
public int newItem() throws Exception
|
||||
{
|
||||
// tba
|
||||
return -1;
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
public void addNewStock(int amount) throws SQLException {
|
||||
st.executeUpdate("update stock set avail=avail+"+amount+
|
||||
", ordered=ordered-"+amount+
|
||||
" where id="+id+" and ordered>="+amount);
|
||||
}
|
||||
|
||||
public void removeStock(int amount) throws SQLException {
|
||||
st.executeUpdate("update stock set avail=avail-"+amount+
|
||||
" where id="+id);
|
||||
}
|
||||
|
||||
public void orderStock(int amount) throws SQLException {
|
||||
st.executeUpdate("update stock set ordered=ordered+"+amount+
|
||||
" where id="+id);
|
||||
}
|
||||
|
||||
public int getLastID() throws SQLException {
|
||||
ResultSet rs = st.executeQuery("select max(id) from stock");
|
||||
if(rs!=null) {
|
||||
rs.next();
|
||||
int v = rs.getInt(1);
|
||||
rs.close();
|
||||
return v;
|
||||
|
||||
public String getDescription() throws SQLException
|
||||
{
|
||||
ResultSet rs = st.executeQuery("select description from stock where id=" + id);
|
||||
if (rs != null)
|
||||
{
|
||||
rs.next();
|
||||
String s = rs.getString(1);
|
||||
rs.close();
|
||||
return s;
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
|
||||
public int getAvailable() throws SQLException
|
||||
{
|
||||
ResultSet rs = st.executeQuery("select avail from stock where id=" + id);
|
||||
if (rs != null)
|
||||
{
|
||||
rs.next();
|
||||
int v = rs.getInt(1);
|
||||
rs.close();
|
||||
return v;
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
public int getOrdered() throws SQLException
|
||||
{
|
||||
ResultSet rs = st.executeQuery("select ordered from stock where id=" + id);
|
||||
if (rs != null)
|
||||
{
|
||||
rs.next();
|
||||
int v = rs.getInt(1);
|
||||
rs.close();
|
||||
return v;
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
public boolean isItemValid() throws SQLException
|
||||
{
|
||||
ResultSet rs = st.executeQuery("select valid from stock where id=" + id);
|
||||
if (rs != null)
|
||||
{
|
||||
rs.next();
|
||||
boolean b = rs.getBoolean(1);
|
||||
rs.close();
|
||||
return b;
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
public void addNewStock(int amount) throws SQLException
|
||||
{
|
||||
st.executeUpdate("update stock set avail=avail+" + amount +
|
||||
", ordered=ordered-" + amount +
|
||||
" where id=" + id + " and ordered>=" + amount);
|
||||
}
|
||||
|
||||
public void removeStock(int amount) throws SQLException
|
||||
{
|
||||
st.executeUpdate("update stock set avail=avail-" + amount +
|
||||
" where id=" + id);
|
||||
}
|
||||
|
||||
public void orderStock(int amount) throws SQLException
|
||||
{
|
||||
st.executeUpdate("update stock set ordered=ordered+" + amount +
|
||||
" where id=" + id);
|
||||
}
|
||||
|
||||
public int getLastID() throws SQLException
|
||||
{
|
||||
ResultSet rs = st.executeQuery("select max(id) from stock");
|
||||
if (rs != null)
|
||||
{
|
||||
rs.next();
|
||||
int v = rs.getInt(1);
|
||||
rs.close();
|
||||
return v;
|
||||
}
|
||||
throw new SQLException("No ResultSet");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -5,79 +5,88 @@ import org.omg.CosNaming.*;
|
||||
/**
|
||||
* This class implements the server side of the example.
|
||||
*
|
||||
* $Id: StockDispenserImpl.java,v 1.1 1999/01/25 21:22:03 scrappy Exp $
|
||||
* $Id: StockDispenserImpl.java,v 1.2 2001/10/25 05:59:58 momjian Exp $
|
||||
*/
|
||||
public class StockDispenserImpl extends stock._StockDispenserImplBase
|
||||
{
|
||||
private int maxObjects = 10;
|
||||
private int numObjects = 0;
|
||||
private StockItemStatus[] stock = new StockItemStatus[maxObjects];
|
||||
|
||||
public StockDispenserImpl(String[] args,String name,int num)
|
||||
{
|
||||
super();
|
||||
|
||||
try {
|
||||
// get reference to orb
|
||||
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
|
||||
|
||||
// prestart num objects
|
||||
if(num>=maxObjects)
|
||||
num=maxObjects;
|
||||
numObjects = num;
|
||||
for(int i=0;i<numObjects;i++) {
|
||||
stock[i] = new StockItemStatus();
|
||||
stock[i].ref = new StockItemImpl(args,"StockItem"+(i+1));
|
||||
orb.connect(stock[i].ref);
|
||||
}
|
||||
} catch(org.omg.CORBA.SystemException e) {
|
||||
e.printStackTrace();
|
||||
private int maxObjects = 10;
|
||||
private int numObjects = 0;
|
||||
private StockItemStatus[] stock = new StockItemStatus[maxObjects];
|
||||
|
||||
public StockDispenserImpl(String[] args, String name, int num)
|
||||
{
|
||||
super();
|
||||
|
||||
try
|
||||
{
|
||||
// get reference to orb
|
||||
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
|
||||
|
||||
// prestart num objects
|
||||
if (num >= maxObjects)
|
||||
num = maxObjects;
|
||||
numObjects = num;
|
||||
for (int i = 0;i < numObjects;i++)
|
||||
{
|
||||
stock[i] = new StockItemStatus();
|
||||
stock[i].ref = new StockItemImpl(args, "StockItem" + (i + 1));
|
||||
orb.connect(stock[i].ref);
|
||||
}
|
||||
}
|
||||
catch (org.omg.CORBA.SystemException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method, defined in stock.idl, reserves a slot in the dispenser
|
||||
*/
|
||||
public stock.StockItem reserveItem() throws stock.StockException
|
||||
{
|
||||
for(int i=0;i<numObjects;i++) {
|
||||
if(!stock[i].inUse) {
|
||||
stock[i].inUse = true;
|
||||
System.out.println("Reserving slot "+i);
|
||||
return stock[i].ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method, defined in stock.idl, reserves a slot in the dispenser
|
||||
*/
|
||||
public stock.StockItem reserveItem() throws stock.StockException
|
||||
{
|
||||
for (int i = 0;i < numObjects;i++)
|
||||
{
|
||||
if (!stock[i].inUse)
|
||||
{
|
||||
stock[i].inUse = true;
|
||||
System.out.println("Reserving slot " + i);
|
||||
return stock[i].ref;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This releases a slot from the dispenser
|
||||
*/
|
||||
public void releaseItem(stock.StockItem item) throws stock.StockException
|
||||
{
|
||||
for(int i=0;i<numObjects;i++) {
|
||||
if(stock[i].ref.getInstanceName().equals(item.getInstanceName())) {
|
||||
stock[i].inUse = false;
|
||||
System.out.println("Releasing slot "+i);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* This releases a slot from the dispenser
|
||||
*/
|
||||
public void releaseItem(stock.StockItem item) throws stock.StockException
|
||||
{
|
||||
for (int i = 0;i < numObjects;i++)
|
||||
{
|
||||
if (stock[i].ref.getInstanceName().equals(item.getInstanceName()))
|
||||
{
|
||||
stock[i].inUse = false;
|
||||
System.out.println("Releasing slot " + i);
|
||||
return ;
|
||||
}
|
||||
}
|
||||
System.out.println("Reserved object not a member of this dispenser");
|
||||
return ;
|
||||
}
|
||||
System.out.println("Reserved object not a member of this dispenser");
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class defines a slot in the dispenser
|
||||
*/
|
||||
class StockItemStatus
|
||||
{
|
||||
StockItemImpl ref;
|
||||
boolean inUse;
|
||||
|
||||
StockItemStatus() {
|
||||
ref = null;
|
||||
inUse = false;
|
||||
|
||||
/**
|
||||
* This class defines a slot in the dispenser
|
||||
*/
|
||||
class StockItemStatus
|
||||
{
|
||||
StockItemImpl ref;
|
||||
boolean inUse;
|
||||
|
||||
StockItemStatus()
|
||||
{
|
||||
ref = null;
|
||||
inUse = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -5,159 +5,204 @@ import org.omg.CosNaming.*;
|
||||
/**
|
||||
* This class implements the server side of the example.
|
||||
*
|
||||
* $Id: StockItemImpl.java,v 1.1 1999/01/25 21:22:04 scrappy Exp $
|
||||
* $Id: StockItemImpl.java,v 1.2 2001/10/25 05:59:58 momjian Exp $
|
||||
*/
|
||||
public class StockItemImpl extends stock._StockItemImplBase
|
||||
{
|
||||
private StockDB db;
|
||||
private String instanceName;
|
||||
|
||||
public StockItemImpl(String[] args,String iname) {
|
||||
super();
|
||||
try {
|
||||
db =new StockDB();
|
||||
db.connect(args[1],args[2],args[3]);
|
||||
System.out.println("StockDB object "+iname+" created");
|
||||
instanceName = iname;
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It sets the item to view
|
||||
*/
|
||||
public void fetchItem(int id) throws stock.StockException {
|
||||
try {
|
||||
db.fetchItem(id);
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It sets the item to view
|
||||
*/
|
||||
public int newItem() throws stock.StockException {
|
||||
try {
|
||||
return db.newItem();
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public String getDescription() throws stock.StockException {
|
||||
try {
|
||||
return db.getDescription();
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public int getAvailable() throws stock.StockException {
|
||||
try {
|
||||
return db.getAvailable();
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public int getOrdered() throws stock.StockException {
|
||||
try {
|
||||
return db.getOrdered();
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public boolean isItemValid() throws stock.StockException {
|
||||
try {
|
||||
return db.isItemValid();
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public void addNewStock(int id) throws stock.StockException {
|
||||
try {
|
||||
db.addNewStock(id);
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
private StockDB db;
|
||||
private String instanceName;
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public void removeStock(int id) throws stock.StockException {
|
||||
try {
|
||||
db.removeStock(id);
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
public StockItemImpl(String[] args, String iname)
|
||||
{
|
||||
super();
|
||||
try
|
||||
{
|
||||
db = new StockDB();
|
||||
db.connect(args[1], args[2], args[3]);
|
||||
System.out.println("StockDB object " + iname + " created");
|
||||
instanceName = iname;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public void orderStock(int id) throws stock.StockException {
|
||||
try {
|
||||
db.orderStock(id);
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It sets the item to view
|
||||
*/
|
||||
public void fetchItem(int id) throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
db.fetchItem(id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the highest id used, hence the number of items available
|
||||
*/
|
||||
public int getLastID() throws stock.StockException {
|
||||
try {
|
||||
return db.getLastID();
|
||||
} catch(Exception e) {
|
||||
throw new stock.StockException(e.toString());
|
||||
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It sets the item to view
|
||||
*/
|
||||
public int newItem() throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
return db.newItem();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public String getDescription() throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
return db.getDescription();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public int getAvailable() throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
return db.getAvailable();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public int getOrdered() throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
return db.getOrdered();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public boolean isItemValid() throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
return db.isItemValid();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public void addNewStock(int id) throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
db.addNewStock(id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public void removeStock(int id) throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
db.removeStock(id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is defined in stock.idl
|
||||
*
|
||||
* It returns the description of a Stock item
|
||||
*/
|
||||
public void orderStock(int id) throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
db.orderStock(id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the highest id used, hence the number of items available
|
||||
*/
|
||||
public int getLastID() throws stock.StockException
|
||||
{
|
||||
try
|
||||
{
|
||||
return db.getLastID();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new stock.StockException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is used by our Dispenser
|
||||
*/
|
||||
public String getInstanceName()
|
||||
{
|
||||
return instanceName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is used by our Dispenser
|
||||
*/
|
||||
public String getInstanceName() {
|
||||
return instanceName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -5,49 +5,54 @@ import org.omg.CosNaming.*;
|
||||
/**
|
||||
* This class implements the server side of the example.
|
||||
*
|
||||
* $Id: StockServer.java,v 1.1 1999/01/25 21:22:04 scrappy Exp $
|
||||
* $Id: StockServer.java,v 1.2 2001/10/25 05:59:58 momjian Exp $
|
||||
*/
|
||||
public class StockServer
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int numInstances = 3;
|
||||
|
||||
try {
|
||||
// Initialise the ORB
|
||||
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
|
||||
|
||||
// Create the StockDispenser object
|
||||
StockDispenserImpl dispenser = new StockDispenserImpl(args,"Stock Dispenser",numInstances);
|
||||
|
||||
// Export the new object
|
||||
orb.connect(dispenser);
|
||||
|
||||
// Get the naming service
|
||||
org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService");
|
||||
if(nameServiceObj == null) {
|
||||
System.err.println("nameServiceObj = null");
|
||||
return;
|
||||
}
|
||||
|
||||
org.omg.CosNaming.NamingContext nameService = org.omg.CosNaming.NamingContextHelper.narrow(nameServiceObj);
|
||||
if(nameService == null) {
|
||||
System.err.println("nameService = null");
|
||||
return;
|
||||
}
|
||||
|
||||
// bind the dispenser into the naming service
|
||||
NameComponent[] dispenserName = {
|
||||
new NameComponent("StockDispenser","Stock")
|
||||
};
|
||||
nameService.rebind(dispenserName,dispenser);
|
||||
|
||||
// Now wait forever for the current thread to die
|
||||
Thread.currentThread().join();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int numInstances = 3;
|
||||
|
||||
try
|
||||
{
|
||||
// Initialise the ORB
|
||||
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
|
||||
|
||||
// Create the StockDispenser object
|
||||
StockDispenserImpl dispenser = new StockDispenserImpl(args, "Stock Dispenser", numInstances);
|
||||
|
||||
// Export the new object
|
||||
orb.connect(dispenser);
|
||||
|
||||
// Get the naming service
|
||||
org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService");
|
||||
if (nameServiceObj == null)
|
||||
{
|
||||
System.err.println("nameServiceObj = null");
|
||||
return ;
|
||||
}
|
||||
|
||||
org.omg.CosNaming.NamingContext nameService = org.omg.CosNaming.NamingContextHelper.narrow(nameServiceObj);
|
||||
if (nameService == null)
|
||||
{
|
||||
System.err.println("nameService = null");
|
||||
return ;
|
||||
}
|
||||
|
||||
// bind the dispenser into the naming service
|
||||
NameComponent[] dispenserName = {
|
||||
new NameComponent("StockDispenser", "Stock")
|
||||
};
|
||||
nameService.rebind(dispenserName, dispenser);
|
||||
|
||||
// Now wait forever for the current thread to die
|
||||
Thread.currentThread().join();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user