~ubuntu-branches/ubuntu/lucid/libstruts1.2-java/lucid

« back to all changes in this revision

Viewing changes to contrib/artimus/WEB-INF/src/java/org/apache/artimus/keys/sql/Statements.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-04-24 12:14:23 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060424121423-naev53qigqgks0sa
Tags: 1.2.9-1
New upstream  release Fixes  three security  problems: CVE-2006-1546,
CVE-2006-1547,  CVE-2006-1548  (closes:  #360551),  thanks  to  Moritz
Muehlenhoff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Header: /home/cvs/jakarta-struts/contrib/artimus/WEB-INF/src/java/org/apache/artimus/keys/sql/Statements.java,v 1.3 2004/03/14 07:15:04 sraeburn Exp $
3
 
 * $Revision: 1.3 $
4
 
 * $Date: 2004/03/14 07:15:04 $
5
 
 *
6
 
 * Copyright 2001-2004 The Apache Software Foundation.
7
 
 * 
8
 
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 
 * you may not use this file except in compliance with the License.
10
 
 * You may obtain a copy of the License at
11
 
 * 
12
 
 *      http://www.apache.org/licenses/LICENSE-2.0
13
 
 * 
14
 
 * Unless required by applicable law or agreed to in writing, software
15
 
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 
 * See the License for the specific language governing permissions and
18
 
 * limitations under the License.
19
 
 */
20
 
 
21
 
 
22
 
package org.apache.artimus.keys.sql;
23
 
 
24
 
 
25
 
import java.sql.Connection;
26
 
import java.sql.SQLException;
27
 
import java.sql.PreparedStatement;
28
 
import java.sql.ResultSet;
29
 
 
30
 
import org.apache.scaffold.sql.ConnectionPool;
31
 
 
32
 
 
33
 
/**
34
 
 * SQL statements for the Keys package.
35
 
 * @version $Revision: 1.3 $ $Date: 2004/03/14 07:15:04 $
36
 
 */
37
 
public final class Statements {
38
 
 
39
 
 
40
 
    /**
41
 
     * Returns next sequential key for given table, without
42
 
     * allocating a key.
43
 
     * <p>
44
 
     * @return The next int key value to be inserted
45
 
     * @exception SQLException if SQL error occurs
46
 
     */
47
 
    public static final synchronized int peekKey(String keyName)
48
 
            throws SQLException {
49
 
       int next = 0;
50
 
       int result = 0;
51
 
       Connection connection = null;
52
 
       PreparedStatement statement = null;
53
 
       ResultSet resultSet = null;
54
 
       try {
55
 
           connection = ConnectionPool.getConnection();
56
 
           statement =
57
 
                connection.prepareStatement(Commands.KEYS_NEXT);
58
 
           statement.setString(1,keyName);
59
 
           resultSet = statement.executeQuery();
60
 
           if (resultSet.next()) {
61
 
                next = resultSet.getInt(1);
62
 
          }
63
 
        }
64
 
 
65
 
        finally {
66
 
            try {
67
 
                if (resultSet != null) resultSet.close();
68
 
                if (statement != null) statement.close();
69
 
                if (connection!= null) connection.close();
70
 
            }
71
 
            catch (SQLException sqle) {}
72
 
        }
73
 
        return next;
74
 
 
75
 
    } // ---- End getKeyNext ----
76
 
 
77
 
 
78
 
    /**
79
 
     * Returns next sequential key for given table.
80
 
     * <p>
81
 
     * This ensures compatibility for DBMS products that do not
82
 
     * support auto-incrementing a key field.
83
 
     * <p>
84
 
     * Intended to generate primary keys, but could be used to
85
 
     * create other serial numbers based on an unsigned int.
86
 
     * <p>
87
 
     * Allocating the key involves reading the current key, and
88
 
     * then incrementing the key for the next user. The method
89
 
     * is synchronized so that two threads do not read the
90
 
     * same key before it is increment.
91
 
     * <p>
92
 
     * @return The unsigned int key value to be inserted
93
 
     * @exception SQLException if SQL error occurs
94
 
     * @param keyName The name of the table for this key
95
 
     */
96
 
    public static final synchronized Integer allocateKey(String keyName)
97
 
            throws SQLException {
98
 
       Integer next = null;
99
 
       int result = 0;
100
 
       Connection connection = null;
101
 
       PreparedStatement statement = null;
102
 
       ResultSet resultSet = null;
103
 
       try {
104
 
           connection = ConnectionPool.getConnection();
105
 
           statement =
106
 
                connection.prepareStatement(Commands.KEYS_NEXT);
107
 
           statement.setString(1,keyName);
108
 
           resultSet = statement.executeQuery();
109
 
           if (resultSet.next()) {
110
 
                next = new Integer(resultSet.getInt(1));
111
 
                statement = connection.prepareStatement(
112
 
                    Commands.KEYS_INC);
113
 
                statement.setString(1,keyName);
114
 
                result = statement.executeUpdate();
115
 
          }
116
 
        }
117
 
 
118
 
        finally {
119
 
            try {
120
 
                if (resultSet != null) resultSet.close();
121
 
                if (statement != null) statement.close();
122
 
                if (connection!= null) connection.close();
123
 
            }
124
 
            catch (SQLException sqle) {}
125
 
        }
126
 
        return next;
127
 
 
128
 
    } // ---- End getKeyNext ----
129
 
 
130
 
}