How To Write Named Parameterized Query In Java
在java中,对于大查询,创建参数化查询并不容易,因为JAVA JDBC API接口PreparedStatement只支持通过索引传递参数。
为了克服这个限制,我们将创建自定义类,使我们能够按名称将参数传递给查询。让我们看看手头的问题是什么,然后我们将讨论解决方案。
为什么我们需要参数化查询?
在 Java 中,要执行 sql 查询,我们必须创建语句,然后我们可以执行该语句以从数据库中选择、插入、更新或删除数据。但是,不推荐这种方法,因为 java 语句不带任何参数,因此无法防止 SQL 注入。
/*
* Here, we inserted static values in query. In your case these values might come from user input.
* Here, we are directly concatinating the values in the query. Hence, It's not SQL injection safe.
*/
String sqlQuery = " INSERT INTO employee (id, name, designation, salary)"
+ " VALUES('1', 'Dwayne Johnson', 'Project Manager', '250000')";
Statement stmt = conn.createStatement();
stmt.executeUpdate(sqlQuery);
stmt.close();
为了防止 SQL 注入,我们必须编写参数化查询。要在 java 中创建参数化查询,我们有 PreparedStatement。它可以通过在查询中传递问号 (?) 来获取参数,然后将每个问号索引替换为所需的值。
/*
* Here, we assigned static values in parameter. In your case these values might come from user input.
* Here, we are not directly concatinating the values in the query. Hence, It's SQL injection safe.
*/
String sqlQuery = " INSERT INTO employee (id, name, designation, salary)"
+ " VALUES(?, ?, ?, ?)";
PreparedStatement prepStmt = conn.prepareStatement(sqlQuery);
prepStmt.setLong(1, 1);
prepStmt.setString(2, "Dwayne Jhonson");
prepStmt.setString(3, "Team Leader");
prepStmt.setDouble(4, 10000);
prepStmt.executeUpdate();
prepStmt.close();
为什么我们需要命名参数化查询?
正如您在上面的示例中看到的,PreparedStatement 使用问号的索引将参数传递给查询。需要时间,因为您必须计算索引以传递值,如果您的查询中有太多列来计算查询中每列的索引,情况会变得更糟。如果我们可以做这样的事情——
/*
* This code is just to show requirement. PreparedStatement in java JDBC API does not support this syntax to set parameters
*/
String sqlQuery = " INSERT INTO employee (id, name, designation, salary)"
+ " VALUES(:id, :name, :designation, :salary)";
PreparedStatement prepStmt = conn.prepareStatement(sqlQuery);
prepStmt.setLong("id", 1);
prepStmt.setString("name", "Dwayne Jhonson");
prepStmt.setString("designation", "Team Leader");
prepStmt.setDouble("salary", 10000);
prepStmt.executeUpdate();
prepStmt.close();
但是,PreparedStatement 不支持按名称设置参数。因此,我们必须创建自定义的 PreparedStatement 类,该类将为我们提供按名称设置参数的功能。
在一分钟内创建您的第一个命名参数化查询
下面,我展示了我为编写参数化查询而创建的两个自定义类 -
只需在您的项目中创建这两个类,您就可以像这样在一分钟内创建参数化查询 -
String sqlQuery = " INSERT INTO employee (id, name, designation, salary)"
+ " VALUES(:id, :name, :designation, :salary)";
PreparedStatement prepStmt = NamedPreparedStatement.prepareStatement(connection, sqlQuery);
prepStmt.setLong("id", 1);
prepStmt.setString("name", "Dwayne Jhonson");
prepStmt.setString("designation", "Team Leader");
prepStmt.setDouble("salary", 10000);
prepStmt.executeUpdate();
prepStmt.close();
你不需要知道这些类是如何工作的。您可以创建这些类并开始使用它们,如上例所示。但是,作为一名程序员,您应该尝试了解它是如何工作的。这种方法很简单,因为您不必计算列的索引,并且您可以随时更改列的顺序。
NamedPreparedStatement.java
package com.codemeright.namedPerameterizedQuery;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
public class NamedPreparedStatement extends PreparedStatementImpl {
private enum FormatType {
NULL, BOOLEAN, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BIGDECIMAL, STRING, STRINGLIST, DATE, TIME, TIMESTAMP
}
private String originalSQL;
private final List<String> lstParameters;
public static NamedPreparedStatement prepareStatement(Connection conn, String sql) throws SQLException {
List<String> orderedParameters = new ArrayList<String>();
int length = sql.length();
StringBuffer parsedQuery = new StringBuffer(length);
boolean inSingleQuote = false;
boolean inDoubleQuote = false;
boolean inSingleLineComment = false;
boolean inMultiLineComment = false;
for (int i = 0; i < length; i++) {
char c = sql.charAt(i);
if (inSingleQuote) {
if (c == '\'') {
inSingleQuote = false;
}
} else if (inDoubleQuote) {
if (c == '"') {
inDoubleQuote = false;
}
} else if (inMultiLineComment) {
if (c == '*' && sql.charAt(i + 1) == '/') {
inMultiLineComment = false;
}
} else if (inSingleLineComment) {
if (c == '\n') {
inSingleLineComment = false;
}
} else if (c == '\'') {
inSingleQuote = true;
} else if (c == '"') {
inDoubleQuote = true;
} else if (c == '/' && sql.charAt(i + 1) == '*') {
inMultiLineComment = true;
} else if (c == '-' && sql.charAt(i + 1) == '-') {
inSingleLineComment = true;
} else if (c == ':' && i + 1 < length && Character.isJavaIdentifierStart(sql.charAt(i + 1))) {
int j = i + 2;
while (j < length && Character.isJavaIdentifierPart(sql.charAt(j))) {
j++;
}
String name = sql.substring(i + 1, j);
orderedParameters.add(name);
c = '?';
i += name.length();
}
parsedQuery.append(c);
}
return new NamedPreparedStatement(conn.prepareStatement(parsedQuery.toString()), sql, orderedParameters);
}
private NamedPreparedStatement(PreparedStatement preparedStatement, String originalSQL, List<String> orderedParameters) {
super(preparedStatement);
this.originalSQL = originalSQL.trim();
this.lstParameters = orderedParameters;
}
private Collection<Integer> getParameterIndexes(String parameter) {
Collection<Integer> indexes = new ArrayList<Integer>();
for (int i = 0; i < lstParameters.size(); i++) {
if (lstParameters.get(i).equalsIgnoreCase(parameter)) {
indexes.add(i + 1);
}
}
if (indexes.isEmpty()) {
throw new IllegalArgumentException(String.format("SQL statement doesn't contain the parameter '%s'",
parameter));
}
return indexes;
}
public void setNull(String parameter, int sqlType) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setNull(i, sqlType);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((String) null, FormatType.NULL)));
}
}
public void setBoolean(String parameter, boolean x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setBoolean(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Boolean) x, FormatType.BOOLEAN)));
}
}
public void setByte(String parameter, byte x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setByte(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Byte) x, FormatType.BYTE)));
}
}
public void setShort(String parameter, short x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setShort(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Short) x, FormatType.SHORT)));
}
}
public void setInt(String parameter, int x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setInt(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Integer) x, FormatType.INTEGER)));
}
}
public void setLong(String parameter, long x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setLong(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Long) x, FormatType.LONG)));
}
}
public void setFloat(String parameter, float x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setFloat(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Float) x, FormatType.FLOAT)));
}
}
public void setDouble(String parameter, double x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setDouble(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Double) x, FormatType.DOUBLE)));
}
}
public void setBigDecimal(String parameter, BigDecimal x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setBigDecimal(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((BigDecimal) x, FormatType.BIGDECIMAL)));
}
}
public void setString(String parameter, String x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setString(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((String) x, FormatType.STRING)));
}
}
public void setBytes(String parameter, byte[] x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setBytes(i, x);
String fval = "";
for (int j = 0; j < x.length; j++) {
fval += (char) x[j] + ",";
}
if (fval.endsWith(",")) {
fval = fval.substring(0, fval.length() - 1);
}
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((String) fval, FormatType.STRING)));
}
}
public void setDate(String parameter, Date x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setDate(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Date) x, FormatType.DATE)));
}
}
public void setTime(String parameter, Time x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setTime(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Time) x, FormatType.TIME)));
}
}
public void setTimestamp(String parameter, Timestamp x) throws SQLException {
for (Integer i : getParameterIndexes(parameter)) {
getPreparedStatement().setTimestamp(i, x);
this.originalSQL = this.originalSQL.replaceFirst("(?i):" + parameter, Matcher.quoteReplacement(format((Timestamp) x, FormatType.TIMESTAMP)));
}
}
public String getQuery() {
return this.originalSQL.trim();
}
private String format(Object o, FormatType type) {
String returnParam = "";
try {
switch (type) {
case NULL:
returnParam = "NULL";
break;
case BIGDECIMAL:
returnParam = ((o == null) ? "NULL" : "'" + ((BigDecimal) o).toString() + "'");
break;
case BOOLEAN:
returnParam = ((o == null) ? "NULL" : "'" + (((Boolean) o == Boolean.TRUE) ? "1" : "0") + "'");
break;
case BYTE:
returnParam = ((o == null) ? "NULL" : "'" + ((Byte) o).intValue() + "'");
break;
case DATE:
returnParam = ((o == null) ? "NULL" : "'" + new SimpleDateFormat("yyyy-MM-dd").format((Date) o) + "'");
break;
case DOUBLE:
returnParam = ((o == null) ? "NULL" : "'" + ((Double) o).toString() + "'");
break;
case FLOAT:
returnParam = ((o == null) ? "NULL" : "'" + ((Float) o).toString() + "'");
break;
case INTEGER:
returnParam = ((o == null) ? "NULL" : "'" + ((Integer) o).toString() + "'");
break;
case LONG:
returnParam = ((o == null) ? "NULL" : "'" + ((Long) o).toString() + "'");
break;
case SHORT:
returnParam = ((o == null) ? "NULL" : "'" + ((Short) o).toString() + "'");
break;
case STRING:
returnParam = ((o == null) ? "NULL" : "'" + o.toString() + "'");
break;
case STRINGLIST:
returnParam = ((o == null) ? "NULL" : "'" + o.toString() + "'");
break;
case TIME:
returnParam = ((o == null) ? "NULL" : "'" + new SimpleDateFormat("hh:mm:ss a").format(o) + "'");
break;
case TIMESTAMP:
returnParam = ((o == null) ? "NULL" : "'" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a").format(o) + "'");
break;
default:
break;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return returnParam.trim();
}
}
PreparedStatementImpl.java
package com.codemeright.namedPerameterizedQuery;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
class PreparedStatementImpl implements PreparedStatement {
private final PreparedStatement preparedStatement;
protected PreparedStatement getPreparedStatement() {
return preparedStatement;
}
public PreparedStatementImpl(PreparedStatement preparedStatement) {
this.preparedStatement = preparedStatement;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return preparedStatement.unwrap(iface);
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
return preparedStatement.executeQuery(sql);
}
@Override
public ResultSet executeQuery() throws SQLException {
return preparedStatement.executeQuery();
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return preparedStatement.isWrapperFor(iface);
}
@Override
public int executeUpdate(String sql) throws SQLException {
return preparedStatement.executeUpdate(sql);
}
@Override
public int executeUpdate() throws SQLException {
return preparedStatement.executeUpdate();
}
@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
preparedStatement.setNull(parameterIndex, sqlType);
}
@Override
public void close() throws SQLException {
preparedStatement.close();
}
@Override
public int getMaxFieldSize() throws SQLException {
return preparedStatement.getMaxFieldSize();
}
@Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
preparedStatement.setBoolean(parameterIndex, x);
}
@Override
public void setByte(int parameterIndex, byte x) throws SQLException {
preparedStatement.setByte(parameterIndex, x);
}
@Override
public void setMaxFieldSize(int max) throws SQLException {
preparedStatement.setMaxFieldSize(max);
}
@Override
public void setShort(int parameterIndex, short x) throws SQLException {
preparedStatement.setShort(parameterIndex, x);
}
@Override
public int getMaxRows() throws SQLException {
return preparedStatement.getMaxRows();
}
@Override
public void setInt(int parameterIndex, int x) throws SQLException {
preparedStatement.setInt(parameterIndex, x);
}
@Override
public void setMaxRows(int max) throws SQLException {
preparedStatement.setMaxRows(max);
}
@Override
public void setLong(int parameterIndex, long x) throws SQLException {
preparedStatement.setLong(parameterIndex, x);
}
@Override
public void setEscapeProcessing(boolean enable) throws SQLException {
preparedStatement.setEscapeProcessing(enable);
}
@Override
public void setFloat(int parameterIndex, float x) throws SQLException {
preparedStatement.setFloat(parameterIndex, x);
}
@Override
public void setDouble(int parameterIndex, double x) throws SQLException {
preparedStatement.setDouble(parameterIndex, x);
}
@Override
public int getQueryTimeout() throws SQLException {
return preparedStatement.getQueryTimeout();
}
@Override
public void setQueryTimeout(int seconds) throws SQLException {
preparedStatement.setQueryTimeout(seconds);
}
@Override
public void setBigDecimal(int parameterIndex, BigDecimal x)
throws SQLException {
preparedStatement.setBigDecimal(parameterIndex, x);
}
@Override
public void setString(int parameterIndex, String x) throws SQLException {
preparedStatement.setString(parameterIndex, x);
}
@Override
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
preparedStatement.setBytes(parameterIndex, x);
}
@Override
public void cancel() throws SQLException {
preparedStatement.cancel();
}
@Override
public SQLWarning getWarnings() throws SQLException {
return preparedStatement.getWarnings();
}
@Override
public void setDate(int parameterIndex, Date x) throws SQLException {
preparedStatement.setDate(parameterIndex, x);
}
@Override
public void setTime(int parameterIndex, Time x) throws SQLException {
preparedStatement.setTime(parameterIndex, x);
}
@Override
public void clearWarnings() throws SQLException {
preparedStatement.clearWarnings();
}
@Override
public void setCursorName(String name) throws SQLException {
preparedStatement.setCursorName(name);
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x)
throws SQLException {
preparedStatement.setTimestamp(parameterIndex, x);
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, int length)
throws SQLException {
preparedStatement.setAsciiStream(parameterIndex, x, length);
}
@Override
public boolean execute(String sql) throws SQLException {
return preparedStatement.execute(sql);
}
@Deprecated
@Override
public void setUnicodeStream(int parameterIndex, InputStream x, int length)
throws SQLException {
preparedStatement.setUnicodeStream(parameterIndex, x, length);
}
@Override
public ResultSet getResultSet() throws SQLException {
return preparedStatement.getResultSet();
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length)
throws SQLException {
preparedStatement.setBinaryStream(parameterIndex, x, length);
}
@Override
public int getUpdateCount() throws SQLException {
return preparedStatement.getUpdateCount();
}
@Override
public boolean getMoreResults() throws SQLException {
return preparedStatement.getMoreResults();
}
@Override
public void clearParameters() throws SQLException {
preparedStatement.clearParameters();
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType)
throws SQLException {
preparedStatement.setObject(parameterIndex, x, targetSqlType);
}
@Override
public void setFetchDirection(int direction) throws SQLException {
preparedStatement.setFetchDirection(direction);
}
@Override
public int getFetchDirection() throws SQLException {
return preparedStatement.getFetchDirection();
}
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
preparedStatement.setObject(parameterIndex, x);
}
@Override
public void setFetchSize(int rows) throws SQLException {
preparedStatement.setFetchSize(rows);
}
@Override
public int getFetchSize() throws SQLException {
return preparedStatement.getFetchSize();
}
@Override
public int getResultSetConcurrency() throws SQLException {
return preparedStatement.getResultSetConcurrency();
}
@Override
public boolean execute() throws SQLException {
return preparedStatement.execute();
}
@Override
public int getResultSetType() throws SQLException {
return preparedStatement.getResultSetType();
}
@Override
public void addBatch(String sql) throws SQLException {
preparedStatement.addBatch(sql);
}
@Override
public void clearBatch() throws SQLException {
preparedStatement.clearBatch();
}
@Override
public void addBatch() throws SQLException {
preparedStatement.addBatch();
}
@Override
public int[] executeBatch() throws SQLException {
return preparedStatement.executeBatch();
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, int length)
throws SQLException {
preparedStatement.setCharacterStream(parameterIndex, reader, length);
}
@Override
public void setRef(int parameterIndex, Ref x) throws SQLException {
preparedStatement.setRef(parameterIndex, x);
}
@Override
public void setBlob(int parameterIndex, Blob x) throws SQLException {
preparedStatement.setBlob(parameterIndex, x);
}
@Override
public void setClob(int parameterIndex, Clob x) throws SQLException {
preparedStatement.setClob(parameterIndex, x);
}
@Override
public Connection getConnection() throws SQLException {
return preparedStatement.getConnection();
}
@Override
public void setArray(int parameterIndex, Array x) throws SQLException {
preparedStatement.setArray(parameterIndex, x);
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
return preparedStatement.getMetaData();
}
@Override
public boolean getMoreResults(int current) throws SQLException {
return preparedStatement.getMoreResults(current);
}
@Override
public void setDate(int parameterIndex, Date x, Calendar cal)
throws SQLException {
preparedStatement.setDate(parameterIndex, x, cal);
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
return preparedStatement.getGeneratedKeys();
}
@Override
public void setTime(int parameterIndex, Time x, Calendar cal)
throws SQLException {
preparedStatement.setTime(parameterIndex, x, cal);
}
@Override
public int executeUpdate(String sql, int autoGeneratedKeys)
throws SQLException {
return preparedStatement.executeUpdate(sql, autoGeneratedKeys);
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
throws SQLException {
preparedStatement.setTimestamp(parameterIndex, x, cal);
}
@Override
public void setNull(int parameterIndex, int sqlType, String typeName)
throws SQLException {
preparedStatement.setNull(parameterIndex, sqlType, typeName);
}
@Override
public int executeUpdate(String sql, int[] columnIndexes)
throws SQLException {
return preparedStatement.executeUpdate(sql, columnIndexes);
}
@Override
public void setURL(int parameterIndex, URL x) throws SQLException {
preparedStatement.setURL(parameterIndex, x);
}
@Override
public int executeUpdate(String sql, String[] columnNames)
throws SQLException {
return preparedStatement.executeUpdate(sql, columnNames);
}
@Override
public ParameterMetaData getParameterMetaData() throws SQLException {
return preparedStatement.getParameterMetaData();
}
@Override
public void setRowId(int parameterIndex, RowId x) throws SQLException {
preparedStatement.setRowId(parameterIndex, x);
}
@Override
public void setNString(int parameterIndex, String value)
throws SQLException {
preparedStatement.setNString(parameterIndex, value);
}
@Override
public boolean execute(String sql, int autoGeneratedKeys)
throws SQLException {
return preparedStatement.execute(sql, autoGeneratedKeys);
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value,
long length) throws SQLException {
preparedStatement.setNCharacterStream(parameterIndex, value, length);
}
@Override
public void setNClob(int parameterIndex, NClob value) throws SQLException {
preparedStatement.setNClob(parameterIndex, value);
}
@Override
public void setClob(int parameterIndex, Reader reader, long length)
throws SQLException {
preparedStatement.setClob(parameterIndex, reader, length);
}
@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
return preparedStatement.execute(sql, columnIndexes);
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream, long length)
throws SQLException {
preparedStatement.setBlob(parameterIndex, inputStream, length);
}
@Override
public void setNClob(int parameterIndex, Reader reader, long length)
throws SQLException {
preparedStatement.setNClob(parameterIndex, reader, length);
}
@Override
public boolean execute(String sql, String[] columnNames)
throws SQLException {
return preparedStatement.execute(sql, columnNames);
}
@Override
public void setSQLXML(int parameterIndex, SQLXML xmlObject)
throws SQLException {
preparedStatement.setSQLXML(parameterIndex, xmlObject);
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType,
int scaleOrLength) throws SQLException {
preparedStatement.setObject(parameterIndex, x, targetSqlType, scaleOrLength);
}
@Override
public int getResultSetHoldability() throws SQLException {
return preparedStatement.getResultSetHoldability();
}
@Override
public boolean isClosed() throws SQLException {
return preparedStatement.isClosed();
}
@Override
public void setPoolable(boolean poolable) throws SQLException {
preparedStatement.setPoolable(poolable);
}
@Override
public boolean isPoolable() throws SQLException {
return preparedStatement.isPoolable();
}
@Override
public void closeOnCompletion() throws SQLException {
preparedStatement.closeOnCompletion();
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, long length)
throws SQLException {
preparedStatement.setAsciiStream(parameterIndex, x, length);
}
@Override
public boolean isCloseOnCompletion() throws SQLException {
return preparedStatement.isCloseOnCompletion();
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, long length)
throws SQLException {
preparedStatement.setBinaryStream(parameterIndex, x, length);
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader,
long length) throws SQLException {
preparedStatement.setCharacterStream(parameterIndex, reader, length);
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x)
throws SQLException {
preparedStatement.setAsciiStream(parameterIndex, x);
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x)
throws SQLException {
preparedStatement.setBinaryStream(parameterIndex, x);
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader)
throws SQLException {
preparedStatement.setCharacterStream(parameterIndex, reader);
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value)
throws SQLException {
preparedStatement.setNCharacterStream(parameterIndex, value);
}
@Override
public void setClob(int parameterIndex, Reader reader) throws SQLException {
preparedStatement.setClob(parameterIndex, reader);
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream)
throws SQLException {
preparedStatement.setBlob(parameterIndex, inputStream);
}
@Override
public void setNClob(int parameterIndex, Reader reader) throws SQLException {
preparedStatement.setNClob(parameterIndex, reader);
}
}
所以,现在您已经看到了自定义类NamedParameterizedQuery.java 和 ParameterizedQueryImpl.java。 此外,您还知道如何使用上述示例中的这些类创建命名参数化查询。如果你好奇这些类是如何工作的,你可以在 debud 模式下测试它们以了解它的功能。