Search in sources :

Example 51 with PreparedStatement

use of java.sql.PreparedStatement in project hs4j by killme2008.

the class MysqlThread method run.

@Override
public void run() {
    try {
        this.barrier.await();
    } catch (Exception e) {
    // ignore
    }
    final String insertSQL = "insert into user values(?,?,?,?,?,?,?,?,?,?,?)";
    for (int i = 0; i < this.repeats; i++) {
        String postfix = this.index + "_" + i;
        Connection conn = null;
        try {
            conn = this.dataSource.getConnection();
            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(insertSQL);
                // id
                pstmt.setInt(1, 0);
                pstmt.setString(2, "my_first_name_" + postfix);
                pstmt.setString(3, "last_name_" + postfix);
                pstmt.setString(4, "myduty_" + postfix);
                String phone = String.valueOf(i);
                pstmt.setString(5, phone);
                pstmt.setString(6, phone);
                pstmt.setString(7, phone);
                pstmt.setString(8, phone);
                pstmt.setString(9, "my_home_address_" + postfix);
                pstmt.setString(10, "my_office_address_" + postfix);
                pstmt.setString(11, this.remark);
                pstmt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (pstmt != null) {
                    try {
                        pstmt.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    try {
        this.barrier.await();
    } catch (Exception e) {
    // ignore
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 52 with PreparedStatement

use of java.sql.PreparedStatement in project hs4j by killme2008.

the class HandlerSocketBinaryDecodeUnitTest method readBytesJDBC.

private byte[] readBytesJDBC(String key) throws Exception {
    Connection conn = getConnection();
    PreparedStatement ps = conn.prepareStatement("select value from hs4jtest where id = ?");
    try {
        ps.setString(1, key);
        ResultSet rs = ps.executeQuery();
        assertTrue(rs.next());
        byte[] bytes = rs.getBytes(1);
        return bytes;
    } finally {
        ps.close();
        conn.close();
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 53 with PreparedStatement

use of java.sql.PreparedStatement in project hs4j by killme2008.

the class HandlerSocketBinaryDecodeUnitTest method dropTable.

private void dropTable(Connection conn) throws Exception {
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement("drop table hs4jtest");
        ps.execute();
    } finally {
        ps.close();
        conn.close();
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement)

Example 54 with PreparedStatement

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

the class AbstractReadInterface method getCollectionResultSets.

@Override
@SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION", "ODR_OPEN_DATABASE_RESOURCE" }, justification = "ResultSet is wrapped in a DocPartResult. It's iterated and closed in caller code")
public List<DocPartResult> getCollectionResultSets(DSLContext dsl, MetaDatabase metaDatabase, MetaCollection metaCollection, Collection<Integer> dids) throws SQLException {
    ArrayList<DocPartResult> result = new ArrayList<>();
    Connection connection = dsl.configuration().connectionProvider().acquire();
    try {
        Iterator<? extends MetaDocPart> metaDocPartIterator = metaCollection.streamContainedMetaDocParts().sorted(TableRefComparator.MetaDocPart.DESC).iterator();
        while (metaDocPartIterator.hasNext()) {
            MetaDocPart metaDocPart = metaDocPartIterator.next();
            String statament = getDocPartStatament(metaDatabase, metaDocPart, dids);
            PreparedStatement preparedStatement = connection.prepareStatement(statament);
            result.add(new ResultSetDocPartResult(metaDataReadInterface, dataTypeProvider, errorHandler, metaDocPart, preparedStatement.executeQuery(), sqlHelper));
        }
    } finally {
        dsl.configuration().connectionProvider().release(connection);
    }
    return result;
}
Also used : ResultSetDocPartResult(com.torodb.backend.d2r.ResultSetDocPartResult) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSetDocPartResult(com.torodb.backend.d2r.ResultSetDocPartResult) DocPartResult(com.torodb.core.d2r.DocPartResult) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 55 with PreparedStatement

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

the class AbstractReadInterface method getAllCollectionDids.

@Override
@SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION", "ODR_OPEN_DATABASE_RESOURCE" }, justification = "ResultSet is wrapped in a Cursor<Integer>. It's iterated and closed in caller code")
public Cursor<Integer> getAllCollectionDids(DSLContext dsl, MetaDatabase metaDatabase, MetaCollection metaCollection) throws SQLException {
    MetaDocPart rootDocPart = metaCollection.getMetaDocPartByTableRef(tableRefFactory.createRoot());
    if (rootDocPart == null) {
        return new EmptyCursor<>();
    }
    String statement = getReadAllCollectionDidsStatement(metaDatabase.getIdentifier(), rootDocPart.getIdentifier());
    Connection connection = dsl.configuration().connectionProvider().acquire();
    try {
        PreparedStatement preparedStatement = connection.prepareStatement(statement);
        return new DefaultDidCursor(errorHandler, preparedStatement.executeQuery());
    } finally {
        dsl.configuration().connectionProvider().release(connection);
    }
}
Also used : MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) EmptyCursor(com.torodb.core.cursors.EmptyCursor) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

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