mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -15,8 +15,10 @@
|
||||
*/
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.Closeable;
|
||||
@@ -740,4 +742,31 @@ public final class JdbcUtils implements JdbcConstants {
|
||||
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
public static <T> void executeQuery(DataSource dataSource
|
||||
, ResultSetConsumer<T> consumer
|
||||
, String sql
|
||||
, Object... parameters) throws SQLException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
stmt = conn.prepareStatement(sql);
|
||||
for (int i = 0; i < parameters.length; ++i) {
|
||||
stmt.setObject(i + 1, parameters[i]);
|
||||
}
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
if (consumer != null) {
|
||||
T object = consumer.apply(rs);
|
||||
consumer.accept(object);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
close(rs);
|
||||
close(stmt);
|
||||
close(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user