本文整理汇总了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);
}
示例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");
}
示例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;
}
示例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.");
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
示例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();
}
示例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();
}
示例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);
}
}
示例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();
}
示例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);");
}
}