1.因为内存模式运行时不支持整体备份还原,关掉服务数据丢失,所以采用jsp 方式导出内存中的数据
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//取得spring 数据链接
DataSource dataSource = (DataSource)getBeans("dataSource");
Connection c = dataSource.getConnection();
Statement s = c.createStatement();
ResultSet r = s.executeQuery("SELECT *FROM INFORMATION_SCHEMA.TABLES where table_type='TABLE' ORDER BY TABLE_NAME desc");
Statement s2 = c.createStatement();
String action = request.getParameter("action");
while(r.next()){
String table = r.getString("TABLE_NAME");
System.out.println("table:"+table);
if(action !=null&&action.equals("imp")){
s2.addBatch("delete from "+table+";");
stmt.addBatch("INSERT INTO "+table+" ( SELECT * FROM CSVREAD('c:/backup/"+table+".txt','','charset=UTF-8'));");
}
if(action !=null&&action.equals("exp")){
s2.addBatch("CALL CSVWRITE('D:/h2base/backup/"+table+".txt', 'SELECT * FROM "+table+"','charset=UTF-8');");
}
System.out.println("table:"+table+" -done");
}
/*
//jdbc 链接内存数据库
if(action!=null&&action.equals("test")){
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:dataSource", "sa", "");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM PORTAL_NEWS ");
while(rs.next()) {
System.out.println(rs.getString("id")+","+new String(rs.getString("news_title").getBytes("gbk"),"utf-8"));
}
conn.close();
}*/
s2.executeBatch();
s2.clearBatch();
c.close();
2.服务模式导出数据库
Backup.execute("D://h2base//aaa.zip", "~/db", "mydb", false); //备份
Restore.execute(D://h2base//aaa.zip, "~/db", "mydb");//还原