Search in sources :

Example 66 with Statement

use of java.sql.Statement in project HikariCP by brettwooldridge.

the class MockDataSource method createMockConnection.

public static Connection createMockConnection() throws SQLException {
    // Setup mock connection
    final Connection mockConnection = mock(Connection.class);
    // Autocommit is always true by default
    when(mockConnection.getAutoCommit()).thenReturn(true);
    // Handle Connection.createStatement()
    Statement statement = mock(Statement.class);
    when(mockConnection.createStatement()).thenReturn(statement);
    when(mockConnection.createStatement(anyInt(), anyInt())).thenReturn(statement);
    when(mockConnection.createStatement(anyInt(), anyInt(), anyInt())).thenReturn(statement);
    when(mockConnection.isValid(anyInt())).thenReturn(true);
    // Handle Connection.prepareStatement()
    PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);
    when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), anyInt())).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), any(int[].class))).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), any(String[].class))).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).doNothing().when(mockPreparedStatement).setInt(anyInt(), anyInt());
    ResultSet mockResultSet = mock(ResultSet.class);
    when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
    when(mockResultSet.getString(anyInt())).thenReturn("aString");
    when(mockResultSet.next()).thenReturn(true);
    // Handle Connection.prepareCall()
    CallableStatement mockCallableStatement = mock(CallableStatement.class);
    when(mockConnection.prepareCall(anyString())).thenReturn(mockCallableStatement);
    when(mockConnection.prepareCall(anyString(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
    when(mockConnection.prepareCall(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
    return mockConnection;
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 67 with Statement

use of java.sql.Statement in project HikariCP by brettwooldridge.

the class ExceptionTest method testUseAfterClose.

@Test
public void testUseAfterClose() throws SQLException {
    try (Connection connection = ds.getConnection()) {
        assertNotNull(connection);
        connection.close();
        try (Statement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?")) {
            fail();
        } catch (SQLException e) {
            assertSame("Connection is closed", e.getMessage());
        }
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 68 with Statement

use of java.sql.Statement in project flyway by flyway.

the class VerticaMigrationMediumTest method emptySearchPath.

@Test
public void emptySearchPath() {
    Flyway flyway1 = new Flyway();
    DriverDataSource driverDataSource = (DriverDataSource) dataSource;
    flyway1.setDataSource(new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, driverDataSource.getUrl(), driverDataSource.getUser(), driverDataSource.getPassword(), null) {

        @Override
        public Connection getConnection() throws SQLException {
            Connection connection = super.getConnection();
            Statement statement = null;
            try {
                statement = connection.createStatement();
                statement.execute("SET search_path = v_catalog");
            } finally {
                JdbcUtils.closeStatement(statement);
            }
            return connection;
        }
    });
    flyway1.setLocations(getBasedir());
    flyway1.setSchemas("public");
    flyway1.migrate();
}
Also used : Flyway(org.flywaydb.core.Flyway) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 69 with Statement

use of java.sql.Statement in project flyway by flyway.

the class PostgreSQLMigrationMediumTest method emptySearchPath.

@Test
public void emptySearchPath() {
    Flyway flyway1 = new Flyway();
    DriverDataSource driverDataSource = (DriverDataSource) dataSource;
    flyway1.setDataSource(new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, driverDataSource.getUrl(), driverDataSource.getUser(), driverDataSource.getPassword(), null) {

        @Override
        public Connection getConnection() throws SQLException {
            Connection connection = super.getConnection();
            Statement statement = null;
            try {
                statement = connection.createStatement();
                statement.execute("SELECT set_config('search_path', '', false)");
            } finally {
                JdbcUtils.closeStatement(statement);
            }
            return connection;
        }
    });
    flyway1.setLocations(getBasedir());
    flyway1.setSchemas("public");
    flyway1.migrate();
}
Also used : Flyway(org.flywaydb.core.Flyway) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 70 with Statement

use of java.sql.Statement in project gephi-plugins-bootcamp by gephi.

the class SQLiteDatabaseExporter method execute.

@Override
public boolean execute() {
    Connection connection = null;
    try {
        if (path.getParentFile().exists()) {
            //Create connection
            SQLiteDriver sQLiteDriver = Lookup.getDefault().lookup(SQLiteDriver.class);
            String connectionUrl = SQLUtils.getUrl(sQLiteDriver, path.getAbsolutePath(), 0, "");
            connection = sQLiteDriver.getConnection(connectionUrl, "", "");
            //Create statement and create nodes and egdes table
            Statement statement = connection.createStatement();
            // set timeout to 30 sec.
            statement.setQueryTimeout(30);
            statement.executeUpdate("drop table if exists nodes");
            statement.executeUpdate("drop table if exists edges");
            statement.executeUpdate("create table nodes (id string, label string)");
            statement.executeUpdate("create table edges (source string, target string, weight real)");
            //Get the current graph in the defined workspace
            GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
            GraphModel graphModel = graphController.getGraphModel(workspace);
            Graph graph = graphModel.getGraphVisible();
            //Count the number of tasks (nodes + edges) and start the progress
            int tasks = graph.getNodeCount() + graph.getEdgeCount();
            Progress.start(progress, tasks);
            //Export nodes. Progress is incremented at each step.
            for (Node n : graph.getNodes().toArray()) {
                String id = n.getId().toString();
                String label = n.getLabel();
                statement.executeUpdate("insert into nodes values('" + id + "', '" + label + "')");
                if (cancel) {
                    return false;
                }
                Progress.progress(progress);
            }
            //Export edges. Progress is incremented at each step.
            for (Edge e : graph.getEdges().toArray()) {
                String sourceId = e.getSource().getId().toString();
                String targetId = e.getTarget().getId().toString();
                String weight = String.valueOf(e.getWeight());
                statement.executeUpdate("insert into edges values('" + sourceId + "', '" + targetId + "', '" + weight + "')");
                if (cancel) {
                    return false;
                }
                Progress.progress(progress);
            }
            //Finish progress
            Progress.finish(progress);
            return true;
        } else {
            throw new FileNotFoundException(path.getAbsolutePath() + " does not exist");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            // connection close failed.
            System.err.println(e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) SQLiteDriver(org.gephi.io.database.drivers.SQLiteDriver) Node(org.gephi.graph.api.Node) Connection(java.sql.Connection) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) Graph(org.gephi.graph.api.Graph) GraphModel(org.gephi.graph.api.GraphModel) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Aggregations

Statement (java.sql.Statement)3054 Connection (java.sql.Connection)1634 ResultSet (java.sql.ResultSet)1631 SQLException (java.sql.SQLException)1529 PreparedStatement (java.sql.PreparedStatement)1329 Test (org.junit.Test)570 ArrayList (java.util.ArrayList)323 CallableStatement (java.sql.CallableStatement)135 ResultSetMetaData (java.sql.ResultSetMetaData)127 IOException (java.io.IOException)121 Properties (java.util.Properties)114 HashMap (java.util.HashMap)83 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)81 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)71 StringPlus (mom.trd.opentheso.bdd.tools.StringPlus)63 DataSource (javax.sql.DataSource)62 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)61 Vector (java.util.Vector)61 DatabaseMetaData (java.sql.DatabaseMetaData)53 KeyValue (org.vcell.util.document.KeyValue)49