Package java.sql

Examples of java.sql.Connection


    List<List<Integer>> values = new ArrayList<List<Integer>>();
    values.add(Arrays.asList(1, 2));
    values.add(Arrays.asList(2, 3));
    command.setValueSource(new IteratorValueSource(values.iterator(), 2));
   
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement p = Mockito.mock(PreparedStatement.class);
    Mockito.stub(p.executeBatch()).toReturn(new int [] {1, 1});
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey, IntNum) VALUES (?, ?)")).toReturn(p); //$NON-NLS-1$
   
    JDBCExecutionFactory config = new JDBCExecutionFactory();
   
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, Mockito.mock(ExecutionContext.class), config);
    updateExecution.execute();
View Full Code Here


      throw new TransactionRuntimeException(
        "Program Error: DataSource is not mapped to Identifier "
          + identifier);
  }

  Connection conn = ds.getConnection();

  if (conn != null)
      return conn;

  ConnectionStrategy cs = null;
View Full Code Here

    private Connection getJDBCConnection(String driver, String url,
      String user, String passwd) throws QueryTestFailedException {

  TestLogger.log("Creating Driver Connection: \"" + url + "\"" + " user:password - " + (user!=null?user:"NA") + ":" + (passwd!=null?passwd:"NA")); //$NON-NLS-1$ //$NON-NLS-2$

  Connection conn = null;
  try {
      // Create a connection
      if (user != null && user.length() > 0) {
    conn = DriverManager.getConnection(url, user, passwd);
      } else {
View Full Code Here

      return ds.getConnection();
    }
  }
 
  public void execute(DataSourceFactory connF, String vdbName, String sql) throws Exception {
    Connection connection = connF.getConnection(vdbName);
    try {
      connection.getMetaData();
      Statement statement = connection.createStatement();
      boolean hasResults = statement.execute(sql);
      if (hasResults) {
        ResultSet results = statement.getResultSet();
        ResultSetMetaData metadata = results.getMetaData();
        int columns = metadata.getColumnCount();
       
        while(results.next()) {
          for (int i = 0; i < columns; i++) {
            System.out.print(results.getString(i+1));
            System.out.print(",");
          }
          System.out.println("");
        }
        System.out.println("Done getting results!");
        results.close();       
      }
      else {
        System.out.println("update count is="+statement.getUpdateCount());
      }
      statement.close();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      if (connection != null) {
        connection.close();
      }
    }   
  } 
View Full Code Here

            throw new RuntimeException(e);
        }
    }
   
    private static Connection getConnection(String identifier, ConnectionStrategy connStrategy) throws QueryTestFailedException {
  Connection conn = connStrategy.createDriverConnection(identifier);
  // force autocommit back to true, just in case the last user didnt
  try {
    conn.setAutoCommit(true);
  } catch (Exception sqle) {
    throw new QueryTestFailedException(sqle);
  }
 
  return conn;
View Full Code Here

   
  }
 
 
  @Test public void testSessions() throws Exception{
    Connection c = ds.getConnection("TransactionsRevisited"); // to create the session
    Collection<Session> sessions = admin.getSessions();
   
    int size = sessions.size();
    assertTrue( size >= 1);
   
    Session found = null;
    for (Session s: sessions) {
      if (s.getUserName().equals("admin@teiid-security")) {
        found = s;
        break;
      }
    }
   
    assertNotNull(found);
   
    admin.terminateSession(found.getSessionId());
   
    sessions = admin.getSessions();
    assertTrue(sessions.size() == (size-1));
    c.close();
  }
View Full Code Here

    String msg = "Unable to find jndi source " + jndi_name;//$NON-NLS-1$

    QueryTestFailedException mme = new QueryTestFailedException(msg);//$NON-NLS-1$
    throw mme;
      }
      Connection conn = source.getConnection();
      return conn;
  } catch (QueryTestFailedException qtfe) {
      throw qtfe;
  } catch (Exception e) {
      throw new QueryTestFailedException(e);
View Full Code Here

     * @throws ClassNotFoundException if the JDBC driver couldn't be loaded
     * @throws SQLException if the connection couldn't be created
     */
    public synchronized Connection getConnection()
            throws ClassNotFoundException, SQLException {
        Connection con;
        Transactor tx = Transactor.getInstance();
        if (tx != null) {
            con = tx.getConnection(this);
        } else {
            con = getThreadLocalConnection();
        }

        boolean fileUpdated = props.lastModified() > lastRead ||
                (defaultProps != null && defaultProps.lastModified() > lastRead);

        if (con == null || con.isClosed() || fileUpdated) {
            init();
            con = DriverManager.getConnection(url, conProps);

            // If we wanted to use SQL transactions, we'd set autoCommit to
            // false here and make commit/rollback invocations in Transactor methods;
View Full Code Here

    private Connection getThreadLocalConnection() {
        if (connection == null) {
            connection = new ThreadLocal();
            return null;
        }
        Connection con = (Connection) connection.get();
        if (con != null) {
            // test if connection is still ok
            try {
                Statement stmt = con.createStatement();
                stmt.execute("SELECT 1");
                stmt.close();
            } catch (SQLException sx) {
                try {
                    con.close();
                } catch (SQLException ignore) {/* nothing to do */}
                return null;
            }
        }
        return con;
View Full Code Here

     * Get a db connection that was previously registered with this transactor thread.
     * @param src the db source
     * @return the connection
     */
    public Connection getConnection(DbSource src) {
        Connection con = (Connection) sqlConnections.get(src);
        Long tested = (Long) testedConnections.get(src);
        long now = System.currentTimeMillis();
        if (con != null && (tested == null || now - tested.longValue() > 60000)) {
            // Check if the connection is still alive by executing a simple statement.
            try {
                Statement stmt = con.createStatement();
                stmt.execute("SELECT 1");
                stmt.close();
                testedConnections.put(src, new Long(now));
            } catch (SQLException sx) {
                try {
                    con.close();
                } catch (SQLException ignore) {/* nothing to do */}
                return null;
            }
        }
        return con;
View Full Code Here

TOP

Related Classes of java.sql.Connection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.