当前位置: 首页>>代码示例>>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;未经允许,请勿转载。