Search in sources :

Example 86 with User

use of org.h2.engine.User in project h2database by h2database.

the class TestDataSource method testDataSourceFactory.

private void testDataSourceFactory() throws Exception {
    ObjectFactory factory = new JdbcDataSourceFactory();
    assertTrue(null == factory.getObjectInstance("test", null, null, null));
    Reference ref = new Reference("java.lang.String");
    assertTrue(null == factory.getObjectInstance(ref, null, null, null));
    ref = new Reference(JdbcDataSource.class.getName());
    ref.add(new StringRefAddr("url", "jdbc:h2:mem:"));
    ref.add(new StringRefAddr("user", "u"));
    ref.add(new StringRefAddr("password", "p"));
    ref.add(new StringRefAddr("loginTimeout", "1"));
    ref.add(new StringRefAddr("description", "test"));
    JdbcDataSource ds = (JdbcDataSource) factory.getObjectInstance(ref, null, null, null);
    assertEquals(1, ds.getLoginTimeout());
    assertEquals("test", ds.getDescription());
    assertEquals("jdbc:h2:mem:", ds.getURL());
    assertEquals("u", ds.getUser());
    assertEquals("p", ds.getPassword());
    Reference ref2 = ds.getReference();
    assertEquals(ref.size(), ref2.size());
    assertEquals(ref.get("url").getContent().toString(), ref2.get("url").getContent().toString());
    assertEquals(ref.get("user").getContent().toString(), ref2.get("user").getContent().toString());
    assertEquals(ref.get("password").getContent().toString(), ref2.get("password").getContent().toString());
    assertEquals(ref.get("loginTimeout").getContent().toString(), ref2.get("loginTimeout").getContent().toString());
    assertEquals(ref.get("description").getContent().toString(), ref2.get("description").getContent().toString());
    ds.setPasswordChars("abc".toCharArray());
    assertEquals("abc", ds.getPassword());
}
Also used : JdbcDataSourceFactory(org.h2.jdbcx.JdbcDataSourceFactory) ObjectFactory(javax.naming.spi.ObjectFactory) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) JdbcDataSource(org.h2.jdbcx.JdbcDataSource)

Example 87 with User

use of org.h2.engine.User in project h2database by h2database.

the class TestDataSource method testXAConnection.

private void testXAConnection(boolean userInDataSource) throws Exception {
    deleteDb("dataSource");
    JdbcDataSource ds = new JdbcDataSource();
    String url = getURL("dataSource", true);
    String user = getUser();
    ds.setURL(url);
    if (userInDataSource) {
        ds.setUser(user);
        ds.setPassword(getPassword());
    }
    if (userInDataSource) {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString());
    } else {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=", ds.toString());
    }
    XAConnection xaConn;
    if (userInDataSource) {
        xaConn = ds.getXAConnection();
    } else {
        xaConn = ds.getXAConnection(user, getPassword());
    }
    int traceId = ((JdbcXAConnection) xaConn).getTraceId();
    assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn"));
    xaConn.addConnectionEventListener(new ConnectionEventListener() {

        @Override
        public void connectionClosed(ConnectionEvent event) {
        // nothing to do
        }

        @Override
        public void connectionErrorOccurred(ConnectionEvent event) {
        // nothing to do
        }
    });
    XAResource res = xaConn.getXAResource();
    assertFalse(res.setTransactionTimeout(1));
    assertEquals(0, res.getTransactionTimeout());
    assertTrue(res.isSameRM(res));
    assertFalse(res.isSameRM(null));
    Connection conn = xaConn.getConnection();
    assertEquals(user.toUpperCase(), conn.getMetaData().getUserName());
    Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
    assertEquals(0, list.length);
    Statement stat = conn.createStatement();
    stat.execute("SELECT * FROM DUAL");
    conn.close();
    xaConn.close();
}
Also used : Statement(java.sql.Statement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) ConnectionEvent(javax.sql.ConnectionEvent) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 88 with User

use of org.h2.engine.User in project h2database by h2database.

the class TestSpatial method testInPlaceUpdate.

/**
 * If the user mutate the geometry of the object, the object cache must not
 * be updated.
 */
private void testInPlaceUpdate() throws SQLException {
    try (Connection conn = getConnection(URL)) {
        ResultSet rs = conn.createStatement().executeQuery("SELECT 'POINT(1 1)'::geometry");
        assertTrue(rs.next());
        // Mutate the geometry
        ((Geometry) rs.getObject(1)).apply(new AffineTransformation(1, 0, 1, 1, 0, 1));
        rs.close();
        rs = conn.createStatement().executeQuery("SELECT 'POINT(1 1)'::geometry");
        assertTrue(rs.next());
        // Check if the geometry is the one requested
        assertEquals(1, ((Point) rs.getObject(1)).getX());
        assertEquals(1, ((Point) rs.getObject(1)).getY());
        rs.close();
    }
}
Also used : ValueGeometry(org.h2.value.ValueGeometry) Geometry(org.locationtech.jts.geom.Geometry) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) AffineTransformation(org.locationtech.jts.geom.util.AffineTransformation)

Example 89 with User

use of org.h2.engine.User in project h2database by h2database.

the class MixedMode method main.

/**
 * This method is called when executing this sample application from the
 * command line.
 *
 * @param args the command line parameters
 */
public static void main(String... args) throws Exception {
    // start the server, allows to access the database remotely
    Server server = Server.createTcpServer("-tcpPort", "9081");
    server.start();
    System.out.println("You can access the database remotely now, using the URL:");
    System.out.println("jdbc:h2:tcp://localhost:9081/~/test (user: sa, password: sa)");
    // now use the database in your application in embedded mode
    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
    // some simple 'business usage'
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE TIMER IF EXISTS");
    stat.execute("CREATE TABLE TIMER(ID INT PRIMARY KEY, TIME VARCHAR)");
    System.out.println("Execute this a few times: " + "SELECT TIME FROM TIMER");
    System.out.println("To stop this application " + "(and the server), run: DROP TABLE TIMER");
    try {
        while (true) {
            // runs forever, except if you drop the table remotely
            stat.execute("MERGE INTO TIMER VALUES(1, NOW())");
            Thread.sleep(1000);
        }
    } catch (SQLException e) {
        System.out.println("Error: " + e.toString());
    }
    conn.close();
    // stop the server
    server.stop();
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 90 with User

use of org.h2.engine.User in project sql2o by aaberg.

the class H2Tests method setUp.

@Before
public void setUp() throws Exception {
    driverClassName = "org.h2.Driver";
    url = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
    user = "sa";
    pass = "";
    org.h2.jdbcx.JdbcDataSource datasource = new org.h2.jdbcx.JdbcDataSource();
    datasource.setURL(url);
    datasource.setUser(user);
    datasource.setPassword(pass);
    ds = datasource;
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Aggregations

Connection (java.sql.Connection)36 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)17 Statement (java.sql.Statement)17 ResultSet (java.sql.ResultSet)16 Server (org.h2.tools.Server)15 DbException (org.h2.message.DbException)14 Column (org.h2.table.Column)12 ValueString (org.h2.value.ValueString)12 Properties (java.util.Properties)10 Database (org.h2.engine.Database)10 Schema (org.h2.schema.Schema)8 IOException (java.io.IOException)7 User (org.h2.engine.User)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 SimpleResultSet (org.h2.tools.SimpleResultSet)7 Value (org.h2.value.Value)7 PrintStream (java.io.PrintStream)6 Timestamp (java.sql.Timestamp)6 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)6