Search in sources :

Example 81 with ParameterMetaData

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));
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ParameterMetaData(java.sql.ParameterMetaData) Date(java.sql.Date) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 82 with ParameterMetaData

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));
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ParameterMetaData(java.sql.ParameterMetaData) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 83 with ParameterMetaData

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());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) IntKeyMap(org.jkiss.utils.IntKeyMap) DBCException(org.jkiss.dbeaver.model.exec.DBCException) ParameterMetaData(java.sql.ParameterMetaData) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException)

Example 84 with ParameterMetaData

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;
}
Also used : SQLException(java.sql.SQLException) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ParameterMetaData(java.sql.ParameterMetaData)

Example 85 with ParameterMetaData

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;
}
Also used : SQLException(java.sql.SQLException) ParameterMetaData(java.sql.ParameterMetaData)

Aggregations

ParameterMetaData (java.sql.ParameterMetaData)136 PreparedStatement (java.sql.PreparedStatement)82 Test (org.junit.Test)55 Connection (java.sql.Connection)45 SQLException (java.sql.SQLException)35 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)31 ResultSet (java.sql.ResultSet)22 Statement (java.sql.Statement)20 ResultSetMetaData (java.sql.ResultSetMetaData)18 SQLServerParameterMetaData (com.microsoft.sqlserver.jdbc.SQLServerParameterMetaData)15 BigDecimal (java.math.BigDecimal)12 CallableStatement (java.sql.CallableStatement)12 Test (org.junit.jupiter.api.Test)12 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)11 UnitTest (nl.topicus.jdbc.test.category.UnitTest)9 StringContains.containsString (org.hamcrest.core.StringContains.containsString)9 Date (java.sql.Date)7 DisplayName (org.junit.jupiter.api.DisplayName)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5