操作sql脚本生成表

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脚本
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值