use of org.springframework.jdbc.core.PreparedStatementCallback in project otter by alibaba.
the class BitTableIntegration method test_mysql.
@Test
public void test_mysql() throws UnsupportedEncodingException {
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(SCHEMA_NAME, TABLE_NAME);
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.BIT, Types.BIT };
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);
return null;
// throw new RuntimeException("rollback");
}
});
}
use of org.springframework.jdbc.core.PreparedStatementCallback in project otter by alibaba.
the class DdlUtils method getShardKeyByDRDS.
/**
* 获取DRDS下表的拆分字段, 返回格式为 id,name
*
* @param dataSource
* @param schemaName
* @param tableName
* @return
*/
public static String getShardKeyByDRDS(final JdbcTemplate jdbcTemplate, final String catalogName, final String schemaName, final String tableName) {
try {
return (String) jdbcTemplate.execute("show partitions from ?", new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
DatabaseMetaData metaData = ps.getConnection().getMetaData();
// String sName = getIdentifierName(schemaName, metaData);
String convertTableName = tableName;
if (metaData.storesUpperCaseIdentifiers()) {
convertTableName = tableName.toUpperCase();
}
if (metaData.storesLowerCaseIdentifiers()) {
convertTableName = tableName.toLowerCase();
}
String tName = convertTableName;
ps.setString(1, tName);
ResultSet rs = ps.executeQuery();
String log = null;
if (rs.next()) {
log = rs.getString("KEYS");
}
rs.close();
return log;
}
});
} catch (DataAccessException e) {
// 兼容下oracle源库和目标库DRDS表名不一致的情况,识别一下表名不存在
Throwable cause = e.getRootCause();
if (cause instanceof SQLException) {
// ER_NO_SUCH_TABLE
if (((SQLException) cause).getErrorCode() == 1146) {
return null;
}
}
throw e;
}
}
Aggregations