Print.out自己写的工具类相当于system.out.println();
Properties properties = new Properties();
properties.load(Resources.getResourceAsStream("PropertiesName"));
Print.Out(properties.getProperty("status"));
if(properties.get("status").equals("0")) {
String username = properties.getProperty("jdbc.user");
String password = properties.getProperty("jdbc.password");
String host = properties.getProperty("jdbc.host");
String port = properties.getProperty("jdbc.port");
String importDatabaseName = properties.getProperty("jdbc.importDatabaseName");
//使用要导入sql脚本的位置
String file = Resources.getResourceURL("powermanager.sql").getFile();
String desfile = file.substring(1);
StringBuilder sb = new StringBuilder();
String loginCommand = new StringBuffer()
.append("mysql -u")
.append(username)
.append(" -p")
.append(password)
.append(" -h")
.append(host)
.append(" -P")
.append(port)
.toString();
String dbCommand = new StringBuffer()
.append("use ")
.append(importDatabaseName)
.toString();
//第三步:拼接要导入的sql脚本
String sqlCommand = new StringBuffer()
.append("source ")
.append(desfile)
.toString();
String[] str = new String[] {loginCommand,dbCommand,sqlCommand};
Print.Out("数据库正在初始化...");
Process exec = Runtime.getRuntime().exec(str[0]);
OutputStream outputStream = exec.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
writer.write(str[1]+"\r\n"+str[2]);
writer.flush();
writer.close();
outputStream.close();
//修改Properties配置文件的内容
properties.setProperty("status", "1");
FileOutputStream fos = new FileOutputStream(Resources.getResourceURL("config/db.properties").getFile());
//保存修改的信息
properties.store(fos, "数据库生成");
Thread.sleep(10000);
Print.Out("数据库生成...");
/*
* 以上结束了动态生成表的sql脚本
*/