KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionEnhydra > persistent > UserNumSessionsQuery


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: UserNumSessionsQuery.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.sessionEnhydra.persistent;
26
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 import com.lutris.appserver.server.sql.DBConnection;
32 import com.lutris.appserver.server.sql.Query;
33 import com.lutris.appserver.server.user.User;
34
35 /**
36  * Queries the session associated with a user.
37  *
38  * @version $Revision: 1.2 $
39  * @author Kyle Clark
40  */

41 class UserNumSessionsQuery implements Query {
42
43     private User user;
44     private static final Integer JavaDoc zero = new Integer JavaDoc(0);
45
46     /**
47      * @param sessionKey
48      * the key for the session that should be retrieved.
49      */

50     UserNumSessionsQuery(User user) {
51         this.user = user;
52     }
53
54     /**
55      * Method to query objects from the database.
56      *
57      * @param conn Handle to database connection.
58      * @exception java.sql.SQLException If a database access error occurs.
59      */

60     public ResultSet JavaDoc executeQuery(DBConnection conn)
61         throws SQLException JavaDoc {
62         String JavaDoc sql = "select count (*) num from "
63             + PersistentSessionHome.dbTableName
64             + " where userName = ?";
65         PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
66         stmt.setString(1, user.getName());
67         return conn.executeQuery(stmt, sql);
68     }
69
70     /**
71      * Return the number of sessions associated with the user.
72      *
73      * @param rs
74      * the result set from which number of user sessions
75      * will be instantiated.
76      * @return an Integer specifying the number of sessions.
77      * @exception SQLException If a database access error occurs.
78      */

79     public Object JavaDoc next(ResultSet JavaDoc rs) throws SQLException JavaDoc {
80         if (rs.next()) {
81             return new Integer JavaDoc(rs.getString("num"));
82         }
83         return zero;
84     }
85
86 }
87
Popular Tags