AutoKeys.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * Redistribution and use in source and binary forms,
  5.  * with or without modification, are permitted provided
  6.  * that the following conditions are met:
  7.  * 1) Redistributions of source code must retain the above
  8.  * copyright notice, this list of conditions and the
  9.  * following  disclaimer.
  10.  * 2)  Redistributions in binary form must reproduce the
  11.  * above copyright notice, this list of conditions and
  12.  * the following disclaimer in the documentation and/or
  13.  * other materials provided with the distribution.
  14.  * 3) Neither the name of "Rafael Steil" nor
  15.  * the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without
  17.  * specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  20.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  21.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  22.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  25.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  30.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  32.  * IN CONTRACT, STRICT LIABILITY, OR TORT
  33.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  35.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  36.  *
  37.  * Created on 24/05/2004 17:40:25
  38.  * The JForum Project
  39.  * http://www.jforum.net
  40.  */
  41. package net.jforum.dao.generic;
  42. import java.sql.Connection;
  43. import java.sql.PreparedStatement;
  44. import java.sql.ResultSet;
  45. import java.sql.SQLException;
  46. import java.sql.Statement;
  47. import net.jforum.JForumExecutionContext;
  48. import net.jforum.util.DbUtils;
  49. import net.jforum.util.preferences.ConfigKeys;
  50. import net.jforum.util.preferences.SystemGlobals;
  51. /**
  52.  * @author Rafael Steil
  53.  * @version $Id: AutoKeys.java,v 1.11 2007/10/01 14:26:43 rafaelsteil Exp $
  54.  */
  55. public class AutoKeys
  56. {
  57. private String autoGeneratedKeysQuery;
  58. protected boolean supportAutoGeneratedKeys()
  59. {
  60. return SystemGlobals.getBoolValue(ConfigKeys.DATABASE_AUTO_KEYS);
  61. }
  62. /**
  63.  * {@link #setSupportAutoGeneratedKey(boolean)} is set to <code>false</code>
  64.  * 
  65.  * @param query The query to execute to retreive the last generated key
  66.  */
  67. protected void setAutoGeneratedKeysQuery(String query)
  68. {
  69. this.autoGeneratedKeysQuery = query;
  70. }
  71. protected String getAutoGeneratedKeysQuery()
  72. {
  73. return this.autoGeneratedKeysQuery;
  74. }
  75. protected String getErrorMessage()
  76. {
  77. return "Could not obtain the latest generated key. This error may be associated"
  78. + " to some invalid database driver operation or server failure."
  79. + " Please check the database configurations and code logic.";
  80. }
  81. protected PreparedStatement getStatementForAutoKeys(String queryName, Connection conn) throws SQLException
  82. {
  83. PreparedStatement p = null;
  84. if (this.supportAutoGeneratedKeys()) {
  85. p = conn.prepareStatement(SystemGlobals.getSql(queryName), Statement.RETURN_GENERATED_KEYS);
  86. }
  87. else {
  88. p = conn.prepareStatement(SystemGlobals.getSql(queryName));
  89. }
  90. return p;
  91. }
  92. protected PreparedStatement getStatementForAutoKeys(String queryName) throws SQLException
  93. {
  94. return this.getStatementForAutoKeys(queryName, JForumExecutionContext.getConnection());
  95. }
  96. protected int executeAutoKeysQuery(PreparedStatement p) throws SQLException
  97. {
  98. return this.executeAutoKeysQuery(p, JForumExecutionContext.getConnection());
  99. }
  100. protected int executeAutoKeysQuery(PreparedStatement p, Connection conn) throws SQLException
  101. {
  102. int id = -1;
  103. p.executeUpdate();
  104. ResultSet rs = null;
  105. try {
  106. if (this.supportAutoGeneratedKeys()) {
  107. rs = p.getGeneratedKeys();
  108. if (rs.next()) {
  109. id = rs.getInt(1);
  110. }
  111. }
  112. else {
  113. p = conn.prepareStatement(this.getAutoGeneratedKeysQuery());
  114. rs = p.executeQuery();
  115. if (rs.next()) {
  116. id = rs.getInt(1);
  117. }
  118. }
  119. }
  120. finally {
  121. DbUtils.close(rs);
  122. }
  123. if (id == -1) {
  124. throw new SQLException(this.getErrorMessage());
  125. }
  126. return id;
  127. }
  128. }