initHsqlDB()

private void initHsqlDB() throws Exception {
// 创建HSQL数据库并初始化用户信息。
String path = targetDB.getAdd();
File dir = new File(path);
// 如果没有这个目录,创建这个目录
if(!dir.exists()){
System.out.println("make dirs:"+path);
dir.mkdirs();
}
// 如果有子目录,删除所有子目录
File[] subDirFile = dir.listFiles();
for (int i = 0; i < subDirFile.length; i++) {
System.out.println("delect "+ subDirFile[i].getAbsolutePath());
subDirFile[i].delete();
}
Class.forName("org.hsqldb.jdbc.JDBCDriver");
Connection hsqlConn = DriverManager.getConnection("jdbc:hsqldb:file:"
+ this.targetDB.getAdd() + java.io.File.separator
+ this.targetDB.getDbName(), "SA", "");
for (int k = 0; k < users.size(); k++) {
String user = users.get(k).getUserName().toUpperCase();
String pwd = users.get(k).getPassword();
// 创建HSQLDB的Schema和用户
Statement stmt = hsqlConn.createStatement();
String sql = "CREATE USER " + user + " PASSWORD '"
+ pwd + "' ADMIN";
System.out.println(sql);
stmt.execute(sql);
sql = "CREATE SCHEMA " + user
+ " AUTHORIZATION DBA";
stmt.execute(sql);
System.out.println(sql);
hsqlConn.commit();
}
hsqlConn.close();

//创建存储oracle数据结构信息表。
for(int k = 0; k < users.size(); k++){
String user = users.get(k).getUserName().toUpperCase();
String pwd = users.get(k).getPassword();
hsqlConn = targetDB.getConn(user, pwd);
Statement stm = hsqlConn.createStatement();
stm.execute(HsqlDataBase.UBHAVE_TABINFO_DDL);
stm.execute(HsqlDataBase.UBHAVE_SEQINFO_DDL);
hsqlConn.commit();
hsqlConn.close();
}


// 如果不存在文件dbscrpit.sql,则创建,并把所有表的select写入文件(需要转换所有表)
File file = new File("dbscrpit.sql");
if (file.exists())
return;
FileOutputStream fos = null;
// 分析数据库,创建文件:dbscrpit.sql
file.createNewFile();
System.out.println(file.getAbsolutePath());

System.out.println(file.getAbsolutePath());
fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos);
Connection conn = this.originDB.getConn("topeng", "topeng");
if (null == conn) {
System.out.println("connect database faile");
return;
}
String sqlTables = "";
for (DBUser user : this.users) {
sqlTables = "select TABLE_NAME from dba_tables where OWNER='"
+ user.getUserName().toUpperCase() + "'";

Statement stmt = conn.createStatement();
System.out.println(sqlTables);
ResultSet rs = stmt.executeQuery(sqlTables);
while (rs.next()) {
String wsql = "select * from " + rs.getString("TABLE_NAME")
+ ";\n";
System.out.println(wsql);
writer.write(wsql);
writer.flush();
}

}
writer.close();
}

你可能感兴趣的:(数据结构,oracle,sql,jdbc,HSQLDB)