Java Code Examples for java.sql.CallableStatement#setDouble()

The following examples show how to use java.sql.CallableStatement#setDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CallableStatementRegressionTest.java    From r-course with MIT License 6 votes vote down vote up
/**
 * Tests fix for BUG#9682 - Stored procedures with DECIMAL parameters with
 * storage specifications that contained "," in them would fail.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug9682() throws Exception {
    if (!serverSupportsStoredProcedures()) {
        return;
    }

    createProcedure("testBug9682", "(decimalParam DECIMAL(18,0))\nBEGIN\n   SELECT 1;\nEND");

    CallableStatement cStmt = null;

    try {
        cStmt = this.conn.prepareCall("Call testBug9682(?)");
        cStmt.setDouble(1, 18.0);
        cStmt.execute();
    } finally {
        if (cStmt != null) {
            cStmt.close();
        }
    }
}
 
Example 2
Source File: JdbcLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenCallProcedure_thenCorrect() {

    try {
        String preparedSql = "{call insertEmployee(?,?,?,?)}";
        CallableStatement cstmt = con.prepareCall(preparedSql);
        cstmt.setString(2, "ana");
        cstmt.setString(3, "tester");
        cstmt.setDouble(4, 2000);
        cstmt.registerOutParameter(1, Types.INTEGER);
        cstmt.execute();
        int new_id = cstmt.getInt(1);
        assertTrue(new_id > 0);
    } catch (SQLException exc) {
        LOG.error("Procedure incorrect or does not exist!");
    }
}
 
Example 3
Source File: GpsDao.java    From nearbydemo with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 根据latitude和longitude获取到geohash
 * @param latitude
 * @param longitude
 * @param precision
 * @return
 */
public String getGeoHash(double latitude, double longitude, int precision) {
	String geohash = "";
	Connection conn = null;
	ResultSet rs = null;
	CallableStatement cstmt = null;
	String func = "select geohash_encode(?, ?,?)";// 不能加{}
	try {
		conn = DBUtil.getDBConn();// 首先要获取连接,即连接到数据库
		cstmt = conn.prepareCall(func);
		cstmt.setDouble(1, latitude);
		cstmt.setDouble(2, longitude);
		cstmt.setInt(3, precision);
		cstmt.execute();
		rs = cstmt.getResultSet();
		if (rs.next()) {
			geohash = rs.getString(1);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}finally {
		DBUtil.closeDB(rs, cstmt, conn);//关闭资源很重要
	}
	return geohash;
}
 
Example 4
Source File: JDBCInsertGPS.java    From nearbydemo with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * latitude , longitude , precision
 * @param latitude
 * @param longitude
 * @param precision
 * @return
 */
public static String getGeoHash(double latitude,double longitude,int precision) {
	conn = getConnection(); // ����Ҫ��ȡ���ӣ������ӵ����ݿ�
	String func = "select geohash_encode(?, ?,?)";//���ܼ�{}
	try {
		CallableStatement cstmt = conn.prepareCall(func);
		cstmt.setDouble(1, latitude);
		cstmt.setDouble(2, longitude);
		cstmt.setInt(3, precision);
		cstmt.execute();
		ResultSet rs = cstmt.getResultSet();
		if (rs.next()) {
			 return rs.getString(1);
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return "";
}
 
Example 5
Source File: JDBCInsertGPS.java    From nearbydemo with Eclipse Public License 1.0 6 votes vote down vote up
public void testFun(){
	conn = getConnection(); // ����Ҫ��ȡ���ӣ������ӵ����ݿ�
	String func = "select CustomerLevel(?)";//���ܼ�{}
	try {
		CallableStatement cstmt = conn.prepareCall(func);
		cstmt.setDouble(1, 50000000);
		cstmt.execute();
		ResultSet rs = cstmt.getResultSet();
		if (rs.next()) {
			 System.out.println(rs.getString(1));
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 
Example 6
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes INOUT parameters of various types.
 * @throws SQLException 
 */
public void testManyTypesInoutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call MANY_TYPES_INOUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.registerOutParameter (2, java.sql.Types.SMALLINT);
    cs.registerOutParameter (4, java.sql.Types.INTEGER);
    cs.registerOutParameter (6, java.sql.Types.BIGINT);
    cs.registerOutParameter (8, java.sql.Types.REAL);
    cs.registerOutParameter (10, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.TIME);

    cs.setShort(1, (short)6);
    cs.setShort(2, (short)9);
    cs.setInt(3, 6);
    cs.setInt(4, 9);
    cs.setLong(5, (long)99999);
    cs.setLong(6, (long)88888888);
    cs.setFloat(7, (float)6.123453);
    cs.setFloat(8, (float)77777);
    cs.setDouble(9, (double)6.123453);
    cs.setDouble(10, (double)8888888888888.01234);
    cs.setTime(11, Time.valueOf("11:06:03"));
    cs.setTime(12, Time.valueOf("10:05:02"));

    cs.execute();

    assertEquals("Short: Sum of 6 + 9", 15, cs.getShort(2));
    assertEquals("Int: Sum of 6 + 9", 15, cs.getInt(4));
    assertEquals("Long: Sum of 99999 + 88888888", 88988887, cs.getLong(6));
    assertEquals("Float: Sum of 6.123453 and 77777" , (float) 77783.123453,
        cs.getFloat(8), .000001);
    assertEquals("Double: Sum of Sum of 6.987654 and 8888888888888.01234",
        8.888888888894135e12, cs.getDouble(10), .000001);
    assertEquals("Time: changed to", Time.valueOf("11:06:03"), 
        cs.getTime(12));

}
 
Example 7
Source File: UseCase1Client.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void setHeapPercentage(double percentage)
throws SQLException {
  String sql = "call SYS.SET_EVICTION_HEAP_PERCENTAGE_SG(?,?)";
  CallableStatement cs = this.connection.prepareCall(sql);
  Log.getLogWriter().info("CALL SYS.SET_EVICTION_HEAP_PERCENTAGE_SG(" + percentage + ", null)");
  cs.setDouble(1, percentage);
  cs.setNull(2, Types.VARCHAR);
  cs.executeUpdate();
}
 
Example 8
Source File: BackupRestoreBigDataTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * This is intended to change the default eviction percentage to the value specified in the
 * sql.backupAndRestore.BackupAndRestorePrms-evictionHeapPercentage parameter.
 */
private static void setEvictionHeapPercentage() {
  try {
    Connection conn = (Connection) threadLocal_gfxdConnection.get();

    String getEvictionHeapPercentageSql = "values SYS.GET_EVICTION_HEAP_PERCENTAGE()";
    ResultSet resultSet = executeSqlQuery(conn, getEvictionHeapPercentageSql);
    resultSet.beforeFirst();
    while (resultSet.next()) {
      logWriter.info("BackupRestoreBigDataTest.setEvictionHeapPercentage-percentage before set=" +
                     resultSet.getDouble(1));
    }

    String setEvictionHeapPercentageSql = "CALL SYS.SET_EVICTION_HEAP_PERCENTAGE(?)";
    double evictionHeapPercentage = BackupAndRestorePrms.getEvictionHeapPercentage();
    logWriter.info("BackupRestoreBigDataTest.setEvictionHeapPercentage-sql=" + setEvictionHeapPercentageSql +
                   " with args " + evictionHeapPercentage);

    CallableStatement cs = conn.prepareCall(setEvictionHeapPercentageSql);
    cs.setDouble(1, evictionHeapPercentage);
    cs.executeUpdate();

    resultSet = executeSqlQuery(conn, getEvictionHeapPercentageSql);
    resultSet.beforeFirst();
    while (resultSet.next()) {
      logWriter.info("BackupRestoreBigDataTest.setEvictionHeapPercentage-percentage after set=" +
                     resultSet.getDouble(1));
    }

  } catch (SQLException se) {
    SQLHelper.handleSQLException(se);
  }
}
 
Example 9
Source File: CallableStatementIT.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrepareCall() throws SQLException
{
  // test CallableStatement with no binding parameters
  connection = getConnection();
  statement = connection.createStatement();
  CallableStatement callableStatement = connection.prepareCall("call square_it(5)");
  assertThat(callableStatement.getParameterMetaData().getParameterCount(), is(0));

  // test CallableStatement with 1 binding parameter
  callableStatement = connection.prepareCall("call square_it(?)");
  // test that getParameterMetaData works with CallableStatement. At this point, it always returns the type as "text."
  assertThat(callableStatement.getParameterMetaData().getParameterType(1), is(Types.VARCHAR));
  callableStatement.getParameterMetaData().getParameterTypeName(1);
  assertThat(callableStatement.getParameterMetaData().getParameterTypeName(1), is("text"));
  callableStatement.setFloat(1, 7.0f);
  ResultSet rs = callableStatement.executeQuery();
  rs.next();
  assertEquals(49.0f, rs.getFloat(1), 1.0f);

  // test CallableStatement with 2 binding parameters
  callableStatement = connection.prepareCall("call add_nums(?,?)");
  callableStatement.setDouble(1, 32);
  callableStatement.setDouble(2, 15);
  rs = callableStatement.executeQuery();
  rs.next();
  assertEquals(47, rs.getDouble(1), .5);
}
 
Example 10
Source File: CallableTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes numeric IN and OUT parameters.
 * Excluded from JSR169/j2ME, which doesn't support get/set BigDecimal yet.
 * @throws SQLException 
 */
public void xtestNumericTypesInAndOutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.setShort(1, (short) 3);
    cs.setInt(2, 4);
    cs.setLong(3, 5);
    cs.setFloat(4, (float) 6.0);
    cs.setDouble(5, 7.0);
    cs.setBigDecimal(6, new BigDecimal("88.88"));

    cs.registerOutParameter (7, java.sql.Types.SMALLINT);
    cs.registerOutParameter (8, java.sql.Types.INTEGER);
    cs.registerOutParameter (9, java.sql.Types.BIGINT);
    cs.registerOutParameter (10, java.sql.Types.REAL);
    cs.registerOutParameter (11, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.DECIMAL);

    cs.execute();

    assertEquals("OUT short", (short) 3, cs.getShort(7));
    assertEquals("OUT int"  , 4, cs.getInt(8));
    assertEquals("OUT long" , 5, cs.getLong(9));
    assertEquals("OUT float" , (float) 6.0, cs.getFloat(10), .0001);
    assertEquals("OUT double" , 7.0, cs.getDouble(11), .0001);
    assertDecimalSameValue("OUT decimal", "88.88", cs.getBigDecimal(12));

    // test that setObject() does the right thing for BigDecimal. see db-5488.
    cs.setObject(3, new BigDecimal( "10" ) );
    cs.execute();
    assertEquals("OUT long" , 10, cs.getLong(9));

    // test that setObject() does the right thing for BigInteger. see db-5488.
    cs.setObject(3, new BigInteger( "11" ) );
    cs.execute();
    assertEquals("OUT long" , 11, cs.getLong(9));
}
 
Example 11
Source File: PostgresServerJDBCFunctionTypesIT.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testIntToStringWithDouble() throws Exception {
    CallableStatement call = getConnection().prepareCall("{ ? = call testspg__intToString (?) }");
    call.setDouble(2, 589.32);
    // JDBC ensures that the OutParameter has the right type
    call.registerOutParameter (1, Types.VARCHAR);
    call.execute ();
    assertEquals("bob589", call.getString(1));
}
 
Example 12
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes numeric IN and OUT parameters.
 * Excluded from JSR169/j2ME, which doesn't support get/set BigDecimal yet.
 * @throws SQLException 
 */
public void xtestNumericTypesInAndOutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.setShort(1, (short) 3);
    cs.setInt(2, 4);
    cs.setLong(3, 5);
    cs.setFloat(4, (float) 6.0);
    cs.setDouble(5, 7.0);
    cs.setBigDecimal(6, new BigDecimal("88.88"));

    cs.registerOutParameter (7, java.sql.Types.SMALLINT);
    cs.registerOutParameter (8, java.sql.Types.INTEGER);
    cs.registerOutParameter (9, java.sql.Types.BIGINT);
    cs.registerOutParameter (10, java.sql.Types.REAL);
    cs.registerOutParameter (11, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.DECIMAL);

    cs.execute();

    assertEquals("OUT short", (short) 3, cs.getShort(7));
    assertEquals("OUT int"  , 4, cs.getInt(8));
    assertEquals("OUT long" , 5, cs.getLong(9));
    assertEquals("OUT float" , (float) 6.0, cs.getFloat(10), .0001);
    assertEquals("OUT double" , 7.0, cs.getDouble(11), .0001);
    assertDecimalSameValue("OUT decimal", "88.88", cs.getBigDecimal(12));
}
 
Example 13
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes INOUT parameters of various types.
 * @throws SQLException 
 */
public void testManyTypesInoutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call MANY_TYPES_INOUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.registerOutParameter (2, java.sql.Types.SMALLINT);
    cs.registerOutParameter (4, java.sql.Types.INTEGER);
    cs.registerOutParameter (6, java.sql.Types.BIGINT);
    cs.registerOutParameter (8, java.sql.Types.REAL);
    cs.registerOutParameter (10, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.TIME);

    cs.setShort(1, (short)6);
    cs.setShort(2, (short)9);
    cs.setInt(3, 6);
    cs.setInt(4, 9);
    cs.setLong(5, (long)99999);
    cs.setLong(6, (long)88888888);
    cs.setFloat(7, (float)6.123453);
    cs.setFloat(8, (float)77777);
    cs.setDouble(9, (double)6.123453);
    cs.setDouble(10, (double)8888888888888.01234);
    cs.setTime(11, Time.valueOf("11:06:03"));
    cs.setTime(12, Time.valueOf("10:05:02"));

    cs.execute();

    assertEquals("Short: Sum of 6 + 9", 15, cs.getShort(2));
    assertEquals("Int: Sum of 6 + 9", 15, cs.getInt(4));
    assertEquals("Long: Sum of 99999 + 88888888", 88988887, cs.getLong(6));
    assertEquals("Float: Sum of 6.123453 and 77777" , (float) 77783.123453,
        cs.getFloat(8), .000001);
    assertEquals("Double: Sum of Sum of 6.987654 and 8888888888888.01234",
        8.888888888894135e12, cs.getDouble(10), .000001);
    assertEquals("Time: changed to", Time.valueOf("11:06:03"), 
        cs.getTime(12));

}
 
Example 14
Source File: UseCase1Client.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void setHeapPercentage(double percentage)
throws SQLException {
  String sql = "call SYS.SET_EVICTION_HEAP_PERCENTAGE_SG(?,?)";
  CallableStatement cs = this.connection.prepareCall(sql);
  Log.getLogWriter().info("CALL SYS.SET_EVICTION_HEAP_PERCENTAGE_SG(" + percentage + ", null)");
  cs.setDouble(1, percentage);
  cs.setNull(2, Types.VARCHAR);
  cs.executeUpdate();
}
 
Example 15
Source File: BackupRestoreBigDataTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * This is intended to change the default eviction percentage to the value specified in the
 * sql.backupAndRestore.BackupAndRestorePrms-evictionHeapPercentage parameter.
 */
private static void setEvictionHeapPercentage() {
  try {
    Connection conn = (Connection) threadLocal_gfxdConnection.get();

    String getEvictionHeapPercentageSql = "values SYS.GET_EVICTION_HEAP_PERCENTAGE()";
    ResultSet resultSet = executeSqlQuery(conn, getEvictionHeapPercentageSql);
    resultSet.beforeFirst();
    while (resultSet.next()) {
      logWriter.info("BackupRestoreBigDataTest.setEvictionHeapPercentage-percentage before set=" +
                     resultSet.getDouble(1));
    }

    String setEvictionHeapPercentageSql = "CALL SYS.SET_EVICTION_HEAP_PERCENTAGE(?)";
    double evictionHeapPercentage = BackupAndRestorePrms.getEvictionHeapPercentage();
    logWriter.info("BackupRestoreBigDataTest.setEvictionHeapPercentage-sql=" + setEvictionHeapPercentageSql +
                   " with args " + evictionHeapPercentage);

    CallableStatement cs = conn.prepareCall(setEvictionHeapPercentageSql);
    cs.setDouble(1, evictionHeapPercentage);
    cs.executeUpdate();

    resultSet = executeSqlQuery(conn, getEvictionHeapPercentageSql);
    resultSet.beforeFirst();
    while (resultSet.next()) {
      logWriter.info("BackupRestoreBigDataTest.setEvictionHeapPercentage-percentage after set=" +
                     resultSet.getDouble(1));
    }

  } catch (SQLException se) {
    SQLHelper.handleSQLException(se);
  }
}
 
Example 16
Source File: JDBCInsertGPS.java    From nearbydemo with Eclipse Public License 1.0 5 votes vote down vote up
public void testFun2(){
	conn = getConnection(); // ����Ҫ��ȡ���ӣ������ӵ����ݿ�
	String func = "{? = call CustomerLevel(?)}";//���ܼ�{}
	try {
		CallableStatement cstmt = conn.prepareCall(func);
		cstmt.registerOutParameter(1, Types.VARCHAR);
		cstmt.setDouble(2, 50000000);
		cstmt.execute();
		System.out.println(cstmt.getString(1));
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 17
Source File: CallableTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes numeric IN and OUT parameters.
 * Excluded from JSR169/j2ME, which doesn't support get/set BigDecimal yet.
 * @throws SQLException 
 */
public void xtestNumericTypesInAndOutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.setShort(1, (short) 3);
    cs.setInt(2, 4);
    cs.setLong(3, 5);
    cs.setFloat(4, (float) 6.0);
    cs.setDouble(5, 7.0);
    cs.setBigDecimal(6, new BigDecimal("88.88"));

    cs.registerOutParameter (7, java.sql.Types.SMALLINT);
    cs.registerOutParameter (8, java.sql.Types.INTEGER);
    cs.registerOutParameter (9, java.sql.Types.BIGINT);
    cs.registerOutParameter (10, java.sql.Types.REAL);
    cs.registerOutParameter (11, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.DECIMAL);

    cs.execute();

    assertEquals("OUT short", (short) 3, cs.getShort(7));
    assertEquals("OUT int"  , 4, cs.getInt(8));
    assertEquals("OUT long" , 5, cs.getLong(9));
    assertEquals("OUT float" , (float) 6.0, cs.getFloat(10), .0001);
    assertEquals("OUT double" , 7.0, cs.getDouble(11), .0001);
    assertDecimalSameValue("OUT decimal", "88.88", cs.getBigDecimal(12));
}
 
Example 18
Source File: CallableTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Calls a SQL procedure that takes INOUT parameters of various types.
 * @throws SQLException 
 */
public void testManyTypesInoutProc() throws SQLException
{
    CallableStatement cs = prepareCall
        ("call MANY_TYPES_INOUT_PROC(?,?,?,?,?,?,?,?,?,?,?,?)");

    cs.registerOutParameter (2, java.sql.Types.SMALLINT);
    cs.registerOutParameter (4, java.sql.Types.INTEGER);
    cs.registerOutParameter (6, java.sql.Types.BIGINT);
    cs.registerOutParameter (8, java.sql.Types.REAL);
    cs.registerOutParameter (10, java.sql.Types.DOUBLE);
    cs.registerOutParameter (12, java.sql.Types.TIME);

    cs.setShort(1, (short)6);
    cs.setShort(2, (short)9);
    cs.setInt(3, 6);
    cs.setInt(4, 9);
    cs.setLong(5, (long)99999);
    cs.setLong(6, (long)88888888);
    cs.setFloat(7, (float)6.123453);
    cs.setFloat(8, (float)77777);
    cs.setDouble(9, (double)6.123453);
    cs.setDouble(10, (double)8888888888888.01234);
    cs.setTime(11, Time.valueOf("11:06:03"));
    cs.setTime(12, Time.valueOf("10:05:02"));

    cs.execute();

    assertEquals("Short: Sum of 6 + 9", 15, cs.getShort(2));
    assertEquals("Int: Sum of 6 + 9", 15, cs.getInt(4));
    assertEquals("Long: Sum of 99999 + 88888888", 88988887, cs.getLong(6));
    assertEquals("Float: Sum of 6.123453 and 77777" , (float) 77783.123453,
        cs.getFloat(8), .000001);
    assertEquals("Double: Sum of Sum of 6.987654 and 8888888888888.01234",
        8.888888888894135e12, cs.getDouble(10), .000001);
    assertEquals("Time: changed to", Time.valueOf("11:06:03"), 
        cs.getTime(12));

}
 
Example 19
Source File: JDBCExecutor.java    From amforeas with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Utility method which registers in a CallableStatement object the different {@link amforeas.jdbc.StoredProcedureParam}
 * instances in the given list. Returns a List of {@link amforeas.jdbc.StoredProcedureParam} with all the OUT parameters
 * registered in the CallableStatement
 * @param cs the CallableStatement object where the parameters are registered.
 * @param params a list of {@link amforeas.jdbc.StoredProcedureParam}
 * @return a list of OUT {@link amforeas.jdbc.StoredProcedureParam} 
 * @throws SQLException if we fail to register any of the parameters in the CallableStatement
 * @throws AmforeasBadRequestException 
 */
private List<StoredProcedureParam> addParameters (final CallableStatement cs, final List<StoredProcedureParam> params) throws SQLException, AmforeasBadRequestException {
    final List<StoredProcedureParam> outParams = new ArrayList<StoredProcedureParam>();
    int i = 1;
    for (StoredProcedureParam p : params) {
        final Integer sqlType = p.getSqlType();
        if (p.isOutParameter()) {
            l.debug("Adding OUT parameter " + p.toString());
            cs.registerOutParameter(i++, sqlType);
            outParams.add(p);
        } else {
            l.debug("Adding IN parameter " + p.toString());
            switch (sqlType) {
                case Types.BIGINT:
                case Types.INTEGER:
                case Types.TINYINT:
                    // case Types.NUMERIC:
                    cs.setInt(i++, Integer.valueOf(p.getValue()));
                    break;
                case Types.DATE:
                    cs.setDate(i++, (Date) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.TIME:
                    cs.setTime(i++, (Time) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.TIMESTAMP:
                    cs.setTimestamp(i++, (Timestamp) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.DECIMAL:
                    cs.setBigDecimal(i++, (BigDecimal) AmforeasUtils.parseValue(p.getValue()));
                    break;
                case Types.DOUBLE:
                    cs.setDouble(i++, Double.valueOf(p.getValue()));
                    break;
                case Types.FLOAT:
                    cs.setLong(i++, Long.valueOf(p.getValue()));
                    break;
                default:
                    cs.setString(i++, p.getValue());
                    break;
            }
        }
    }
    return outParams;
}
 
Example 20
Source File: ProcedureUtils.java    From ureport with Apache License 2.0 4 votes vote down vote up
private static StatementWrapper buildProcedureCallableStatement(String sql,Map<String, Object> pmap,Connection conn){
	try {
		Map<String,Object> paramMap=new LinkedHashMap<String,Object>();
		int leftParnPos=sql.indexOf("(");
		int rightParnPos=sql.indexOf(")");
		String paramStr="";
		if(leftParnPos>-1 && rightParnPos>-1){
			paramStr=sql.substring(leftParnPos+1,rightParnPos);				
		}
		int oracleCursorIndex=-1,paramIndex=0;
		String[] str=paramStr.split(",");
		for(String param:str){
			paramIndex++;
			param=param.trim();
			if(param.toLowerCase().equals("oracle")){
				sql=sql.replaceFirst(param, "?");
				oracleCursorIndex=paramIndex;
				continue;
			}else if(!param.startsWith(":")){
				continue;
			}
			sql=sql.replaceFirst(param, "?");
			String paramName=param.substring(1,param.length());
			Object paramValue=pmap.get(paramName);
			paramMap.put(paramName, (paramValue==null ? "" : paramValue));
		}
		String procedure="{"+sql+"}";
		CallableStatement cs= conn.prepareCall(procedure);
		int index=1;
		for(String name:paramMap.keySet()){
			Object value=paramMap.get(name);
			if(value instanceof String){
				cs.setString(index,(String)value);									
			}else if(value instanceof Date){
				Date date=(Date)value;
				cs.setDate(index, new java.sql.Date(date.getTime()));
			}else if(value instanceof Integer){
				cs.setInt(index, (Integer)value);
			}else if(value instanceof Float){
				cs.setFloat(index, (Float)value);
			}else if(value instanceof Double){
				cs.setDouble(index, (Double)value);
			}else{
				cs.setObject(index, value);
			}
			index++;
		}
		if(oracleCursorIndex>-1){
			cs.registerOutParameter(oracleCursorIndex, -10);
		}
		return new StatementWrapper(cs,oracleCursorIndex);
	} catch (SQLException e) {
		throw new ReportException(e);
	}
}