Search in sources :

Example 46 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class RoutingDatabaseOnlyWithHintForDMLTest method assertUpdateWithoutAlias.

@Test
public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitException {
    String sql = "UPDATE `t_order` SET `status` = ? WHERE `order_id` = ? AND `user_id` = ?";
    for (int i = 10; i < 30; i++) {
        for (int j = 0; j < 2; j++) {
            try (DynamicShardingValueHelper helper = new DynamicDatabaseShardingValueHelper(i);
                Connection connection = shardingDataSource.getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                preparedStatement.setString(1, "updated");
                preparedStatement.setInt(2, i * 100 + j);
                preparedStatement.setInt(3, i);
                assertThat(preparedStatement.executeUpdate(), is(1));
            }
        }
    }
    assertDataSet("update", "updated");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 47 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class RoutingDatabaseOnlyWithHintForDMLTest method assertUpdateWithAlias.

@Test
public void assertUpdateWithAlias() throws SQLException, DatabaseUnitException {
    String sql = "UPDATE `t_order` AS o SET o.`status` = ? WHERE o.`order_id` = ? AND o.`user_id` = ?";
    for (int i = 10; i < 30; i++) {
        for (int j = 0; j < 2; j++) {
            try (DynamicShardingValueHelper helper = new DynamicDatabaseShardingValueHelper(i);
                Connection connection = shardingDataSource.getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                preparedStatement.setString(1, "updated");
                preparedStatement.setInt(2, i * 100 + j);
                preparedStatement.setInt(3, i);
                assertThat(preparedStatement.executeUpdate(), is(1));
            }
        }
    }
    assertDataSet("update", "updated");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 48 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class ShardingDataBasesOnlyWithHintForDMLTest method assertUpdateWithoutAlias.

@Test
public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitException {
    String sql = "UPDATE `t_order` SET `status` = ? WHERE `order_id` = ? AND `user_id` = ?";
    for (int i = 10; i < 30; i++) {
        for (int j = 0; j < 2; j++) {
            try (DynamicShardingValueHelper helper = new DynamicShardingValueHelper(i, i * 100 + j);
                Connection connection = shardingDataSource.getConnection()) {
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, "updated");
                preparedStatement.setInt(2, i * 100 + j);
                preparedStatement.setInt(3, i);
                assertThat(preparedStatement.executeUpdate(), is(1));
            }
        }
    }
    assertDataSet("update", "updated");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 49 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class ShardingDataBasesOnlyWithHintForDMLTest method assertInsertWithAllPlaceholders.

@Test
public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitException {
    String sql = "INSERT INTO `t_order` VALUES (?, ?, ?)";
    for (int i = 1; i <= 10; i++) {
        try (DynamicShardingValueHelper helper = new DynamicShardingValueHelper(i, i);
            Connection connection = shardingDataSource.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, i);
            preparedStatement.setInt(2, i);
            preparedStatement.setString(3, "insert");
            preparedStatement.executeUpdate();
        }
    }
    assertDataSet("insert", "insert");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 50 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class ShardingDataBasesOnlyWithHintForDMLTest method assertInsertWithoutPlaceholder.

@Test
public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitException {
    String sql = "INSERT INTO `t_order` VALUES (%s, %s, 'insert')";
    for (int i = 1; i <= 10; i++) {
        try (DynamicShardingValueHelper helper = new DynamicShardingValueHelper(i, i);
            Connection connection = shardingDataSource.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement(String.format(sql, i, i));
            preparedStatement.executeUpdate();
        }
    }
    assertDataSet("insert", "insert");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

PreparedStatement (java.sql.PreparedStatement)5900 ResultSet (java.sql.ResultSet)3733 SQLException (java.sql.SQLException)3074 Connection (java.sql.Connection)2478 Test (org.junit.Test)1099 ArrayList (java.util.ArrayList)851 Properties (java.util.Properties)742 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)345 DatabaseException (net.jforum.exceptions.DatabaseException)254 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)251 Timestamp (java.sql.Timestamp)248 Statement (java.sql.Statement)243 BigDecimal (java.math.BigDecimal)206 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)174 HashMap (java.util.HashMap)169 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)165 List (java.util.List)136 Date (java.util.Date)124 Date (java.sql.Date)123 BaseTest (org.apache.phoenix.query.BaseTest)114