private void initDB() throws IOException {
//db.sql目录可以放在和src平等目录的 congfig下
String path = this.getClass().getClassLoader().getResource("db.sql").getPath();
path = path.substring(1);//由于盘符之前多了 / 例如: /D/...
//打开一个cmd窗口
String command = "cmd /c mysql -uroot -p123456 <" + path;
//采用线程去执行
Process process = Runtime.getRuntime().exec(command);
//读取返回的错误流
InputStream in = process.getErrorStream();
int len = 0;
byte buffer[] = new byte[1024];
StringBuffer sb = new StringBuffer();
while((len=in.read(buffer))>0){
sb.append(new String(buffer,0,len));
}
if(sb.length()>0){
throw new RuntimeException(sb.toString());
}
}
javaee 网站初始化数据库,导入mysql 数据库
最新推荐文章于 2022-11-24 17:00:54 发布