use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingTablesOnlyForPStatementWithDMLTest method assertInsert.
@Test
public void assertInsert() throws SQLException, DatabaseUnitException {
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
try (Connection connection = shardingDataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
for (int i = 1; i <= 10; i++) {
preparedStatement.setInt(1, i);
preparedStatement.setInt(2, i);
preparedStatement.setString(3, "insert");
preparedStatement.executeUpdate();
}
}
assertDataSet("insert", "insert");
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingMasterSlaveForPStatementWithDMLTest method assertInsertWithAllPlaceholders.
@Test
public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitException {
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, i);
preparedStatement.setInt(2, j);
preparedStatement.setString(3, "insert");
preparedStatement.executeUpdate();
}
}
}
assertDataSet("insert", "insert");
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingMasterSlaveForPStatementWithDMLTest method assertDeleteWithoutAlias.
@Test
public void assertDeleteWithoutAlias() throws SQLException, DatabaseUnitException {
String sql = "DELETE `t_order` WHERE `order_id` = ? AND `user_id` = ?";
for (int i = 10; i < 20; i++) {
for (int j = 0; j < 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, i * 100 + j);
preparedStatement.setInt(2, i);
assertThat(preparedStatement.executeUpdate(), is(1));
}
}
}
assertDataSet("delete", "init");
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingMasterSlaveForPStatementWithDMLTest method assertInsertWithHint.
@Test
public void assertInsertWithHint() throws SQLException, DatabaseUnitException {
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
try (Connection connection = shardingDataSource.getConnection();
HintManager hintManager = HintManager.getInstance()) {
hintManager.addDatabaseShardingValue("t_order", "user_id", j);
hintManager.addTableShardingValue("t_order", "order_id", i);
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, i);
preparedStatement.setInt(2, j);
preparedStatement.setString(3, "insert");
preparedStatement.executeUpdate();
}
}
}
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.setMasterRouteOnly();
assertDataSet("insert", "insert");
}
}
use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.
the class ShardingMasterSlaveForPStatementWithDMLTest method assertInsertWithPlaceholdersForNotShardingKeys.
@Test
public void assertInsertWithPlaceholdersForNotShardingKeys() throws SQLException, DatabaseUnitException {
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, ?)";
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(String.format(sql, i, j));
preparedStatement.setString(1, "insert");
preparedStatement.executeUpdate();
}
}
}
assertDataSet("insert", "insert");
}
Aggregations