Search in sources :

Example 71 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestFtp method test.

private void test(String dir) throws Exception {
    Server server = FtpServer.createFtpServer("-ftpDir", dir, "-ftpPort", "8121").start();
    FtpServer ftp = (FtpServer) server.getService();
    ftp.setEventListener(this);
    FtpClient client = FtpClient.open("localhost:8121");
    client.login("sa", "sa");
    client.makeDirectory("ftp");
    client.changeWorkingDirectory("ftp");
    assertEquals("CWD", lastEvent.getCommand());
    client.makeDirectory("hello");
    client.changeWorkingDirectory("hello");
    client.changeDirectoryUp();
    assertEquals("CDUP", lastEvent.getCommand());
    client.nameList("hello");
    client.removeDirectory("hello");
    client.close();
    server.stop();
}
Also used : Server(org.h2.tools.Server) FtpServer(org.h2.dev.ftp.server.FtpServer) FtpServer(org.h2.dev.ftp.server.FtpServer) FtpClient(org.h2.dev.ftp.FtpClient)

Example 72 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestAutoReconnect method testWrongUrl.

private void testWrongUrl() throws Exception {
    deleteDb(getTestName());
    Server tcp = Server.createTcpServer().start();
    try {
        conn = getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";AUTO_SERVER=TRUE");
        assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";OPEN_NEW=TRUE");
        assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";OPEN_NEW=TRUE");
        conn.close();
        conn = getConnection("jdbc:h2:tcp://localhost/" + getBaseDir() + "/" + getTestName());
        assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, this).getConnection("jdbc:h2:" + getBaseDir() + "/" + getTestName() + ";AUTO_SERVER=TRUE;OPEN_NEW=TRUE");
        conn.close();
    } finally {
        tcp.stop();
    }
}
Also used : Server(org.h2.tools.Server)

Example 73 with Server

use of org.h2.tools.Server in project traccar by traccar.

the class ConsoleServlet method init.

@Override
public void init() {
    super.init();
    try {
        Field field = WebServlet.class.getDeclaredField("server");
        field.setAccessible(true);
        org.h2.server.web.WebServer server = (org.h2.server.web.WebServer) field.get(this);
        ConnectionInfo connectionInfo = new ConnectionInfo("Traccar|" + Context.getConfig().getString("database.driver") + "|" + Context.getConfig().getString("database.url") + "|" + Context.getConfig().getString("database.user"));
        Method method;
        method = org.h2.server.web.WebServer.class.getDeclaredMethod("updateSetting", ConnectionInfo.class);
        method.setAccessible(true);
        method.invoke(server, connectionInfo);
        method = org.h2.server.web.WebServer.class.getDeclaredMethod("setAllowOthers", boolean.class);
        method.setAccessible(true);
        method.invoke(server, true);
    } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        Log.warning(e);
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) ConnectionInfo(org.h2.server.web.ConnectionInfo)

Example 74 with Server

use of org.h2.tools.Server 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 75 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestTools method testSSL.

private void testSSL() throws SQLException {
    String result;
    Connection conn;
    try {
        result = runServer(0, new String[] { "-tcp", "-tcpAllowOthers", "-tcpPort", "9001", "-tcpPassword", "abcdef", "-tcpSSL" });
        assertContains(result, "ssl://");
        assertContains(result, ":9001");
        assertContains(result, "others can");
        assertTrue(result.indexOf("Starts the H2 Console") < 0);
        conn = getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa");
        conn.close();
        result = runServer(0, new String[] { "-tcpShutdown", "ssl://localhost:9001", "-tcpPassword", "abcdef" });
        assertContains(result, "Shutting down");
        assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:ssl://localhost:9001/mem:", "sa", "sa");
        result = runServer(0, new String[] { "-web", "-webPort", "9002", "-webAllowOthers", "-webSSL", "-pg", "-pgAllowOthers", "-pgPort", "9003", "-tcp", "-tcpAllowOthers", "-tcpPort", "9006", "-tcpPassword", "abc" });
        Server stop = server;
        assertContains(result, "https://");
        assertContains(result, ":9002");
        assertContains(result, "pg://");
        assertContains(result, ":9003");
        assertContains(result, "others can");
        assertTrue(result.indexOf("only local") < 0);
        assertContains(result, "tcp://");
        assertContains(result, ":9006");
        conn = getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa");
        conn.close();
        result = runServer(0, new String[] { "-tcpShutdown", "tcp://localhost:9006", "-tcpPassword", "abc", "-tcpShutdownForce" });
        assertContains(result, "Shutting down");
        stop.shutdown();
        assertThrows(ErrorCode.CONNECTION_BROKEN_1, this).getConnection("jdbc:h2:tcp://localhost:9006/mem:", "sa", "sa");
    } finally {
        shutdownServers();
    }
}
Also used : Server(org.h2.tools.Server) Connection(java.sql.Connection)

Aggregations

Server (org.h2.tools.Server)47 Connection (java.sql.Connection)27 SQLException (java.sql.SQLException)22 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)15 IOException (java.io.IOException)13 ResultSet (java.sql.ResultSet)10 DbException (org.h2.message.DbException)9 Socket (java.net.Socket)8 Test (org.junit.Test)8 Properties (java.util.Properties)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ServerSocket (java.net.ServerSocket)6 PrintStream (java.io.PrintStream)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 Task (org.h2.util.Task)5 TcpServer (org.h2.server.TcpServer)4 PgServer (org.h2.server.pg.PgServer)4 WebServer (org.h2.server.web.WebServer)4