JDBC의 명명 된 매개 변수
등 JDBC 대신 위치들에서 파라미터를,이 이름이 있습니까 @name
, @city
아래의 ADO.NET 쿼리에?
select * from customers where name=@name and city = @city
JDBC는 명명 된 매개 변수를 지원하지 않습니다. 일반 JDBC (고통을 유발하는)를 사용하지 않는 한 전체 IoC 컨테이너없이 사용할 수있는 Springs Excellent JDBCTemplate을 사용하는 것이 좋습니다.
NamedParameterJDBCTemplate 은 명명 된 매개 변수를 지원하므로 다음과 같이 사용할 수 있습니다.
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("name", name);
paramSource.addValue("city", city);
jdbcTemplate.queryForRowSet("SELECT * FROM customers WHERE name = :name AND city = :city", paramSource);
큰 프레임 워크를 포함하지 않으려면 간단한 수제 클래스가 트릭을 수행 할 수 있다고 생각합니다.
명명 된 매개 변수를 처리하는 클래스의 예 :
public class NamedParamStatement {
public NamedParamStatement(Connection conn, String sql) throws SQLException {
int pos;
while((pos = sql.indexOf(":")) != -1) {
int end = sql.substring(pos).indexOf(" ");
if (end == -1)
end = sql.length();
else
end += pos;
fields.add(sql.substring(pos+1,end));
sql = sql.substring(0, pos) + "?" + sql.substring(end);
}
prepStmt = conn.prepareStatement(sql);
}
public PreparedStatement getPreparedStatement() {
return prepStmt;
}
public ResultSet executeQuery() throws SQLException {
return prepStmt.executeQuery();
}
public void close() throws SQLException {
prepStmt.close();
}
public void setInt(String name, int value) throws SQLException {
prepStmt.setInt(getIndex(name), value);
}
private int getIndex(String name) {
return fields.indexOf(name)+1;
}
private PreparedStatement prepStmt;
private List<String> fields = new ArrayList<String>();
}
클래스 호출의 예 :
String sql;
sql = "SELECT id, Name, Age, TS FROM TestTable WHERE Age < :age OR id = :id";
NamedParamStatement stmt = new NamedParamStatement(conn, sql);
stmt.setInt("age", 35);
stmt.setInt("id", 2);
ResultSet rs = stmt.executeQuery();
위의 간단한 예제는 명명 된 매개 변수를 두 번 사용하는 것을 처리하지 않습니다. 따옴표 안에 : 기호를 사용하여 처리하지도 않습니다.
Vanilla JDBC only supports named parameters in a CallableStatement
(e.g. setString("name", name)
), and even then, I suspect the underlying stored procedure implementation has to support it.
An example of how to use named parameters:
//uss Sybase ASE sysobjects table...adjust for your RDBMS
stmt = conn.prepareCall("create procedure p1 (@id int = null, @name varchar(255) = null) as begin "
+ "if @id is not null "
+ "select * from sysobjects where id = @id "
+ "else if @name is not null "
+ "select * from sysobjects where name = @name "
+ " end");
stmt.execute();
//call the proc using one of the 2 optional params
stmt = conn.prepareCall("{call p1 ?}");
stmt.setInt("@id", 10);
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
System.out.println(rs.getString(1));
}
//use the other optional param
stmt = conn.prepareCall("{call p1 ?}");
stmt.setString("@name", "sysprocedures");
rs = stmt.executeQuery();
while (rs.next())
{
System.out.println(rs.getString(1));
}
You can't use named parameters in JDBC itself. You could try using Spring framework, as it has some extensions that allow the use of named parameters in queries.
Plain vanilla JDBC does not support named parameters.
If you are using DB2 then using DB2 classes directly:
- Using named parameter markers with PreparedStatement objects
- Using named parameter markers with CallableStatement objects
참고URL : https://stackoverflow.com/questions/2309970/named-parameters-in-jdbc
'program story' 카테고리의 다른 글
비동기 메서드가 'Async'로 끝나지 않을 때 Visual Studio에서 경고를 받으려면 어떻게하나요? (0) | 2020.11.08 |
---|---|
EasyMock : 무효 방법 (0) | 2020.11.08 |
C #에서 문자열과 StringBuilder의 차이점 (0) | 2020.11.08 |
302 리디렉션 중 브라우저 쿠키 보내기 (0) | 2020.11.08 |
cells (1,1)을“A1”로 또는 그 반대로 변환 (0) | 2020.11.08 |