use of org.springframework.jdbc.core.JdbcTemplate in project camel by apache.
the class SqlProducerSeparatorTest method setUp.
@Before
public void setUp() throws Exception {
db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.DERBY).addScript("sql/createAndPopulateDatabase.sql").build();
jdbcTemplate = new JdbcTemplate(db);
super.setUp();
}
use of org.springframework.jdbc.core.JdbcTemplate in project camel by apache.
the class CallableStatementWrapperTest method setUp.
@Before
public void setUp() throws Exception {
db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.DERBY).addScript("sql/storedProcedureTest.sql").build();
jdbcTemplate = new JdbcTemplate(db);
templateParser = new TemplateParser();
this.factory = new CallableStatementWrapperFactory(jdbcTemplate, templateParser);
super.setUp();
}
use of org.springframework.jdbc.core.JdbcTemplate in project otter by alibaba.
the class DataSourceChecker method checkMap.
public String checkMap(String namespace, String name, Long dataSourceId) {
Connection conn = null;
Statement stmt = null;
DataMediaSource source = dataMediaSourceService.findById(dataSourceId);
DataSource dataSource = null;
try {
DbMediaSource dbMediaSource = (DbMediaSource) source;
dataSource = dataSourceCreator.createDataSource(dbMediaSource);
// conn = dataSource.getConnection();
// if (null == conn) {
// return DATABASE_FAIL;
// }
ModeValue namespaceValue = ConfigHelper.parseMode(namespace);
ModeValue nameValue = ConfigHelper.parseMode(name);
String tempNamespace = namespaceValue.getSingleValue();
String tempName = nameValue.getSingleValue();
try {
Table table = DdlUtils.findTable(new JdbcTemplate(dataSource), tempNamespace, tempNamespace, tempName);
if (table == null) {
return SELECT_FAIL;
}
} catch (SQLException se) {
logger.error("check error!", se);
return SELECT_FAIL;
} catch (Exception e) {
logger.error("check error!", e);
return SELECT_FAIL;
}
// String selectSql = "SELECT * from " + tempNamespace + "." +
// tempName + " where 1 = 0";
// String insertSql = "INSERT INTO " + tempNamespace + "." +
// tempName + " select * from ";
// insertSql += "( SELECT * from " + tempNamespace + "." + tempName
// + ") table2 where 1 = 0";
// String deleteSql = "DELETE from " + tempNamespace + "." +
// tempName + " where 1 = 0";
//
// stmt = conn.createStatement();
//
// try {
// stmt.executeQuery(selectSql);
// } catch (SQLException se) {
// return SELECT_FAIL;
// }
//
// try {
// stmt.execute(insertSql);
// } catch (SQLException se) {
// return INSERT_FAIL;
// }
//
// try {
// stmt.execute(deleteSql);
// } catch (SQLException se) {
// return DELETE_FAIL;
// }
} finally {
closeConnection(conn, stmt);
dataSourceCreator.destroyDataSource(dataSource);
}
return TABLE_SUCCESS;
}
use of org.springframework.jdbc.core.JdbcTemplate in project otter by alibaba.
the class DbDialectIntegration method test_mysql.
@Test(expectedExceptions = RuntimeException.class)
public void test_mysql() {
DbMediaSource dbMediaSource = new DbMediaSource();
dbMediaSource.setId(10L);
dbMediaSource.setDriver("com.mysql.jdbc.Driver");
dbMediaSource.setUsername("xxxxx");
dbMediaSource.setPassword("xxxxx");
dbMediaSource.setUrl("jdbc:mysql://127.0.0.1:3306");
dbMediaSource.setEncode("UTF-8");
dbMediaSource.setType(DataMediaType.MYSQL);
final DbDialect dbDialect = dbDialectFactory.getDbDialect(2L, dbMediaSource);
want.object(dbDialect).clazIs(MysqlDialect.class);
Table table = dbDialect.findTable("test", "ljh_demo");
System.out.println(table);
final SqlTemplate sqlTemplate = dbDialect.getSqlTemplate();
final JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate();
final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate();
final int[] pkColumnTypes = { Types.INTEGER };
final int[] columnTypes = { Types.VARCHAR, Types.INTEGER, Types.DECIMAL, Types.BIGINT };
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
int affect = 0;
String sql = null;
// 执行insert
sql = sqlTemplate.getInsertSql(SCHEMA_NAME, TABLE_NAME, pkColumns, columns);
System.out.println(sql);
affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
return ps.executeUpdate();
}
});
want.number(affect).isEqualTo(1);
throw new RuntimeException("rollback");
}
});
}
use of org.springframework.jdbc.core.JdbcTemplate in project otter by alibaba.
the class DbDialectTest method test_oracle.
@Test(expectedExceptions = RuntimeException.class)
public void test_oracle() {
DbDataMedia media = getOracleMedia();
final DbDialect dbDialect = dbDialectFactory.getDbDialect(1L, media.getSource());
want.object(dbDialect).clazIs(OracleDialect.class);
final SqlTemplate sqlTemplate = dbDialect.getSqlTemplate();
final JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate();
final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate();
final int[] pkColumnTypes = { Types.NUMERIC, Types.VARCHAR };
final int[] columnTypes = { Types.CHAR, Types.NUMERIC, Types.BLOB, Types.CLOB, Types.DATE, Types.DATE, Types.DATE };
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
int affect = 0;
String sql = null;
// 执行insert
sql = sqlTemplate.getInsertSql(ORACLE_SCHEMA_NAME, TABLE_NAME, pkColumns, columns);
System.out.println(sql);
affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
return ps.executeUpdate();
}
});
want.number(affect).isEqualTo(1);
// 执行update
sql = sqlTemplate.getUpdateSql(ORACLE_SCHEMA_NAME, TABLE_NAME, pkColumns, columns);
System.out.println(sql);
affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
return ps.executeUpdate();
}
});
want.number(affect).isEqualTo(1);
// 执行deleate
sql = sqlTemplate.getDeleteSql(ORACLE_SCHEMA_NAME, TABLE_NAME, pkColumns);
System.out.println(sql);
affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
doPreparedStatement(ps, dbDialect, toTypes(pkColumnTypes), toValues(pkColumnValues));
return ps.executeUpdate();
}
});
want.number(affect).isEqualTo(1);
// 执行merge
sql = sqlTemplate.getMergeSql(ORACLE_SCHEMA_NAME, TABLE_NAME, pkColumns, columns, null, true);
System.out.println(sql);
affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
return ps.executeUpdate();
}
});
want.number(affect).isEqualTo(1);
throw new RuntimeException("rollback");
}
});
}
Aggregations