KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > util > DBHelper


1 /*
2  * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2004 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28
29 package com.sun.ebank.util;
30
31 import java.sql.*;
32 import javax.sql.*;
33 import com.sun.ebank.util.*;
34 import com.sun.ebank.ejb.exception.*;
35
36
37 /**
38  * This class contains helper methods for components
39  * that access Cloudscape databases.
40  */

41 public final class DBHelper {
42     // The getNext<name>Id methods retrieve the next unique
43
// primary key.
44
public static final String JavaDoc getNextAccountId(Connection con)
45         throws SQLException, MissingPrimaryKeyException {
46         Debug.print("DBHelper getNextAccountId");
47
48         return getNextId(con, "next_account_id");
49     }
50      // getNextAccountId
51

52     public static final String JavaDoc getNextCustomerId(Connection con)
53         throws SQLException, MissingPrimaryKeyException {
54         Debug.print("DBHelper getNextCustomerId");
55
56         return getNextId(con, "next_customer_id");
57     }
58      // getNextCustomerId
59

60     public static final String JavaDoc getNextTxId(Connection con)
61         throws SQLException, MissingPrimaryKeyException {
62         Debug.print("DBHelper getNextTxId");
63
64         return getNextId(con, "next_tx_id");
65     }
66      // getNextTxId
67

68     private static final String JavaDoc getNextId(Connection con, String JavaDoc table)
69         throws SQLException, MissingPrimaryKeyException {
70         Debug.print("DBHelper getNextId");
71
72         String JavaDoc selectStatement = "select max(id) from " + table;
73         String JavaDoc updateStatement = "update " + table + " " + "set id = id + 1 ";
74
75         PreparedStatement prepSelect = con.prepareStatement(selectStatement);
76         PreparedStatement prepUpdate = con.prepareStatement(updateStatement);
77
78         prepUpdate.executeUpdate();
79
80         ResultSet rs = prepSelect.executeQuery();
81         rs.next();
82
83         int i = rs.getInt(1);
84         rs.close();
85         prepSelect.close();
86         prepUpdate.close();
87
88         if (i <= 0) {
89             throw new MissingPrimaryKeyException("Table " + table +
90                 " is empty.");
91         }
92
93         return Integer.toString(i);
94     }
95      // getNextId
96

97     public static final java.sql.Date JavaDoc toSQLDate(java.util.Date JavaDoc inDate) {
98         // This method returns a sql.Date version of the util.Date arg.
99
return new java.sql.Date JavaDoc(inDate.getTime());
100     }
101 }
102  // class
103
Popular Tags