Search in sources :

Example 31 with PreparedStatement

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");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 32 with PreparedStatement

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");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AbstractShardingMasterSlaveDBUnitTest(com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest) Test(org.junit.Test)

Example 33 with PreparedStatement

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");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AbstractShardingMasterSlaveDBUnitTest(com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest) Test(org.junit.Test)

Example 34 with PreparedStatement

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");
    }
}
Also used : HintManager(com.dangdang.ddframe.rdb.sharding.api.HintManager) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AbstractShardingMasterSlaveDBUnitTest(com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest) Test(org.junit.Test)

Example 35 with PreparedStatement

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");
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AbstractShardingMasterSlaveDBUnitTest(com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest) 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