Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2ee5229

Browse files
committed
From: Peter T Mount <peter@retep.org.uk> This implements some of the JDBC2 methods, fixes a bug introduced into the JDBC1 portion of the driver, and introduces a new example, showing how to use the CORBA ORB thats in Java2 with JDBC. The Tar file contains the new files, the diff the changes to the others. CHANGELOG is separate as I forgot to make a .orig ;-)
1 parent 1401f63 commit 2ee5229

File tree

12 files changed

+879
-10
lines changed

12 files changed

+879
-10
lines changed

src/interfaces/jdbc/CHANGELOG

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
Mon Jan 25 19:45:00 GMT 1999
2+
- created subfolders example/corba and example/corba/idl to hold the
3+
new example showing how to hook CORBA and PostgreSQL via JDBC
4+
- implemented some JDBC2 methods curtesy of Joachim.Gabler@t-online.de
5+
6+
Sat Jan 23 10:30:00 GMT 1999
7+
- Changed imports in postgresql.jdbc1.ResultSetMetaData as for some
8+
reason it didn't want to compile under jdk1.1.6
9+
110
Tue Dec 29 15:45:00 GMT 1998
211
- Refreshed the README (which was way out of date)
312

src/interfaces/jdbc/Makefile

+39-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
# Makefile for Java JDBC interface
55
#
66
# IDENTIFICATION
7-
# $Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/Makefile,v 1.11 1999/01/17 04:51:49 momjian Exp $
7+
# $Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/Makefile,v 1.12 1999/01/25 21:22:02 scrappy Exp $
88
#
99
#-------------------------------------------------------------------------
1010

1111
FIND = find
12+
IDL2JAVA = idltojava -fno-cpp -fno-tie
1213
JAR = jar
1314
JAVA = java
1415
JAVAC = javac
1516
JAVADOC = javadoc
1617
RM = rm -f
18+
TOUCH = touch
1719

1820
# This defines how to compile a java class
1921
.java.class:
@@ -44,6 +46,9 @@ all: makeVersion.class
4446
@echo ------------------------------------------------------------
4547
@echo To build the examples, type:
4648
@echo " make examples"
49+
@echo
50+
@echo "To build the CORBA example (requires Java2):"
51+
@echo " make corba"
4752
@echo ------------------------------------------------------------
4853
@echo
4954

@@ -142,7 +147,8 @@ clean:
142147
$(FIND) . -name "*~" -exec $(RM) {} \;
143148
$(FIND) . -name "*.class" -exec $(RM) {} \;
144149
$(FIND) . -name "*.html" -exec $(RM) {} \;
145-
$(RM) postgresql.jar
150+
-$(RM) -rf stock example/corba/stock.built
151+
-$(RM) postgresql.jar
146152
-$(RM) -rf Package-postgresql *output
147153

148154
#######################################################################
@@ -219,7 +225,37 @@ example/blobtest.class: example/blobtest.java
219225
example/datestyle.class: example/datestyle.java
220226
example/psql.class: example/psql.java
221227
example/ImageViewer.class: example/ImageViewer.java
222-
#example/Objects.class: example/Objects.java
223228
example/threadsafe.class: example/threadsafe.java
224229
example/metadata.class: example/metadata.java
230+
231+
#######################################################################
232+
#
233+
# CORBA This extensive example shows how to integrate PostgreSQL
234+
# JDBC & CORBA.
235+
236+
CORBASRC = $(wildcard example/corba/*.java)
237+
CORBAOBJ = $(subst .java,.class,$(CORBASRC))
238+
239+
corba: jdbc2 example/corba/stock.built $(CORBAOBJ)
240+
@echo -------------------------------------------------------
241+
@echo The corba example has been built. Before running, you
242+
@echo will need to read the example/corba/readme file on how
243+
@echo to run the example.
244+
@echo
245+
246+
#
247+
# This compiles our idl file and the stubs
248+
#
249+
# Note: The idl file is in example/corba, but it builds a directory under
250+
# the current one. For safety, we delete that directory before running
251+
# idltojava
252+
#
253+
example/corba/stock.built: example/corba/stock.idl
254+
-rm -rf stock
255+
$(IDL2JAVA) $<
256+
$(JAVAC) stock/*.java
257+
$(TOUCH) $@
258+
259+
# tip: we cant use $(wildcard stock/*.java) in the above rule as a race
260+
# condition occurs, where javac is passed no arguments
225261
#######################################################################
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
package example.corba;
2+
3+
import java.io.*;
4+
import java.sql.*;
5+
import org.omg.CosNaming.*;
6+
7+
/**
8+
* This class is the frontend to our mini CORBA application.
9+
*
10+
* It has no GUI, just a text frontend to keep it simple.
11+
*
12+
* $Id: StockClient.java,v 1.1 1999/01/25 21:22:03 scrappy Exp $
13+
*/
14+
public class StockClient
15+
{
16+
org.omg.CosNaming.NamingContext nameService;
17+
18+
stock.StockDispenser dispenser;
19+
stock.StockItem item;
20+
21+
BufferedReader in;
22+
23+
public StockClient(String[] args) {
24+
try {
25+
// We need this for our IO
26+
in = new BufferedReader(new InputStreamReader(System.in));
27+
28+
// Initialize the orb
29+
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
30+
31+
// Get a reference to the Naming Service
32+
org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService");
33+
if(nameServiceObj==null) {
34+
System.err.println("nameServiceObj == null");
35+
return;
36+
}
37+
38+
nameService = org.omg.CosNaming.NamingContextHelper.narrow(nameServiceObj);
39+
if(nameService==null) {
40+
System.err.println("nameService == null");
41+
return;
42+
}
43+
44+
// Resolve the dispenser
45+
NameComponent[] dispName = {
46+
new NameComponent("StockDispenser","Stock")
47+
};
48+
dispenser = stock.StockDispenserHelper.narrow(nameService.resolve(dispName));
49+
if(dispenser==null) {
50+
System.err.println("dispenser == null");
51+
return;
52+
}
53+
54+
// Now run the front end.
55+
run();
56+
} catch(Exception e) {
57+
System.out.println(e.toString());
58+
e.printStackTrace();
59+
System.exit(1);
60+
}
61+
}
62+
63+
public static void main(String[] args) {
64+
new StockClient(args);
65+
}
66+
67+
public void run() {
68+
// First reserve a StockItem
69+
try {
70+
item = dispenser.reserveItem();
71+
} catch(Exception e) {
72+
System.out.println(e.toString());
73+
e.printStackTrace();
74+
System.exit(1);
75+
}
76+
77+
mainMenu();
78+
79+
// finally free the StockItem
80+
try {
81+
dispenser.releaseItem(item);
82+
} catch(Exception e) {
83+
System.out.println(e.toString());
84+
e.printStackTrace();
85+
System.exit(1);
86+
}
87+
}
88+
89+
private void mainMenu() {
90+
boolean run=true;
91+
while(run) {
92+
System.out.println("\nCORBA Stock System\n");
93+
System.out.println(" 1 Display stock item");
94+
System.out.println(" 2 Remove item from stock");
95+
System.out.println(" 3 Put item into stock");
96+
System.out.println(" 4 Order item");
97+
System.out.println(" 5 Display all items");
98+
System.out.println(" 0 Exit");
99+
int i = getMenu("Main",5);
100+
switch(i)
101+
{
102+
case 0:
103+
run=false;
104+
break;
105+
106+
case 1:
107+
displayItem();
108+
break;
109+
110+
case 2:
111+
bookOut();
112+
break;
113+
114+
case 3:
115+
bookIn();
116+
break;
117+
118+
case 4:
119+
order(0);
120+
break;
121+
122+
case 5:
123+
displayAll();
124+
break;
125+
}
126+
}
127+
}
128+
129+
private void displayItem() {
130+
try {
131+
int id = getMenu("\nStockID to display",item.getLastID());
132+
if(id>0) {
133+
item.fetchItem(id);
134+
System.out.println("========================================");
135+
136+
String status = "";
137+
if(!item.isItemValid())
138+
status=" ** Superceded **";
139+
140+
int av = item.getAvailable();
141+
142+
System.out.println(" Stock ID: "+id+status+
143+
"\nItems Available: "+av+
144+
"\nItems on order: "+item.getOrdered()+
145+
"\n Description: "+item.getDescription());
146+
System.out.println("========================================");
147+
148+
if(av>0)
149+
if(yn("Take this item out of stock?")) {
150+
int rem=1;
151+
if(av>1)
152+
rem=getMenu("How many?",av);
153+
if(rem>0)
154+
item.removeStock(rem);
155+
}
156+
157+
}
158+
} catch(Exception e) {
159+
System.out.println(e.toString());
160+
e.printStackTrace();
161+
}
162+
}
163+
164+
private void bookOut() {
165+
try {
166+
int id = getMenu("\nStockID to take out",item.getLastID());
167+
if(id>0) {
168+
item.fetchItem(id);
169+
int av = item.getAvailable();
170+
if(av>0)
171+
if(yn("Take this item out of stock?")) {
172+
int rem=1;
173+
if(av>1)
174+
rem=getMenu("How many?",av);
175+
if(rem>0)
176+
item.removeStock(rem);
177+
}
178+
else {
179+
System.out.println("This item is not in stock.");
180+
int order = item.getOrdered();
181+
if(order>0)
182+
System.out.println("There are "+item.getOrdered()+" items on order.");
183+
else {
184+
if(item.isItemValid()) {
185+
System.out.println("You will need to order some more "+item.getDescription());
186+
order(id);
187+
} else
188+
System.out.println("This item is now obsolete");
189+
}
190+
}
191+
} else
192+
System.out.println(item.getDescription()+"\nThis item is out of stock");
193+
} catch(Exception e) {
194+
System.out.println(e.toString());
195+
e.printStackTrace();
196+
}
197+
}
198+
199+
// book an item into stock
200+
private void bookIn() {
201+
try {
202+
int id = getMenu("\nStockID to book in",item.getLastID());
203+
item.fetchItem(id);
204+
System.out.println(item.getDescription());
205+
206+
if(item.getOrdered()>0) {
207+
int am = getMenu("How many do you want to book in",item.getOrdered());
208+
if(am>0)
209+
item.addNewStock(am);
210+
} else
211+
System.out.println("You don't have any of this item on ordered");
212+
213+
} catch(Exception e) {
214+
System.out.println(e.toString());
215+
e.printStackTrace();
216+
}
217+
}
218+
219+
// Order an item
220+
private void order(int id) {
221+
try {
222+
if(id==0)
223+
id = getMenu("\nStockID to order",item.getLastID());
224+
item.fetchItem(id);
225+
System.out.println(item.getDescription());
226+
int am = getMenu("How many do you want to order",999);
227+
if(am>0)
228+
item.orderStock(am);
229+
} catch(Exception e) {
230+
System.out.println(e.toString());
231+
e.printStackTrace();
232+
}
233+
}
234+
235+
private void displayAll() {
236+
try {
237+
boolean cont=true;
238+
int nr=item.getLastID();
239+
String header = "\nId\tAvail\tOrdered\tDescription";
240+
System.out.println(header);
241+
for(int i=1;i<=nr && cont;i++) {
242+
item.fetchItem(i);
243+
System.out.println(""+i+"\t"+item.getAvailable()+"\t"+item.getOrdered()+"\t"+item.getDescription());
244+
if((i%20)==0) {
245+
if((cont=yn("Continue?")))
246+
System.out.println(header);
247+
}
248+
}
249+
} catch(Exception e) {
250+
System.out.println(e.toString());
251+
e.printStackTrace();
252+
}
253+
}
254+
255+
private int getMenu(String title,int max) {
256+
int v=-1;
257+
while(v<0 || v>max) {
258+
System.out.print(title);
259+
System.out.print(" [0-"+max+"]: ");
260+
System.out.flush();
261+
try {
262+
v = Integer.parseInt(in.readLine());
263+
} catch(Exception nfe) {
264+
v=-1;
265+
}
266+
}
267+
return v;
268+
}
269+
270+
private boolean yn(String title) {
271+
try {
272+
while(true) {
273+
System.out.print(title);
274+
System.out.flush();
275+
String s = in.readLine();
276+
if(s.startsWith("y") || s.startsWith("Y"))
277+
return true;
278+
if(s.startsWith("n") || s.startsWith("N"))
279+
return false;
280+
}
281+
} catch(Exception nfe) {
282+
System.out.println(nfe.toString());
283+
nfe.printStackTrace();
284+
System.exit(1);
285+
}
286+
return false;
287+
}
288+
}

0 commit comments

Comments
 (0)