use of java.sql.ParameterMetaData in project phoenix by apache.
the class QueryMetaDataTest method testRoundParameterMetaData.
@Test
public void testRoundParameterMetaData() throws Exception {
String query = "SELECT a_string, b_string FROM atable WHERE round(a_date,'day', ?) = ?";
Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES));
PreparedStatement statement = conn.prepareStatement(query);
ParameterMetaData pmd = statement.getParameterMetaData();
assertEquals(2, pmd.getParameterCount());
assertEquals(Integer.class.getName(), pmd.getParameterClassName(1));
assertEquals(Date.class.getName(), pmd.getParameterClassName(2));
}
use of java.sql.ParameterMetaData in project phoenix by apache.
the class QueryMetaDataTest method testRowValueConstructorBindParamMetaDataWithLessNumberOfBindArgs.
@Test
public void testRowValueConstructorBindParamMetaDataWithLessNumberOfBindArgs() throws Exception {
String query = "SELECT a_integer, x_integer FROM aTable WHERE (a_integer, x_integer, a_string) = (?, ?)";
Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES));
PreparedStatement statement = conn.prepareStatement(query);
ParameterMetaData pmd = statement.getParameterMetaData();
assertEquals(2, pmd.getParameterCount());
assertEquals(Integer.class.getName(), pmd.getParameterClassName(1));
assertEquals(Integer.class.getName(), pmd.getParameterClassName(2));
}
use of java.sql.ParameterMetaData in project dbeaver by dbeaver.
the class OraclePlanAnalyser method explain.
public void explain() throws DBException {
String planQuery = getPlanQueryString();
try {
// Delete previous statement rows
// (actually there should be no statement with this id -
// but let's do it, just in case)
JDBCPreparedStatement dbStat = session.prepareStatement("DELETE FROM " + planTableName + " WHERE STATEMENT_ID=? ");
try {
dbStat.setString(1, planStmtId);
dbStat.execute();
} finally {
dbStat.close();
}
// Explain plan
dbStat = session.prepareStatement(planQuery);
try {
try {
// Bind parameters if any
ParameterMetaData parameterMetaData = dbStat.getParameterMetaData();
if (parameterMetaData != null && parameterMetaData.getParameterCount() > 0) {
for (int i = 0; i < parameterMetaData.getParameterCount(); i++) {
dbStat.setNull(i + 1, Types.VARCHAR);
}
}
} catch (Exception e) {
log.error(e);
}
dbStat.execute();
} finally {
dbStat.close();
}
// Read explained plan
dbStat = session.prepareStatement("SELECT * FROM " + planTableName + " WHERE STATEMENT_ID=? ORDER BY ID");
try {
dbStat.setString(1, planStmtId);
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
rootNodes = new ArrayList<>();
IntKeyMap<OraclePlanNode> allNodes = new IntKeyMap<>();
while (dbResult.next()) {
OraclePlanNode node = new OraclePlanNode(dataSource, allNodes, dbResult);
allNodes.put(node.getId(), node);
if (node.getParent() == null) {
rootNodes.add(node);
}
}
}
} finally {
dbStat.close();
}
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
}
}
use of java.sql.ParameterMetaData in project hutool by looly.
the class DbUtil method fillParams.
/**
* 填充SQL的参数。<br>
* 对于日期对象特殊处理:传入java.util.Date默认按照Timestamp处理
*
* @param ps PreparedStatement
* @param params SQL参数
* @return {@link PreparedStatement}
* @throws SQLException SQL执行异常
*/
public static PreparedStatement fillParams(PreparedStatement ps, Object... params) throws SQLException {
if (ArrayUtil.isEmpty(params)) {
// 无参数
return ps;
}
Object param;
for (int i = 0; i < params.length; i++) {
int paramIndex = i + 1;
param = params[i];
if (null != param) {
if (param instanceof java.util.Date) {
// 日期特殊处理
if (param instanceof java.sql.Date) {
ps.setDate(paramIndex, (java.sql.Date) param);
} else if (param instanceof java.sql.Time) {
ps.setTime(paramIndex, (java.sql.Time) param);
} else {
ps.setTimestamp(paramIndex, toSqlTimestamp((java.util.Date) param));
}
} else if (param instanceof Number) {
// 针对大数字类型的特殊处理
if (param instanceof BigInteger) {
// BigInteger转为Long
ps.setLong(paramIndex, ((BigInteger) param).longValue());
} else if (param instanceof BigDecimal) {
// BigDecimal的转换交给JDBC驱动处理
ps.setBigDecimal(paramIndex, (BigDecimal) param);
} else {
// 普通数字类型按照默认传入
ps.setObject(paramIndex, param);
}
} else {
ps.setObject(paramIndex, param);
}
} else {
final ParameterMetaData pmd = ps.getParameterMetaData();
int sqlType = Types.VARCHAR;
try {
sqlType = pmd.getParameterType(paramIndex);
} catch (SQLException e) {
// ignore
// log.warn("Null param of index [{}] type get failed, by: {}", paramIndex, e.getMessage());
}
ps.setNull(paramIndex, sqlType);
}
}
return ps;
}
use of java.sql.ParameterMetaData in project javautils by jiadongpo.
the class Dbutil method update.
/**
* 基于PreparedStatement的修改方法 PreparedStatement:表示预编译的 SQL 语句的对象
*
* @param sql
* String 类型的SQL语句(insert delete update)
* @param obj
* 存放动态参数的数组
* @return Integer 表示受影响的行数
*/
public int update(String sql, Object... obj) {
try {
// 获取链接
if (conn == null || conn.isClosed()) {
conn = getConnection();
}
// 创建预编译的 SQL 语句对象
pps = conn.prepareStatement(sql);
// 定义变量length代表数组长度,也就是预处理的sql语句中的参数个数
int length = 0;
// ParameterMetaData:用于获取关于 PreparedStatement 对象中每个参数的类型和属性信息的对象
ParameterMetaData pmd = pps.getParameterMetaData();
length = pmd.getParameterCount();
// 循环将sql语句中的?设置为obj数组中对应的值,注意从1开始,所以i要加1
for (int i = 0; i < length; i++) {
pps.setObject(i + 1, obj[i]);
}
// 执行更新操作
return pps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return -1;
}
Aggregations