Package java.sql

Examples of java.sql.Connection


     */
    public void closeConnections() {
        if (sqlConnections != null) {
            for (Iterator i = sqlConnections.values().iterator(); i.hasNext();) {
                try {
                    Connection con = (Connection) i.next();

                    con.close();
                    nmgr.app.logEvent("Closing DB connection: " + con);
                } catch (Exception ignore) {
                    // exception closing db connection, ignore
                }
            }
View Full Code Here


        }
    }

    public static Db open(String url, String user, String password) {
        try {
            Connection conn = JdbcUtils.getConnection(null, url, user, password);
            return new Db(conn);
        } catch (SQLException e) {
            throw convert(e);
        }
    }
View Full Code Here

    public static Db open(String url, String user, char[] password) {
        try {
            Properties prop = new Properties();
            prop.setProperty("user", user);
            prop.put("password", password);
            Connection conn = JdbcUtils.getConnection(null, url, prop);
            return new Db(conn);
        } catch (SQLException e) {
            throw convert(e);
        }
    }
View Full Code Here

      }
   }

   public static void shutdownInMemoryDatabase(Properties props)
   {
      Connection conn = null;
      Statement st = null;
      try
      {
         String shutDownConnection = getShutdownUrl(props);
         String url = props.getProperty("cache.jdbc.url");
         assert url != null;
         conn = DriverManager.getConnection(shutDownConnection);
         st = conn.createStatement();
         st.execute("SHUTDOWN");
      }
      catch (Throwable e)
      {
         throw new IllegalStateException(e);
      }
      finally
      {
         try
         {
            conn.close();
            st.close();
         }
         catch (SQLException e)
         {
            e.printStackTrace();
View Full Code Here

     */
    public static void execute(String url, String user, String password,
            String schema, String table, String packageName, String folder,
            boolean annotateSchema, boolean trimStrings)
                throws SQLException {
        Connection conn = null;
        try {
            org.h2.Driver.load();
            conn = DriverManager.getConnection(url, user, password);
            Db db = Db.open(url, user, password.toCharArray());
            DbInspector inspector = new DbInspector(db);
View Full Code Here

                userName = pc.getUserName();
                xacon = getXADataSource().
                    getXAConnection(userName,
                                    new String(pc.getPassword()));
            }
            Connection con = xacon.getConnection();
            return new JdbcManagedConnection
                (this, pc, xacon, con, true, true);
        } catch (SQLException ex) {
            ResourceException re =
                new EISSystemException("SQLException: " + ex.getMessage());
View Full Code Here

        }
    }

    private void testAddDelete() throws SQLException {
        deleteDb("index");
        Connection conn = getConnection("index");
        try {
            Statement stat = conn.createStatement();
            stat.execute("CREATE TABLE TEST(ID bigint primary key)");
            int count = 1000;
            stat.execute("insert into test select x from system_range(1, " + count + ")");
            if (!config.memory) {
                conn.close();
                conn = getConnection("index");
                stat = conn.createStatement();
            }
            for (int i = 1; i < count; i++) {
                ResultSet rs = stat.executeQuery("select * from test order by id");
                for (int j = i; rs.next(); j++) {
                    assertEquals(j, rs.getInt(1));
                }
                stat.execute("delete from test where id =" + i);
            }
            stat.execute("drop all objects delete files");
        } finally {
            conn.close();
        }
        deleteDb("index");
    }
View Full Code Here

    private void testCreateIndexOnLob() throws Exception {
        if (config.memory) {
            return;
        }
        deleteDb("lob");
        Connection conn;
        conn = getConnection("lob");
        Statement stat = conn.createStatement();
        stat.execute("create table test(id int, name clob)");
        assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, stat).
                execute("create index idx_n on test(name)");
        stat.execute("drop table test");
        conn.close();
    }
View Full Code Here

        conn.close();
    }

    private void testBlobInputStreamSeek(boolean upgraded) throws Exception {
        deleteDb("lob");
        Connection conn;
        conn = getConnection("lob");
        Statement stat = conn.createStatement();
        stat.execute("create table test(id int primary key, data blob)");
        PreparedStatement prep;
        Random random = new Random();
        byte[] buff = new byte[500000];
        for (int i = 0; i < 10; i++) {
            prep = conn.prepareStatement("insert into test values(?, ?)");
            prep.setInt(1, i);
            random.setSeed(i);
            random.nextBytes(buff);
            prep.setBinaryStream(2, new ByteArrayInputStream(buff), -1);
            prep.execute();
        }
        if (upgraded) {
            if (config.memory) {
                stat.execute("update information_schema.lob_map set pos=null");
            } else {
                stat.execute("alter table information_schema.lob_map drop column pos");
                conn.close();
                conn = getConnection("lob");
            }
        }
        prep = conn.prepareStatement("select * from test where id = ?");
        for (int i = 0; i < 1; i++) {
            random.setSeed(i);
            random.nextBytes(buff);
            for (int j = 0; j < buff.length; j += 10000) {
                prep.setInt(1, i);
                ResultSet rs = prep.executeQuery();
                rs.next();
                InputStream in = rs.getBinaryStream(2);
                in.skip(j);
                int t = in.read();
                assertEquals(t, buff[j] & 0xff);
            }
        }
        conn.close();
        conn.close();
    }
View Full Code Here

                buff.append(buff.length());
            }
        }
        String prefix = buff.toString().substring(0, prefixLength);
        DeleteDbFiles.execute(getBaseDir() + "/index", null, true);
        Connection conn = getConnection("index");
        try {
            Statement stat = conn.createStatement();
            stat.execute("CREATE TABLE a(text VARCHAR PRIMARY KEY)");
            PreparedStatement prepInsert = conn.prepareStatement("INSERT INTO a VALUES(?)");
            PreparedStatement prepDelete = conn.prepareStatement("DELETE FROM a WHERE text=?");
            PreparedStatement prepDeleteAllButOne = conn.prepareStatement("DELETE FROM a WHERE text <> ?");
            int count = 0;
            for (int i = 0; i < 1000; i++) {
                int y = random.nextInt(distinct);
                try {
                    prepInsert.setString(1, prefix + y);
                    prepInsert.executeUpdate();
                    count++;
                } catch (SQLException e) {
                    if (e.getSQLState().equals("23505")) {
                        // ignore
                    } else {
                        TestBase.logError("error", e);
                        break;
                    }
                }
                if (delete && random.nextInt(10) == 1) {
                    if (random.nextInt(4) == 1) {
                        try {
                            prepDeleteAllButOne.setString(1, prefix + y);
                            int deleted = prepDeleteAllButOne.executeUpdate();
                            if (deleted < count - 1) {
                                printError(seed, "deleted:" + deleted + " i:" + i);
                            }
                            count -= deleted;
                        } catch (SQLException e) {
                            TestBase.logError("error", e);
                            break;
                        }
                    } else {
                        try {
                            prepDelete.setString(1, prefix + y);
                            int deleted = prepDelete.executeUpdate();
                            if (deleted > 1) {
                                printError(seed, "deleted:" + deleted + " i:" + i);
                            }
                            count -= deleted;
                        } catch (SQLException e) {
                            TestBase.logError("error", e);
                            break;
                        }
                    }
                }
            }
            int testCount;
            testCount = 0;
            ResultSet rs = stat.executeQuery("SELECT text FROM a ORDER BY text");
            ResultSet rs2 = conn.createStatement().executeQuery("SELECT text FROM a ORDER BY 'x' || text");

//System.out.println("-----------");
//while(rs.next()) {
//    System.out.println(rs.getString(1));
//}
//System.out.println("-----------");
//while(rs2.next()) {
//    System.out.println(rs2.getString(1));
//}
//if (true) throw new AssertionError("stop");
//
            testCount = 0;
            while (rs.next() && rs2.next()) {
                if (!rs.getString(1).equals(rs2.getString(1))) {
                    assertEquals("" + testCount, rs.getString(1), rs.getString(2));
                }
                testCount++;
            }
            assertFalse(rs.next());
            assertFalse(rs2.next());
            if (testCount != count) {
                printError(seed, "count:" + count + " testCount:" + testCount);
            }
            rs = stat.executeQuery("SELECT text, count(*) FROM a GROUP BY text HAVING COUNT(*)>1");
            if (rs.next()) {
                printError(seed, "testCount:" + testCount + " " + rs.getString(1));
            }
        } finally {
            conn.close();
        }
        deleteDb("index");
    }
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.