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 $
4
* $Date: 2004/03/14 07:15:04 $
6
* Copyright 2001-2004 The Apache Software Foundation.
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
12
* http://www.apache.org/licenses/LICENSE-2.0
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.
22
package org.apache.artimus.keys.sql;
25
import java.sql.Connection;
26
import java.sql.SQLException;
27
import java.sql.PreparedStatement;
28
import java.sql.ResultSet;
30
import org.apache.scaffold.sql.ConnectionPool;
34
* SQL statements for the Keys package.
35
* @version $Revision: 1.3 $ $Date: 2004/03/14 07:15:04 $
37
public final class Statements {
41
* Returns next sequential key for given table, without
44
* @return The next int key value to be inserted
45
* @exception SQLException if SQL error occurs
47
public static final synchronized int peekKey(String keyName)
51
Connection connection = null;
52
PreparedStatement statement = null;
53
ResultSet resultSet = null;
55
connection = ConnectionPool.getConnection();
57
connection.prepareStatement(Commands.KEYS_NEXT);
58
statement.setString(1,keyName);
59
resultSet = statement.executeQuery();
60
if (resultSet.next()) {
61
next = resultSet.getInt(1);
67
if (resultSet != null) resultSet.close();
68
if (statement != null) statement.close();
69
if (connection!= null) connection.close();
71
catch (SQLException sqle) {}
75
} // ---- End getKeyNext ----
79
* Returns next sequential key for given table.
81
* This ensures compatibility for DBMS products that do not
82
* support auto-incrementing a key field.
84
* Intended to generate primary keys, but could be used to
85
* create other serial numbers based on an unsigned int.
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.
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
96
public static final synchronized Integer allocateKey(String keyName)
100
Connection connection = null;
101
PreparedStatement statement = null;
102
ResultSet resultSet = null;
104
connection = ConnectionPool.getConnection();
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(
113
statement.setString(1,keyName);
114
result = statement.executeUpdate();
120
if (resultSet != null) resultSet.close();
121
if (statement != null) statement.close();
122
if (connection!= null) connection.close();
124
catch (SQLException sqle) {}
128
} // ---- End getKeyNext ----