Commit d2e27b06 authored by Bruce Momjian's avatar Bruce Momjian

pgjindent jdbc files. First time jdbc files were formatted.

parent b81844b1
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -13,105 +13,122 @@ import java.sql.*; ...@@ -13,105 +13,122 @@ import java.sql.*;
* that an object could be changed by another client, and we need to ensure that * that an object could be changed by another client, and we need to ensure that
* the returned data is live and accurate. * 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 public class StockDB
{ {
Connection con; Connection con;
Statement st; Statement st;
// the current stock number // the current stock number
int id = -1; int id = -1;
public void connect(String url,String usr,String pwd) throws Exception { public void connect(String url, String usr, String pwd) throws Exception
Class.forName("org.postgresql.Driver"); {
System.out.println("Connecting to "+url); Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(url,usr,pwd); System.out.println("Connecting to " + url);
st = con.createStatement(); 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;
} }
throw new SQLException("No ResultSet");
} public void closeConnection() throws Exception
{
public int getAvailable() throws SQLException { con.close();
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 void fetchItem(int id) throws Exception
{
public int getOrdered() throws SQLException { this.id = id;
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 int newItem() throws Exception
{
public boolean isItemValid() throws SQLException { // tba
ResultSet rs = st.executeQuery("select valid from stock where id="+id); return -1;
if(rs!=null) {
rs.next();
boolean b = rs.getBoolean(1);
rs.close();
return b;
} }
throw new SQLException("No ResultSet");
} public String getDescription() throws SQLException
{
public void addNewStock(int amount) throws SQLException { ResultSet rs = st.executeQuery("select description from stock where id=" + id);
st.executeUpdate("update stock set avail=avail+"+amount+ if (rs != null)
", ordered=ordered-"+amount+ {
" where id="+id+" and ordered>="+amount); rs.next();
} String s = rs.getString(1);
rs.close();
public void removeStock(int amount) throws SQLException { return s;
st.executeUpdate("update stock set avail=avail-"+amount+ }
" where id="+id); throw new SQLException("No ResultSet");
}
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");
} 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.*; ...@@ -5,79 +5,88 @@ import org.omg.CosNaming.*;
/** /**
* This class implements the server side of the example. * 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 public class StockDispenserImpl extends stock._StockDispenserImplBase
{ {
private int maxObjects = 10; private int maxObjects = 10;
private int numObjects = 0; private int numObjects = 0;
private StockItemStatus[] stock = new StockItemStatus[maxObjects]; private StockItemStatus[] stock = new StockItemStatus[maxObjects];
public StockDispenserImpl(String[] args,String name,int num) public StockDispenserImpl(String[] args, String name, int num)
{ {
super(); super();
try { try
// get reference to orb {
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); // get reference to orb
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
// prestart num objects
if(num>=maxObjects) // prestart num objects
num=maxObjects; if (num >= maxObjects)
numObjects = num; num = maxObjects;
for(int i=0;i<numObjects;i++) { numObjects = num;
stock[i] = new StockItemStatus(); for (int i = 0;i < numObjects;i++)
stock[i].ref = new StockItemImpl(args,"StockItem"+(i+1)); {
orb.connect(stock[i].ref); stock[i] = new StockItemStatus();
} stock[i].ref = new StockItemImpl(args, "StockItem" + (i + 1));
} catch(org.omg.CORBA.SystemException e) { orb.connect(stock[i].ref);
e.printStackTrace(); }
}
catch (org.omg.CORBA.SystemException e)
{
e.printStackTrace();
}
} }
}
/**
/** * This method, defined in stock.idl, reserves a slot in the dispenser
* This method, defined in stock.idl, reserves a slot in the dispenser */
*/ public stock.StockItem reserveItem() throws stock.StockException
public stock.StockItem reserveItem() throws stock.StockException {
{ for (int i = 0;i < numObjects;i++)
for(int i=0;i<numObjects;i++) { {
if(!stock[i].inUse) { if (!stock[i].inUse)
stock[i].inUse = true; {
System.out.println("Reserving slot "+i); stock[i].inUse = true;
return stock[i].ref; System.out.println("Reserving slot " + i);
} return stock[i].ref;
}
}
return null;
} }
return null;
} /**
* This releases a slot from the dispenser
/** */
* This releases a slot from the dispenser public void releaseItem(stock.StockItem item) throws stock.StockException
*/ {
public void releaseItem(stock.StockItem item) throws stock.StockException for (int i = 0;i < numObjects;i++)
{ {
for(int i=0;i<numObjects;i++) { if (stock[i].ref.getInstanceName().equals(item.getInstanceName()))
if(stock[i].ref.getInstanceName().equals(item.getInstanceName())) { {
stock[i].inUse = false; stock[i].inUse = false;
System.out.println("Releasing slot "+i); System.out.println("Releasing slot " + i);
return; 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
* This class defines a slot in the dispenser {
*/ StockItemImpl ref;
class StockItemStatus boolean inUse;
{
StockItemImpl ref; StockItemStatus()
boolean inUse; {
ref = null;
StockItemStatus() { inUse = false;
ref = null; }
inUse = false;
} }
}
} }
...@@ -5,159 +5,204 @@ import org.omg.CosNaming.*; ...@@ -5,159 +5,204 @@ import org.omg.CosNaming.*;
/** /**
* This class implements the server side of the example. * 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 public class StockItemImpl extends stock._StockItemImplBase
{ {
private StockDB db; private StockDB db;
private String instanceName; private String instanceName;
public StockItemImpl(String[] args,String iname) { public StockItemImpl(String[] args, String iname)
super(); {
try { super();
db =new StockDB(); try
db.connect(args[1],args[2],args[3]); {
System.out.println("StockDB object "+iname+" created"); db = new StockDB();
instanceName = iname; db.connect(args[1], args[2], args[3]);
} catch(Exception e) { System.out.println("StockDB object " + iname + " created");
e.printStackTrace(); instanceName = iname;
}
catch (Exception e)
{
e.printStackTrace();
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It sets the item to view
* It sets the item to view */
*/ public void fetchItem(int id) throws stock.StockException
public void fetchItem(int id) throws stock.StockException { {
try { try
db.fetchItem(id); {
} catch(Exception e) { db.fetchItem(id);
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It sets the item to view
* It sets the item to view */
*/ public int newItem() throws stock.StockException
public int newItem() throws stock.StockException { {
try { try
return db.newItem(); {
} catch(Exception e) { return db.newItem();
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It returns the description of a Stock item
* It returns the description of a Stock item */
*/ public String getDescription() throws stock.StockException
public String getDescription() throws stock.StockException { {
try { try
return db.getDescription(); {
} catch(Exception e) { return db.getDescription();
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It returns the description of a Stock item
* It returns the description of a Stock item */
*/ public int getAvailable() throws stock.StockException
public int getAvailable() throws stock.StockException { {
try { try
return db.getAvailable(); {
} catch(Exception e) { return db.getAvailable();
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It returns the description of a Stock item
* It returns the description of a Stock item */
*/ public int getOrdered() throws stock.StockException
public int getOrdered() throws stock.StockException { {
try { try
return db.getOrdered(); {
} catch(Exception e) { return db.getOrdered();
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It returns the description of a Stock item
* It returns the description of a Stock item */
*/ public boolean isItemValid() throws stock.StockException
public boolean isItemValid() throws stock.StockException { {
try { try
return db.isItemValid(); {
} catch(Exception e) { return db.isItemValid();
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It returns the description of a Stock item
* It returns the description of a Stock item */
*/ public void addNewStock(int id) throws stock.StockException
public void addNewStock(int id) throws stock.StockException { {
try { try
db.addNewStock(id); {
} catch(Exception e) { db.addNewStock(id);
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It returns the description of a Stock item
* It returns the description of a Stock item */
*/ public void removeStock(int id) throws stock.StockException
public void removeStock(int id) throws stock.StockException { {
try { try
db.removeStock(id); {
} catch(Exception e) { db.removeStock(id);
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This is defined in stock.idl
* This is defined in stock.idl *
* * It returns the description of a Stock item
* It returns the description of a Stock item */
*/ public void orderStock(int id) throws stock.StockException
public void orderStock(int id) throws stock.StockException { {
try { try
db.orderStock(id); {
} catch(Exception e) { db.orderStock(id);
throw new stock.StockException(e.toString()); }
catch (Exception e)
{
throw new stock.StockException(e.toString());
}
} }
}
/**
/** * This returns the highest id used, hence the number of items available
* This returns the highest id used, hence the number of items available */
*/ public int getLastID() throws stock.StockException
public int getLastID() throws stock.StockException { {
try { try
return db.getLastID(); {
} catch(Exception e) { return db.getLastID();
throw new stock.StockException(e.toString()); }
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.*; ...@@ -5,49 +5,54 @@ import org.omg.CosNaming.*;
/** /**
* This class implements the server side of the example. * 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 class StockServer
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int numInstances = 3; int numInstances = 3;
try { try
// Initialise the ORB {
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null); // 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); // Create the StockDispenser object
StockDispenserImpl dispenser = new StockDispenserImpl(args, "Stock Dispenser", numInstances);
// Export the new object
orb.connect(dispenser); // Export the new object
orb.connect(dispenser);
// Get the naming service
org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService"); // Get the naming service
if(nameServiceObj == null) { org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService");
System.err.println("nameServiceObj = null"); if (nameServiceObj == null)
return; {
} 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"); org.omg.CosNaming.NamingContext nameService = org.omg.CosNaming.NamingContextHelper.narrow(nameServiceObj);
return; if (nameService == null)
} {
System.err.println("nameService = null");
// bind the dispenser into the naming service return ;
NameComponent[] dispenserName = { }
new NameComponent("StockDispenser","Stock")
}; // bind the dispenser into the naming service
nameService.rebind(dispenserName,dispenser); NameComponent[] dispenserName = {
new NameComponent("StockDispenser", "Stock")
// Now wait forever for the current thread to die };
Thread.currentThread().join(); nameService.rebind(dispenserName, dispenser);
} catch(Exception e) {
e.printStackTrace(); // Now wait forever for the current thread to die
Thread.currentThread().join();
}
catch (Exception e)
{
e.printStackTrace();
}
} }
}
} }
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment