當前位置: 首頁>>代碼示例>>Java>>正文


Java JdbcDataSource.getConnection方法代碼示例

本文整理匯總了Java中org.h2.jdbcx.JdbcDataSource.getConnection方法的典型用法代碼示例。如果您正苦於以下問題:Java JdbcDataSource.getConnection方法的具體用法?Java JdbcDataSource.getConnection怎麽用?Java JdbcDataSource.getConnection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.h2.jdbcx.JdbcDataSource的用法示例。


在下文中一共展示了JdbcDataSource.getConnection方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: initDb

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@BeforeClass
public static void initDb() throws SQLException {
    ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:estest;DB_CLOSE_DELAY=-1;MVCC=true");
    ds.setUser("sa");
    try (Connection con = ds.getConnection()) {
        executeSql(
            con,
            "create table event (ID varchar, VERSION int, TIMESTAMP timestamp, TYPE varchar, PAYLOAD_VERSION int, PAYLOAD clob, primary key (ID, VERSION))",
            " create table version (ID varchar, VERSION int)",
            " create table snapshot(ID varchar primary key, VERSION int, TIMESTAMP timestamp, PAYLOAD_VERSION int, PAYLOAD clob)",
            // we will not use primary key check to escalate optimistic lock exception in very last step
            "create table event_collision (ID varchar, VERSION int, TIMESTAMP timestamp, TYPE varchar, PAYLOAD_VERSION int, PAYLOAD clob)");
    }
    template = new JdbcTemplate(ds);
}
 
開發者ID:goodees,項目名稱:goodees,代碼行數:17,代碼來源:JdbcTest.java

示例2: testDataSource

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void testDataSource() throws SQLException {
    deleteDb("dataSource");
    JdbcDataSource ds = new JdbcDataSource();
    PrintWriter p = new PrintWriter(new StringWriter());
    ds.setLogWriter(p);
    assertTrue(p == ds.getLogWriter());
    ds.setURL(getURL("dataSource", true));
    ds.setUser(getUser());
    ds.setPassword(getPassword());
    Connection conn;
    conn = ds.getConnection();
    Statement stat;
    stat = conn.createStatement();
    stat.execute("SELECT * FROM DUAL");
    conn.close();
    conn = ds.getConnection(getUser(), getPassword());
    stat = conn.createStatement();
    stat.execute("SELECT * FROM DUAL");
}
 
開發者ID:vdr007,項目名稱:ThriftyPaxos,代碼行數:20,代碼來源:TestDataSource.java

示例3: createDatabase

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
/**
 * To create certificate management database.
 *
 * @return Datasource.
 * @throws SQLException SQL Exception.
 */
private DataSource createDatabase() throws SQLException {
    URL resourceURL = ClassLoader.getSystemResource("sql-scripts" + File.separator + "h2.sql");
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:mem:cert;DB_CLOSE_DELAY=-1");
    dataSource.setUser("sa");
    dataSource.setPassword("sa");
    final String LOAD_DATA_QUERY = "RUNSCRIPT FROM '" + resourceURL.getPath() + "'";
    Connection conn = null;
    Statement statement = null;
    try {
        conn = dataSource.getConnection();
        statement = conn.createStatement();
        statement.execute(LOAD_DATA_QUERY);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {}
        }
        if (statement != null) {
            statement.close();
        }
    }
    return dataSource;
}
 
開發者ID:wso2,項目名稱:carbon-device-mgt,代碼行數:32,代碼來源:CertificateAuthenticatorTest.java

示例4: before

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@Override
public void before() throws Throwable {
    // Set up JMS from super
    super.before();
    log.info("+++ BEFORE on JUnit Rule '" + id(Rule_MatsWithDb.class) + "', H2 database:");
    log.info("Setting up H2 database using DB URL [" + H2_DATABASE_URL + "], dropping all objects.");
    // Set up H2 Database
    _dataSource = new JdbcDataSource();
    _dataSource.setURL(H2_DATABASE_URL);
    try {
        try (Connection con = _dataSource.getConnection();
                Statement stmt = con.createStatement();) {
            stmt.execute("DROP ALL OBJECTS DELETE FILES");
        }
    }
    catch (SQLException e) {
        throw new RuntimeException("Got problems running 'DROP ALL OBJECTS DELETE FILES'.", e);
    }
    log.info("--- BEFORE done! JUnit Rule '" + id(Rule_MatsWithDb.class) + "', H2 database.");
}
 
開發者ID:stolsvik,項目名稱:mats,代碼行數:21,代碼來源:Rule_MatsWithDb.java

示例5: createSimpleDataSource

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void createSimpleDataSource()
{
    String url = DatabaseConfig.buildJdbcUrl(config);

    // By default, H2 closes database when all of the connections are closed. When database
    // is closed, data is gone with in-memory mode. It's unexpected. However, if here disables
    // that such behavior using DB_CLOSE_DELAY=-1 option, there're no methods to close the
    // database explicitly. Only way to close is to not disable shutdown hook of H2 database
    // (DB_CLOSE_ON_EXIT=TRUE). But this also causes unexpected behavior when PreDestroy is
    // triggered in a shutdown hook. Therefore, here needs to rely on injector to take care of
    // dependencies so that the database is closed after calling all other PreDestroy methods
    // that depend on this DataSourceProvider.
    // To solve this issue, here holds one Connection until PreDestroy.
    JdbcDataSource ds = new JdbcDataSource();
    ds.setUrl(url + ";DB_CLOSE_ON_EXIT=FALSE");

    logger.debug("Using database URL {}", url);

    try {
        this.closer = ds.getConnection();
    }
    catch (SQLException ex) {
        throw Throwables.propagate(ex);
    }
    this.ds = ds;
}
 
開發者ID:treasure-data,項目名稱:digdag,代碼行數:27,代碼來源:DataSourceProvider.java

示例6: startUp

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@BeforeClass
public static void startUp() throws Exception {
    dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:mem:test");
    dataSource.setUser("sa");
    dataSource.setPassword("sa");
    connection = dataSource.getConnection();

    String createStatement = "CREATE TABLE greetings ("
        + "id INT PRIMARY KEY AUTO_INCREMENT, "
        + "receiver VARCHAR(255), "
        + "sender VARCHAR(255) "
        + ");";

    connection.createStatement().executeUpdate("DROP TABLE IF EXISTS greetings");
    connection.createStatement().executeUpdate(createStatement);

    namingMixIn = new NamingMixIn();
    namingMixIn.initialize();
    bindDataSource(namingMixIn.getInitialContext(), "java:jboss/myDS", dataSource);
}
 
開發者ID:jboss-switchyard,項目名稱:switchyard,代碼行數:22,代碼來源:CamelSqlBindingTest.java

示例7: main

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
public static void main(String[] args) throws SQLException {

        JdbcDataSource dataSource = new JdbcDataSource();
//        dataSource.setUrl("jdbc:h2:~/datasource-test.db");
        dataSource.setUrl("jdbc:h2:mem:test");
        dataSource.setUser("sa");
        dataSource.setPassword("");

        Connection connection = dataSource.getConnection();

        Statement statement = connection.createStatement();

        statement.execute("drop table t1 if exists ");
        statement.execute("create table t1(id int primary key, name char)");
        statement.execute("insert into t1(id,name) values (1,'jdbc')");
        statement.execute("insert into t1(id,name) values (2,'java')");
        statement.execute("insert into t1(id,name) values (3,'trainig')");
        statement.execute("insert into t1(id,name) values (4,'persistence')");

        ResultSet resultSet = statement.executeQuery("select * from t1");

        printResults(resultSet);
    }
 
開發者ID:ieugen,項目名稱:trainings,代碼行數:24,代碼來源:DataSourceMain.java

示例8: createConnection

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@Override
protected Connection createConnection() throws SQLException
{
    final JdbcDataSource ds = new org.h2.jdbcx.JdbcDataSource();
    ds.setURL("jdbc:h2:" + dbFileName.getAbsolutePath() + ";DB_CLOSE_ON_EXIT=FALSE");
    ds.setUser("sa");
    Connection results = ds.getConnection();
    results.setAutoCommit(false);
    return results;
}
 
開發者ID:adnanmitf09,項目名稱:Rubus,代碼行數:11,代碼來源:H2Consumer.java

示例9: setUp

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@Before
public void setUp() throws SQLException {
    final JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    final Connection connection = dataSource.getConnection();
    RunScript.execute(connection, new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("test.sql")));
    System.out.println("Created in-memory database: " + connection.getMetaData().getURL());

    connectionManager = new CountingConnectionManager(new DataSourceConnectionManager(dataSource));
    toTest = new JdbcOperations(connectionManager);
}
 
開發者ID:voho,項目名稱:jdbcis,代碼行數:15,代碼來源:AbstractOperationTest.java

示例10: createDataTables

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
/**
 * To create the database tables for the particular device-type based on the scripts
 *
 * @param databaseName Name of the Database
 * @param scriptFilePath Path of the SQL script File
 * @throws IOException  IO Exception
 * @throws SQLException SQL Exception.
 */
public static DataSource createDataTables(String databaseName, String scriptFilePath) throws IOException,
        SQLException {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:mem:" + databaseName + ";DB_CLOSE_DELAY=-1");
    dataSource.setUser("sa");
    dataSource.setPassword("sa");

    File file = new File(scriptFilePath);

    final String LOAD_DATA_QUERY = "RUNSCRIPT FROM '" + file.getCanonicalPath() + "'";

    Connection connection = null;
    try {
        connection = dataSource.getConnection();
        Statement statement = connection.createStatement();
        statement.execute(LOAD_DATA_QUERY);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
    return dataSource;
}
 
開發者ID:wso2,項目名稱:carbon-device-mgt,代碼行數:35,代碼來源:Utils.java

示例11: setUpClass

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@BeforeClass
public static void setUpClass() throws Exception {
    server = Server.createTcpServer().start();
    Class.forName("org.h2.Driver");
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setUrl("jdbc:h2:mem:");
    connection = dataSource.getConnection();
    String create = new Scanner(BetterSqlMapperTest.class.getResourceAsStream("/sql/test_create.sql"), "UTF-8").useDelimiter("\\A").next();
    Statement statement = connection.createStatement();
    statement.execute(create);
    statement.close();
}
 
開發者ID:yeagy,項目名稱:bss,代碼行數:13,代碼來源:BetterSqlSupportTest.java

示例12: setUpClass

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@BeforeClass
public static void setUpClass() throws Exception {
    server = Server.createTcpServer().start();
    Class.forName("org.h2.Driver");
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setUrl("jdbc:h2:mem:");
    connectionH2 = dataSource.getConnection();
    String create = new Scanner(BetterSqlMapperTest.class.getResourceAsStream("/sql/test_create.sql"), "UTF-8").useDelimiter("\\A").next();
    Statement statement = connectionH2.createStatement();
    statement.execute(create);
    statement.close();
}
 
開發者ID:yeagy,項目名稱:bss,代碼行數:13,代碼來源:BetterSqlTransactionTest.java

示例13: start

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@Override
public void start() {
  final JdbcDataSource dataSource = new JdbcDataSource();
  dataSource.setUrl(getUrl() + ";DB_CLOSE_DELAY=-1");
  try (Connection conn = dataSource.getConnection()) {
    RunScript.execute(conn, new StringReader("SELECT 1"));
  } catch (SQLException x) {
    throw new RuntimeException(x);
  }
}
 
開發者ID:eclipse,項目名稱:che,代碼行數:11,代碼來源:H2DBTestServer.java

示例14: initDB

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void initDB() throws SQLException{
	JdbcDataSource ds = new JdbcDataSource();
	ds.setURL("jdbc:h2:.//data/database;DB_CLOSE_ON_EXIT=FALSE");
	ds.setUser("tracker");
	ds.setPassword("tracker");
	conn = ds.getConnection();
	stat = conn.createStatement();
	this.createTables();
}
 
開發者ID:megablue,項目名稱:Hearthtracker,代碼行數:10,代碼來源:HearthDB.java

示例15: setup

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
public void setup() throws SQLException {
    h2DataSource = new JdbcDataSource();
    h2DataSource.setURL("jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1");

    try (Connection conn = h2DataSource.getConnection()) {
        conn.createStatement().execute("create table PERSONS (ID int, NAME varchar);");
    }
}
 
開發者ID:witoldsz,項目名稱:ultm,代碼行數:9,代碼來源:H2DemoDatabase.java


注:本文中的org.h2.jdbcx.JdbcDataSource.getConnection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。