Search in sources :

Example 66 with PreparedStatement

use of java.sql.PreparedStatement in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method createAuthorization.

@Override
@JdbcTransaction
public ServiceAuthorization createAuthorization(ProjectId project, ServiceAuthorization authorization) throws RepositoryException {
    try {
        ServiceType serviceType = new ServiceType(authorization.serviceType);
        JdbcConnection connection = connectionProvider.get();
        int serviceId = JdbcRepositoryHelpers.getServiceKey(connection, serviceType);
        int projectId = JdbcRepositoryHelpers.getProjectKey(connection, project);
        final String sql = "INSERT INTO service_authorizations (service, project, data) VALUES (?, ?, ?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = null;
        try {
            ps.setInt(1, serviceId);
            ps.setInt(2, projectId);
            ps.setString(3, authorization.data);
            int updateCount = ps.executeUpdate();
            if (updateCount != 1) {
                throw new IllegalStateException("Unexpected number of rows inserted");
            }
        } finally {
            JdbcUtils.safeClose(rs);
            JdbcUtils.safeClose(ps);
        }
        return authorization;
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    }
}
Also used : SQLException(java.sql.SQLException) ServiceType(org.platformlayer.ids.ServiceType) ResultSet(java.sql.ResultSet) JdbcConnection(com.fathomdb.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 67 with PreparedStatement

use of java.sql.PreparedStatement in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method findServiceAuthorization.

@Override
@JdbcTransaction
public ServiceAuthorization findServiceAuthorization(ServiceType serviceType, ProjectId project) throws RepositoryException {
    try {
        JdbcConnection connection = connectionProvider.get();
        int serviceId = JdbcRepositoryHelpers.getServiceKey(connection, serviceType);
        int projectId = JdbcRepositoryHelpers.getProjectKey(connection, project);
        String sql = "SELECT data FROM service_authorizations WHERE service=? and project=?";
        List<ServiceAuthorization> items = Lists.newArrayList();
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = null;
        try {
            ps.setInt(1, serviceId);
            ps.setInt(2, projectId);
            rs = ps.executeQuery();
            while (rs.next()) {
                items.add(mapRow(serviceType, rs));
            }
        } finally {
            JdbcUtils.safeClose(rs);
            JdbcUtils.safeClose(ps);
        }
        if (items.size() == 0) {
            return null;
        }
        if (items.size() != 1) {
            throw new IllegalStateException("Found duplicate results for primary key: " + serviceType + ":" + project);
        }
        return items.get(0);
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JdbcConnection(com.fathomdb.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(org.platformlayer.RepositoryException) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 68 with PreparedStatement

use of java.sql.PreparedStatement in project otter by alibaba.

the class TestMysqlUnsignedInt method insertNumeric.

public static void insertNumeric() throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    Properties from = new Properties();
    from.put("user", "root");
    from.put("password", "root");
    from.put("characterEncoding", "utf8");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/erosa", from);
    PreparedStatement pst = conn.prepareStatement("insert into unsignednumeric(id,id1,id2,id3) values (?,?,?,?)");
    pst.setLong(1, Integer.MAX_VALUE * 2L);
    pst.setLong(2, Integer.MAX_VALUE);
    pst.setBigDecimal(3, new BigDecimal("18446744073709551614"));
    pst.setBigDecimal(4, new BigDecimal("9223372036854775807"));
    pst.executeUpdate();
    pst.close();
    conn.close();
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) BigDecimal(java.math.BigDecimal)

Example 69 with PreparedStatement

use of java.sql.PreparedStatement in project otter by alibaba.

the class TestMysqlUnsignedInt method main.

public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException {
    insertNumeric();
    Thread.sleep(1000L);
    Class.forName("com.mysql.jdbc.Driver");
    Properties from = new Properties();
    from.put("user", "root");
    from.put("password", "root");
    from.put("characterEncoding", "utf8");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/erosa", from);
    PreparedStatement pst = conn.prepareStatement("select id,id1,id2,id3 from unsignednumeric");
    ResultSet rs = pst.executeQuery();
    while (rs.next()) {
        //            try {
        //                System.out.println(rs.getInt(1));
        //            } catch (Exception e) {
        //                System.out.println(rs.getLong(1));
        //            }
        System.out.println(rs.getLong(1));
        System.out.println(rs.getLong(2));
        System.out.println(rs.getBigDecimal(3));
        //            System.out.println(rs.getString(3));
        System.out.println(rs.getBigDecimal(4));
        System.out.println("-----------------------------");
    }
    rs.close();
    pst.close();
    conn.close();
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties)

Example 70 with PreparedStatement

use of java.sql.PreparedStatement in project otter by alibaba.

the class DbDialectTest method test_mysql.

@Test(expectedExceptions = RuntimeException.class)
public void test_mysql() {
    DbDataMedia media = getMysqlMedia();
    final DbDialect dbDialect = dbDialectFactory.getDbDialect(2L, media.getSource());
    want.object(dbDialect).clazIs(MysqlDialect.class);
    final SqlTemplate sqlTemplate = dbDialect.getSqlTemplate();
    final JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate();
    final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate();
    final int[] pkColumnTypes = { Types.INTEGER, Types.VARCHAR };
    final int[] columnTypes = { Types.CHAR, Types.DECIMAL, Types.BLOB, Types.CLOB, Types.DATE, Types.TIMESTAMP, Types.TIMESTAMP };
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            int affect = 0;
            String sql = null;
            // 执行insert
            sql = sqlTemplate.getInsertSql(MYSQL_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(MYSQL_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(MYSQL_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(MYSQL_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");
        }
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) PreparedStatement(java.sql.PreparedStatement) SqlTemplate(com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) TransactionCallback(org.springframework.transaction.support.TransactionCallback) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) DbDataMedia(com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia) Test(org.testng.annotations.Test) BaseDbTest(com.alibaba.otter.node.etl.BaseDbTest)

Aggregations

PreparedStatement (java.sql.PreparedStatement)6623 ResultSet (java.sql.ResultSet)4065 SQLException (java.sql.SQLException)3538 Connection (java.sql.Connection)2806 Test (org.junit.Test)1103 ArrayList (java.util.ArrayList)963 Properties (java.util.Properties)743 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)345 Statement (java.sql.Statement)273 Timestamp (java.sql.Timestamp)260 DatabaseException (net.jforum.exceptions.DatabaseException)254 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)251 HashMap (java.util.HashMap)209 BigDecimal (java.math.BigDecimal)207 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)174 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)165 List (java.util.List)150 IOException (java.io.IOException)145 Date (java.util.Date)133 Date (java.sql.Date)126