001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.dbcp2; 019 020import java.io.InputStream; 021import java.io.Reader; 022import java.math.BigDecimal; 023import java.sql.Array; 024import java.sql.Blob; 025import java.sql.Clob; 026import java.sql.Date; 027import java.sql.NClob; 028import java.sql.PreparedStatement; 029import java.sql.Ref; 030import java.sql.ResultSet; 031import java.sql.ResultSetMetaData; 032import java.sql.RowId; 033import java.sql.SQLException; 034import java.sql.SQLType; 035import java.sql.SQLXML; 036import java.sql.Statement; 037import java.sql.Time; 038import java.sql.Timestamp; 039import java.util.Calendar; 040 041/** 042 * A base delegating implementation of {@link PreparedStatement}. 043 * <p> 044 * All of the methods from the {@link PreparedStatement} interface simply check to see that the 045 * {@link PreparedStatement} is active, and call the corresponding method on the "delegate" provided in my constructor. 046 * <p> 047 * Extends AbandonedTrace to implement Statement tracking and logging of code which created the Statement. Tracking the 048 * Statement ensures that the Connection which created it can close any open Statement's on Connection close. 049 * 050 * @since 2.0 051 */ 052public class DelegatingPreparedStatement extends DelegatingStatement implements PreparedStatement { 053 054 /** 055 * Create a wrapper for the Statement which traces this Statement to the Connection which created it and the code 056 * which created it. 057 * 058 * @param statement 059 * the {@link PreparedStatement} to delegate all calls to. 060 * @param connection 061 * the {@link DelegatingConnection} that created this statement. 062 */ 063 public DelegatingPreparedStatement(final DelegatingConnection<?> connection, final PreparedStatement statement) { 064 super(connection, statement); 065 } 066 067 @Override 068 public void addBatch() throws SQLException { 069 checkOpen(); 070 try { 071 getDelegatePreparedStatement().addBatch(); 072 } catch (final SQLException e) { 073 handleException(e); 074 } 075 } 076 077 @Override 078 public void clearParameters() throws SQLException { 079 checkOpen(); 080 try { 081 getDelegatePreparedStatement().clearParameters(); 082 } catch (final SQLException e) { 083 handleException(e); 084 } 085 } 086 087 @Override 088 public boolean execute() throws SQLException { 089 checkOpen(); 090 if (getConnectionInternal() != null) { 091 getConnectionInternal().setLastUsed(); 092 } 093 try { 094 return getDelegatePreparedStatement().execute(); 095 } catch (final SQLException e) { 096 handleException(e); 097 return false; 098 } 099 } 100 101 /** 102 * @since 2.5.0 103 */ 104 @Override 105 public long executeLargeUpdate() throws SQLException { 106 checkOpen(); 107 try { 108 return getDelegatePreparedStatement().executeLargeUpdate(); 109 } catch (final SQLException e) { 110 handleException(e); 111 return 0; 112 } 113 } 114 115 @Override 116 public ResultSet executeQuery() throws SQLException { 117 checkOpen(); 118 if (getConnectionInternal() != null) { 119 getConnectionInternal().setLastUsed(); 120 } 121 try { 122 return DelegatingResultSet.wrapResultSet(this, getDelegatePreparedStatement().executeQuery()); 123 } catch (final SQLException e) { 124 handleException(e); 125 throw new AssertionError(); 126 } 127 } 128 129 @Override 130 public int executeUpdate() throws SQLException { 131 checkOpen(); 132 if (getConnectionInternal() != null) { 133 getConnectionInternal().setLastUsed(); 134 } 135 try { 136 return getDelegatePreparedStatement().executeUpdate(); 137 } catch (final SQLException e) { 138 handleException(e); 139 return 0; 140 } 141 } 142 143 private PreparedStatement getDelegatePreparedStatement() { 144 return (PreparedStatement) getDelegate(); 145 } 146 147 @Override 148 public ResultSetMetaData getMetaData() throws SQLException { 149 checkOpen(); 150 try { 151 return getDelegatePreparedStatement().getMetaData(); 152 } catch (final SQLException e) { 153 handleException(e); 154 throw new AssertionError(); 155 } 156 } 157 158 @Override 159 public java.sql.ParameterMetaData getParameterMetaData() throws SQLException { 160 checkOpen(); 161 try { 162 return getDelegatePreparedStatement().getParameterMetaData(); 163 } catch (final SQLException e) { 164 handleException(e); 165 throw new AssertionError(); 166 } 167 } 168 169 @Override 170 public void setArray(final int i, final Array x) throws SQLException { 171 checkOpen(); 172 try { 173 getDelegatePreparedStatement().setArray(i, x); 174 } catch (final SQLException e) { 175 handleException(e); 176 } 177 } 178 179 @Override 180 public void setAsciiStream(final int parameterIndex, final InputStream inputStream) throws SQLException { 181 checkOpen(); 182 try { 183 getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream); 184 } catch (final SQLException e) { 185 handleException(e); 186 } 187 } 188 189 @Override 190 public void setAsciiStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { 191 checkOpen(); 192 try { 193 getDelegatePreparedStatement().setAsciiStream(parameterIndex, x, length); 194 } catch (final SQLException e) { 195 handleException(e); 196 } 197 } 198 199 @Override 200 public void setAsciiStream(final int parameterIndex, final InputStream inputStream, final long length) 201 throws SQLException { 202 checkOpen(); 203 try { 204 getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream, length); 205 } catch (final SQLException e) { 206 handleException(e); 207 } 208 } 209 210 @Override 211 public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException { 212 checkOpen(); 213 try { 214 getDelegatePreparedStatement().setBigDecimal(parameterIndex, x); 215 } catch (final SQLException e) { 216 handleException(e); 217 } 218 } 219 220 @Override 221 public void setBinaryStream(final int parameterIndex, final InputStream inputStream) throws SQLException { 222 checkOpen(); 223 try { 224 getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream); 225 } catch (final SQLException e) { 226 handleException(e); 227 } 228 } 229 230 @Override 231 public void setBinaryStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { 232 checkOpen(); 233 try { 234 getDelegatePreparedStatement().setBinaryStream(parameterIndex, x, length); 235 } catch (final SQLException e) { 236 handleException(e); 237 } 238 } 239 240 @Override 241 public void setBinaryStream(final int parameterIndex, final InputStream inputStream, final long length) 242 throws SQLException { 243 checkOpen(); 244 try { 245 getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream, length); 246 } catch (final SQLException e) { 247 handleException(e); 248 } 249 } 250 251 @Override 252 public void setBlob(final int i, final Blob x) throws SQLException { 253 checkOpen(); 254 try { 255 getDelegatePreparedStatement().setBlob(i, x); 256 } catch (final SQLException e) { 257 handleException(e); 258 } 259 } 260 261 @Override 262 public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException { 263 checkOpen(); 264 try { 265 getDelegatePreparedStatement().setBlob(parameterIndex, inputStream); 266 } catch (final SQLException e) { 267 handleException(e); 268 } 269 } 270 271 @Override 272 public void setBlob(final int parameterIndex, final InputStream inputStream, final long length) 273 throws SQLException { 274 checkOpen(); 275 try { 276 getDelegatePreparedStatement().setBlob(parameterIndex, inputStream, length); 277 } catch (final SQLException e) { 278 handleException(e); 279 } 280 } 281 282 @Override 283 public void setBoolean(final int parameterIndex, final boolean x) throws SQLException { 284 checkOpen(); 285 try { 286 getDelegatePreparedStatement().setBoolean(parameterIndex, x); 287 } catch (final SQLException e) { 288 handleException(e); 289 } 290 } 291 292 @Override 293 public void setByte(final int parameterIndex, final byte x) throws SQLException { 294 checkOpen(); 295 try { 296 getDelegatePreparedStatement().setByte(parameterIndex, x); 297 } catch (final SQLException e) { 298 handleException(e); 299 } 300 } 301 302 @Override 303 public void setBytes(final int parameterIndex, final byte[] x) throws SQLException { 304 checkOpen(); 305 try { 306 getDelegatePreparedStatement().setBytes(parameterIndex, x); 307 } catch (final SQLException e) { 308 handleException(e); 309 } 310 } 311 312 @Override 313 public void setCharacterStream(final int parameterIndex, final Reader reader) throws SQLException { 314 checkOpen(); 315 try { 316 getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader); 317 } catch (final SQLException e) { 318 handleException(e); 319 } 320 } 321 322 @Override 323 public void setCharacterStream(final int parameterIndex, final Reader reader, final int length) 324 throws SQLException { 325 checkOpen(); 326 try { 327 getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length); 328 } catch (final SQLException e) { 329 handleException(e); 330 } 331 } 332 333 @Override 334 public void setCharacterStream(final int parameterIndex, final Reader reader, final long length) 335 throws SQLException { 336 checkOpen(); 337 try { 338 getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length); 339 } catch (final SQLException e) { 340 handleException(e); 341 } 342 } 343 344 @Override 345 public void setClob(final int i, final Clob x) throws SQLException { 346 checkOpen(); 347 try { 348 getDelegatePreparedStatement().setClob(i, x); 349 } catch (final SQLException e) { 350 handleException(e); 351 } 352 } 353 354 @Override 355 public void setClob(final int parameterIndex, final Reader reader) throws SQLException { 356 checkOpen(); 357 try { 358 getDelegatePreparedStatement().setClob(parameterIndex, reader); 359 } catch (final SQLException e) { 360 handleException(e); 361 } 362 } 363 364 @Override 365 public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException { 366 checkOpen(); 367 try { 368 getDelegatePreparedStatement().setClob(parameterIndex, reader, length); 369 } catch (final SQLException e) { 370 handleException(e); 371 } 372 } 373 374 @Override 375 public void setDate(final int parameterIndex, final Date x) throws SQLException { 376 checkOpen(); 377 try { 378 getDelegatePreparedStatement().setDate(parameterIndex, x); 379 } catch (final SQLException e) { 380 handleException(e); 381 } 382 } 383 384 @Override 385 public void setDate(final int parameterIndex, final Date x, final Calendar cal) throws SQLException { 386 checkOpen(); 387 try { 388 getDelegatePreparedStatement().setDate(parameterIndex, x, cal); 389 } catch (final SQLException e) { 390 handleException(e); 391 } 392 } 393 394 @Override 395 public void setDouble(final int parameterIndex, final double x) throws SQLException { 396 checkOpen(); 397 try { 398 getDelegatePreparedStatement().setDouble(parameterIndex, x); 399 } catch (final SQLException e) { 400 handleException(e); 401 } 402 } 403 404 @Override 405 public void setFloat(final int parameterIndex, final float x) throws SQLException { 406 checkOpen(); 407 try { 408 getDelegatePreparedStatement().setFloat(parameterIndex, x); 409 } catch (final SQLException e) { 410 handleException(e); 411 } 412 } 413 414 @Override 415 public void setInt(final int parameterIndex, final int x) throws SQLException { 416 checkOpen(); 417 try { 418 getDelegatePreparedStatement().setInt(parameterIndex, x); 419 } catch (final SQLException e) { 420 handleException(e); 421 } 422 } 423 424 @Override 425 public void setLong(final int parameterIndex, final long x) throws SQLException { 426 checkOpen(); 427 try { 428 getDelegatePreparedStatement().setLong(parameterIndex, x); 429 } catch (final SQLException e) { 430 handleException(e); 431 } 432 } 433 434 @Override 435 public void setNCharacterStream(final int parameterIndex, final Reader reader) throws SQLException { 436 checkOpen(); 437 try { 438 getDelegatePreparedStatement().setNCharacterStream(parameterIndex, reader); 439 } catch (final SQLException e) { 440 handleException(e); 441 } 442 } 443 444 @Override 445 public void setNCharacterStream(final int parameterIndex, final Reader value, final long length) 446 throws SQLException { 447 checkOpen(); 448 try { 449 getDelegatePreparedStatement().setNCharacterStream(parameterIndex, value, length); 450 } catch (final SQLException e) { 451 handleException(e); 452 } 453 } 454 455 @Override 456 public void setNClob(final int parameterIndex, final NClob value) throws SQLException { 457 checkOpen(); 458 try { 459 getDelegatePreparedStatement().setNClob(parameterIndex, value); 460 } catch (final SQLException e) { 461 handleException(e); 462 } 463 } 464 465 @Override 466 public void setNClob(final int parameterIndex, final Reader reader) throws SQLException { 467 checkOpen(); 468 try { 469 getDelegatePreparedStatement().setNClob(parameterIndex, reader); 470 } catch (final SQLException e) { 471 handleException(e); 472 } 473 } 474 475 @Override 476 public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException { 477 checkOpen(); 478 try { 479 getDelegatePreparedStatement().setNClob(parameterIndex, reader, length); 480 } catch (final SQLException e) { 481 handleException(e); 482 } 483 } 484 485 @Override 486 public void setNString(final int parameterIndex, final String value) throws SQLException { 487 checkOpen(); 488 try { 489 getDelegatePreparedStatement().setNString(parameterIndex, value); 490 } catch (final SQLException e) { 491 handleException(e); 492 } 493 } 494 495 @Override 496 public void setNull(final int parameterIndex, final int sqlType) throws SQLException { 497 checkOpen(); 498 try { 499 getDelegatePreparedStatement().setNull(parameterIndex, sqlType); 500 } catch (final SQLException e) { 501 handleException(e); 502 } 503 } 504 505 @Override 506 public void setNull(final int paramIndex, final int sqlType, final String typeName) throws SQLException { 507 checkOpen(); 508 try { 509 getDelegatePreparedStatement().setNull(paramIndex, sqlType, typeName); 510 } catch (final SQLException e) { 511 handleException(e); 512 } 513 } 514 515 @Override 516 public void setObject(final int parameterIndex, final Object x) throws SQLException { 517 checkOpen(); 518 try { 519 getDelegatePreparedStatement().setObject(parameterIndex, x); 520 } catch (final SQLException e) { 521 handleException(e); 522 } 523 } 524 525 @Override 526 public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException { 527 checkOpen(); 528 try { 529 getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType); 530 } catch (final SQLException e) { 531 handleException(e); 532 } 533 } 534 535 @Override 536 public void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scale) 537 throws SQLException { 538 checkOpen(); 539 try { 540 getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scale); 541 } catch (final SQLException e) { 542 handleException(e); 543 } 544 } 545 546 /** 547 * @since 2.5.0 548 */ 549 @Override 550 public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType) throws SQLException { 551 checkOpen(); 552 try { 553 getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType); 554 } catch (final SQLException e) { 555 handleException(e); 556 } 557 } 558 559 /** 560 * @since 2.5.0 561 */ 562 @Override 563 public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException { 564 checkOpen(); 565 try { 566 getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scaleOrLength); 567 } catch (final SQLException e) { 568 handleException(e); 569 } 570 } 571 572 @Override 573 public void setRef(final int i, final Ref x) throws SQLException { 574 checkOpen(); 575 try { 576 getDelegatePreparedStatement().setRef(i, x); 577 } catch (final SQLException e) { 578 handleException(e); 579 } 580 } 581 582 @Override 583 public void setRowId(final int parameterIndex, final RowId value) throws SQLException { 584 checkOpen(); 585 try { 586 getDelegatePreparedStatement().setRowId(parameterIndex, value); 587 } catch (final SQLException e) { 588 handleException(e); 589 } 590 } 591 592 @Override 593 public void setShort(final int parameterIndex, final short x) throws SQLException { 594 checkOpen(); 595 try { 596 getDelegatePreparedStatement().setShort(parameterIndex, x); 597 } catch (final SQLException e) { 598 handleException(e); 599 } 600 } 601 602 @Override 603 public void setSQLXML(final int parameterIndex, final SQLXML value) throws SQLException { 604 checkOpen(); 605 try { 606 getDelegatePreparedStatement().setSQLXML(parameterIndex, value); 607 } catch (final SQLException e) { 608 handleException(e); 609 } 610 } 611 612 @Override 613 public void setString(final int parameterIndex, final String x) throws SQLException { 614 checkOpen(); 615 try { 616 getDelegatePreparedStatement().setString(parameterIndex, x); 617 } catch (final SQLException e) { 618 handleException(e); 619 } 620 } 621 622 @Override 623 public void setTime(final int parameterIndex, final Time x) throws SQLException { 624 checkOpen(); 625 try { 626 getDelegatePreparedStatement().setTime(parameterIndex, x); 627 } catch (final SQLException e) { 628 handleException(e); 629 } 630 } 631 632 @Override 633 public void setTime(final int parameterIndex, final Time x, final Calendar cal) throws SQLException { 634 checkOpen(); 635 try { 636 getDelegatePreparedStatement().setTime(parameterIndex, x, cal); 637 } catch (final SQLException e) { 638 handleException(e); 639 } 640 } 641 642 @Override 643 public void setTimestamp(final int parameterIndex, final Timestamp x) throws SQLException { 644 checkOpen(); 645 try { 646 getDelegatePreparedStatement().setTimestamp(parameterIndex, x); 647 } catch (final SQLException e) { 648 handleException(e); 649 } 650 } 651 652 @Override 653 public void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) throws SQLException { 654 checkOpen(); 655 try { 656 getDelegatePreparedStatement().setTimestamp(parameterIndex, x, cal); 657 } catch (final SQLException e) { 658 handleException(e); 659 } 660 } 661 662 /** @deprecated Use setAsciiStream(), setCharacterStream() or setNCharacterStream() */ 663 @Deprecated 664 @Override 665 public void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { 666 checkOpen(); 667 try { 668 getDelegatePreparedStatement().setUnicodeStream(parameterIndex, x, length); 669 } catch (final SQLException e) { 670 handleException(e); 671 } 672 } 673 674 @Override 675 public void setURL(final int parameterIndex, final java.net.URL x) throws SQLException { 676 checkOpen(); 677 try { 678 getDelegatePreparedStatement().setURL(parameterIndex, x); 679 } catch (final SQLException e) { 680 handleException(e); 681 } 682 } 683 684 /** 685 * Returns a String representation of this object. 686 * 687 * @return String 688 */ 689 @SuppressWarnings("resource") 690 @Override 691 public synchronized String toString() { 692 final Statement statement = getDelegate(); 693 return statement == null ? "NULL" : statement.toString(); 694 } 695}