Java Code Examples for org.h2.jdbcx.JdbcDataSource#setUser()

The following examples show how to use org.h2.jdbcx.JdbcDataSource#setUser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBService.java    From stepic_java_webserver with MIT License 7 votes vote down vote up
public static Connection getH2Connection() {
    try {
        String url = "jdbc:h2:./h2db";
        String name = "tully";
        String pass = "tully";

        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL(url);
        ds.setUser(name);
        ds.setPassword(pass);

        Connection connection = DriverManager.getConnection(url, name, pass);
        return connection;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: DbConfig.java    From jqm with Apache License 2.0 6 votes vote down vote up
@Bean
public DataSource dataSource()
{
    try
    {
        // When running inside a container, use its resource directory.
        return (DataSource) new JndiTemplate().lookup("jdbc/spring_ds");
    }
    catch (NamingException e)
    {
        // When running on the command line, just create a temporary file DB (only needed for debug).
        System.out.println("JNDI datasource does not exist - falling back on hard coded DS");
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:./target/TEST.db");
        ds.setUser("sa");
        ds.setPassword("sa");
        return ds;
    }
}
 
Example 3
Source File: Main.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    Properties prop = new Properties();
    prop.load(Main.class.getClassLoader().getResourceAsStream("jdbc.properties"));        
    
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL(prop.getProperty("cc.openhome.jdbcUrl"));
    dataSource.setUser(prop.getProperty("cc.openhome.user"));
    dataSource.setPassword(prop.getProperty("cc.openhome.password"));
    
    AccountDAO acctDAO = new AccountDAOJdbcImpl(dataSource);
    MessageDAO messageDAO = new MessageDAOJdbcImpl(dataSource);
    
    UserService userService = new UserService(acctDAO, messageDAO);
    
    userService.messages("caterpillar")
               .forEach(message -> {
                   System.out.printf("%s\t%s%n",
                       message.getLocalDateTime(),
                       message.getBlabla());
               });

}
 
Example 4
Source File: RoomDB.java    From restful-booker-platform with GNU General Public License v3.0 5 votes vote down vote up
public RoomDB() throws SQLException {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:rbp;MODE=MySQL");
    ds.setUser("user");
    ds.setPassword("password");
    connection = ds.getConnection();

    // If you would like to access the DB for this API locally. Uncomment the line below and
    // use a SQL client to access jdbc:h2:tcp://localhost:9091/mem:rbp
    // Server server = Server.createTcpServer("-tcpPort", "9091", "-tcpAllowOthers").start();
}
 
Example 5
Source File: DatabaseIntegrationTest.java    From realworld-api-quarkus with MIT License 5 votes vote down vote up
private static DataSource dataSource() {
  JdbcDataSource jdbcDataSource = new JdbcDataSource();
  jdbcDataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
  jdbcDataSource.setUser("sa");
  jdbcDataSource.setPassword("");
  return jdbcDataSource;
}
 
Example 6
Source File: MessageDB.java    From restful-booker-platform with GNU General Public License v3.0 5 votes vote down vote up
public MessageDB() throws SQLException {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:rbp;MODE=MySQL");
    ds.setUser("user");
    ds.setPassword("password");
    connection = ds.getConnection();

    // If you would like to access the DB for this API locally. Uncomment the line below and
    // use a SQL client to access jdbc:h2:tcp://localhost:9093/mem:rbp
    // Server.createTcpServer("-tcpPort", "9093", "-tcpAllowOthers").start();
}
 
Example 7
Source File: MultiTenantProcessEngineTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
private DataSource createDataSource(String jdbcUrl, String jdbcUsername, String jdbcPassword) {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(jdbcUrl);
    ds.setUser(jdbcUsername);
    ds.setPassword(jdbcPassword);
    return ds;
}
 
Example 8
Source File: TestConfig.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource testDataSource() {
	JdbcDataSource dataSource = new JdbcDataSource();
	dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE");
	dataSource.setUser("sa");
	return dataSource;
}
 
Example 9
Source File: H2DB.java    From Plan with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Connection getConnectionFor(String dbFilePath) throws SQLException {
    String username = config.get(DatabaseSettings.H2_USER);
    String password = config.get(DatabaseSettings.H2_PASS);

    JdbcDataSource jdbcDataSource = new JdbcDataSource();
    jdbcDataSource.setURL("jdbc:h2:file:" + dbFilePath + ";mode=MySQL;DATABASE_TO_UPPER=false");
    jdbcDataSource.setUser(username);
    jdbcDataSource.setPassword(password);

    return jdbcDataSource.getConnection();
}
 
Example 10
Source File: MyDataSourceConfig.java    From spring-boot-inside with MIT License 5 votes vote down vote up
@Bean(name = "dataSource1")
public DataSource dataSource1(@Value("${db.user}") String user) {
	System.err.println("user: " + user);
	JdbcDataSource ds = new JdbcDataSource();
	ds.setURL("jdbc:h2:˜/test");
	ds.setUser(user);
	return ds;
}
 
Example 11
Source File: Utils.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: H2DB.java    From binder-swagger-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static DataSource getDataSource() {
    if (dataSource == null) {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
        ds.setUser("sa");
        ds.setPassword("sa");
        dataSource = ds;
    }
    return dataSource;
}
 
Example 13
Source File: LoggerPrintWriterJdbcH2Test.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("DataSource#setLogWriter() has no effect in H2, it uses its own internal logging and an SLF4J bridge.")
public void testDataSource_setLogWriter() throws SQLException {
    final JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setUrl(H2_URL);
    dataSource.setUser(USER_ID);
    dataSource.setPassword(PASSWORD);
    dataSource.setLogWriter(createLoggerPrintWriter());
    // dataSource.setLogWriter(new PrintWriter(new OutputStreamWriter(System.out)));
    try (final Connection conn = dataSource.getConnection()) {
        conn.prepareCall("select 1");
    }
    Assert.assertTrue(this.getListAppender().getMessages().size() > 0);
}
 
Example 14
Source File: Step4Test.java    From learning with Apache License 2.0 5 votes vote down vote up
@Before
public void createAndBind() throws Exception {
	JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:mem:test");
    dataSource.setUser("sa");
    dataSource.setPassword("sa");
    connection = dataSource.getConnection();

    String createStatement = "CREATE TABLE CUSTOMER("
    	+ "SSN VARCHAR(11) PRIMARY KEY,"
    	+ "FIRSTNAME VARCHAR(50),"
    	+ "LASTNAME VARCHAR(50),"
    	+ "STREETADDRESS VARCHAR(255),"
    	+ "CITY VARCHAR(60),"
    	+ "STATE VARCHAR(2),"
    	+ "POSTALCODE VARCHAR(60),"
		+ "DOB DATE,"
    	+ "CHECKINGBALANCE DECIMAL(14,2),"
    	+ "SAVINGSBALANCE DECIMAL(14,2));";
    
    String insertCustomer = "INSERT INTO CUSTOMER VALUES "
    		+ "('755-55-5555', 'Joseph', 'Smith', '123 Street', 'Elm', 'NC', '27808', '1970-01-01', 14000.40, 22000.99);";

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

    namingMixIn = new NamingMixIn();
    namingMixIn.initialize();
    bindDataSource(namingMixIn.getInitialContext(), "java:jboss/datasources/CustomerDS", dataSource);
}
 
Example 15
Source File: AppConfig.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public DataSource getDataSource() {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:tcp://localhost/c:/workspace/SpringDI/gossip");
    dataSource.setUser("caterpillar");
    dataSource.setPassword("12345678");
    return dataSource;
}
 
Example 16
Source File: AppConfig.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Profile("dev")
@Bean
public DataSource getDataSource() {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL(jdbcUrl);
    dataSource.setUser(user);
    dataSource.setPassword(password);
    
    return dataSource;
}
 
Example 17
Source File: Service.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static UserService getUserService() throws IOException {
	Properties prop = new Properties();
	prop.load(Main.class.getClassLoader().getResourceAsStream("jdbc.properties"));		
	
	JdbcDataSource dataSource = new JdbcDataSource();
       dataSource.setURL(prop.getProperty("cc.openhome.jdbcUrl"));
       dataSource.setUser(prop.getProperty("cc.openhome.user"));
       dataSource.setPassword(prop.getProperty("cc.openhome.password"));
       
       AccountDAO acctDAO = new AccountDAOJdbcImpl(dataSource);
       MessageDAO messageDAO = new MessageDAOJdbcImpl(dataSource);
       
       UserService userService = new UserService(acctDAO, messageDAO);
	return userService;
}
 
Example 18
Source File: AppConfig.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public DataSource getDataSource() {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL(jdbcUrl);
    dataSource.setUser(user);
    dataSource.setPassword(password);
    return dataSource;
}
 
Example 19
Source File: MultiTenantProcessEngineTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
private DataSource createDataSource(String jdbcUrl, String jdbcUsername, String jdbcPassword) {
  JdbcDataSource ds = new JdbcDataSource();
  ds.setURL(jdbcUrl);
  ds.setUser(jdbcUsername);
  ds.setPassword(jdbcPassword);
  return ds;
}
 
Example 20
Source File: TestMybatis.java    From ace with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws Exception {

        TestH2 h2 = new TestH2();
        // 开始服务
        h2.startServer();
        h2.testH2();

        Class.forName("org.h2.Driver");
        JdbcDataSource JdbcDataSource=new JdbcDataSource();
        JdbcDataSource.setUrl("jdbc:h2:./test");
        JdbcDataSource.setUser("sa");
        JdbcDataSource.setPassword("");


        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("Production", transactionFactory, JdbcDataSource);

        Configuration configuration = new Configuration(environment);
        configuration.addMapper(PersonMapper.class);
        SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);


        SqlSession sqlSession = sqlSessionFactory.openSession();

        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person person=new Person();
        person.setId(1);
        Person person1= personMapper.queryByPrimaryKey(person);
        System.out.println(person1);

        h2.stopServer();


    }