package com.easy.main;

import java.sql.SQLException;

import org.h2.tools.Server;

public class H2Server {
	private Server serverTcp;
	private Server serverWeb;
	private String port = "9094";
	private String dbDir = "easycms";
	private String user = "root";
	private String password = "root";

	public void startServer() {
		try {
			System.out.println("正在启动h2...");
			serverTcp = Server.createTcpServer().start();//默认9092
			serverWeb = Server.createWebServer().start();//http://localhost:8082/login.jsp
		} catch (SQLException e) {
			System.out.println("启动h2出错：" + e.toString());
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void stopServer() {
		if (serverWeb != null) {
			System.out.println("正在关闭h2...");
			serverWeb.stop();
			System.out.println("关闭成功.");
		}
		if (serverTcp != null) {
			System.out.println("正在关闭h2...");
			serverTcp.stop();
			System.out.println("关闭成功.");
		}
	}

	public void useH2() {
		try {
			//new Console().runTool("-url", "jdbc:h2:file:C:/data/easycms", "-user", "root", "-password", "root");
			/*Class.forName("org.h2.Driver");
			Connection conn = DriverManager.getConnection("jdbc:h2:file:C:/data/easycms", user, password);
			File f = new File("D:\\workspace\\easycms\\easycms.sql") ; 	
			Reader reader = new FileReader(f);
			RunScript.execute(conn, reader);*/
			/*Statement stat = conn.createStatement();
			// insert data
			stat.execute("CREATE TABLE TEST(NAME VARCHAR)");
			stat.execute("INSERT INTO TEST VALUES('Hello World')");

			// use data
			ResultSet result = stat.executeQuery("select name from test ");
			int i = 1;
			while (result.next()) {
				System.out.println(i++ + ":" + result.getString("name"));
			}
			result.close();
			stat.close();*/
			//conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		H2Server h2 = new H2Server();
		h2.startServer();
		//h2.useH2();
		//h2.stopServer();
		System.out.println("==END==");
	}
}
