~ubuntu-branches/ubuntu/gutsy/libpgjava/gutsy

« back to all changes in this revision

Viewing changes to example/corba/StockDB.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-06 15:03:58 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070106150358-5b5ps3nz7r3ppi44
Tags: 8.2-504-0ubuntu1
* New upstream version, supporting Postgresql 8.2.
* Build using java-gcj-compat-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package example.corba;
2
 
 
3
 
import java.sql.*;
4
 
 
5
 
/*
6
 
 * This class handles the JDBC side of things. It opens a connection to
7
 
 * the database, and performes queries on that database.
8
 
 *
9
 
 * In essence, you could use this class on it's own. The rest of the classes
10
 
 * in this example handle either the CORBA mechanism, or the frontend.
11
 
 *
12
 
 * Note: Before you ask, why perform a query on each call, you have to remember
13
 
 * that an object could be changed by another client, and we need to ensure that
14
 
 * the returned data is live and accurate.
15
 
 *
16
 
 * $PostgreSQL: pgjdbc/example/corba/StockDB.java,v 1.6 2004/11/09 08:43:10 jurka Exp $
17
 
 */
18
 
public class StockDB
19
 
{
20
 
    Connection con;
21
 
    Statement st;
22
 
 
23
 
    // the current stock number
24
 
    int id = -1;
25
 
 
26
 
    public void connect(String url, String usr, String pwd) throws Exception
27
 
    {
28
 
        Class.forName("org.postgresql.Driver");
29
 
        System.out.println("Connecting to " + url);
30
 
        con = DriverManager.getConnection(url, usr, pwd);
31
 
        st = con.createStatement();
32
 
    }
33
 
 
34
 
    public void closeConnection() throws Exception
35
 
    {
36
 
        con.close();
37
 
    }
38
 
 
39
 
    public void fetchItem(int id) throws Exception
40
 
    {
41
 
        this.id = id;
42
 
    }
43
 
 
44
 
    public int newItem() throws Exception
45
 
    {
46
 
        // tba
47
 
        return -1;
48
 
    }
49
 
 
50
 
    public String getDescription() throws SQLException
51
 
    {
52
 
        ResultSet rs = st.executeQuery("select description from stock where id=" + id);
53
 
        if (rs != null)
54
 
        {
55
 
            rs.next();
56
 
            String s = rs.getString(1);
57
 
            rs.close();
58
 
            return s;
59
 
        }
60
 
        throw new SQLException("No ResultSet");
61
 
    }
62
 
 
63
 
    public int getAvailable() throws SQLException
64
 
    {
65
 
        ResultSet rs = st.executeQuery("select avail from stock where id=" + id);
66
 
        if (rs != null)
67
 
        {
68
 
            rs.next();
69
 
            int v = rs.getInt(1);
70
 
            rs.close();
71
 
            return v;
72
 
        }
73
 
        throw new SQLException("No ResultSet");
74
 
    }
75
 
 
76
 
    public int getOrdered() throws SQLException
77
 
    {
78
 
        ResultSet rs = st.executeQuery("select ordered from stock where id=" + id);
79
 
        if (rs != null)
80
 
        {
81
 
            rs.next();
82
 
            int v = rs.getInt(1);
83
 
            rs.close();
84
 
            return v;
85
 
        }
86
 
        throw new SQLException("No ResultSet");
87
 
    }
88
 
 
89
 
    public boolean isItemValid() throws SQLException
90
 
    {
91
 
        ResultSet rs = st.executeQuery("select valid from stock where id=" + id);
92
 
        if (rs != null)
93
 
        {
94
 
            rs.next();
95
 
            boolean b = rs.getBoolean(1);
96
 
            rs.close();
97
 
            return b;
98
 
        }
99
 
        throw new SQLException("No ResultSet");
100
 
    }
101
 
 
102
 
    public void addNewStock(int amount) throws SQLException
103
 
    {
104
 
        st.executeUpdate("update stock set avail=avail+" + amount +
105
 
                         ", ordered=ordered-" + amount +
106
 
                         " where id=" + id + " and ordered>=" + amount);
107
 
    }
108
 
 
109
 
    public void removeStock(int amount) throws SQLException
110
 
    {
111
 
        st.executeUpdate("update stock set avail=avail-" + amount +
112
 
                         " where id=" + id);
113
 
    }
114
 
 
115
 
    public void orderStock(int amount) throws SQLException
116
 
    {
117
 
        st.executeUpdate("update stock set ordered=ordered+" + amount +
118
 
                         " where id=" + id);
119
 
    }
120
 
 
121
 
    public int getLastID() throws SQLException
122
 
    {
123
 
        ResultSet rs = st.executeQuery("select max(id) from stock");
124
 
        if (rs != null)
125
 
        {
126
 
            rs.next();
127
 
            int v = rs.getInt(1);
128
 
            rs.close();
129
 
            return v;
130
 
        }
131
 
        throw new SQLException("No ResultSet");
132
 
    }
133
 
 
134
 
}