diff options
author | Marc G. Fournier <scrappy@hub.org> | 1999-01-25 21:22:06 +0000 |
---|---|---|
committer | Marc G. Fournier <scrappy@hub.org> | 1999-01-25 21:22:06 +0000 |
commit | 2ee522954d0d634d520e6f10454a8c63ef004a00 (patch) | |
tree | 2b1b3b0756fb3ad81d689008dd5465cbd1c56389 /src/interfaces/jdbc/example/corba/StockDB.java | |
parent | 1401f63dd15fbc073775fb0279c3b7153ef95f66 (diff) | |
download | postgresql-2ee522954d0d634d520e6f10454a8c63ef004a00.tar.gz postgresql-2ee522954d0d634d520e6f10454a8c63ef004a00.zip |
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 ;-)
Diffstat (limited to 'src/interfaces/jdbc/example/corba/StockDB.java')
-rw-r--r-- | src/interfaces/jdbc/example/corba/StockDB.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/interfaces/jdbc/example/corba/StockDB.java b/src/interfaces/jdbc/example/corba/StockDB.java new file mode 100644 index 00000000000..49ceb1434c3 --- /dev/null +++ b/src/interfaces/jdbc/example/corba/StockDB.java @@ -0,0 +1,117 @@ +package example.corba; + +import java.sql.*; + +/** + * This class handles the JDBC side of things. It opens a connection to + * the database, and performes queries on that database. + * + * In essence, you could use this class on it's own. The rest of the classes + * in this example handle either the CORBA mechanism, or the frontend. + * + * Note: Before you ask, why perform a query on each call, you have to remember + * 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.1 1999/01/25 21:22:03 scrappy 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("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; + } + 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"); + } + +} |