springMVC数据库导入导出

数据库导入导出,有2个BUG
第一个弹出的选择框随意移动
第二个导出或导入不成功不会提示报错
.jsp页面
导出导出的jsp页面在这里插入图片描述在这里插入图片描述在这里插入图片描述
ExportImportMysqlCodeController
/**导入sql文件
* @param
* @throws Exception
*/
@RequestMapping(value="/ImportCode")
@ResponseBody
public Object ImportCode() throws Exception{
if(!Jurisdiction.buttonJurisdiction(menuUrl, “add”)){} //校验权限
logBefore(logger, Jurisdiction.getUsername()+“导入sql文件”);
Map<String, Object> map = new HashMap<String, Object>();
/JFrame frame = new JFrame();
frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.setBounds(400, 200, 0, 0);
frame.setVisible(true);
/
JFileChooser fileChoser = new JFileChooser();
UIManager.setLookAndFeel(“com.jtattoo.plaf.luna.LunaLookAndFeel”);//改变选择框的外观
fileChoser.setDragEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(“数据库文件(sql/SQL)”,“sql”);
fileChoser.setFileFilter(filter);
fileChoser.setDialogTitle(“导入”);
int open = fileChoser.showOpenDialog(null);
if(open == 0){
String name = fileChoser.getSelectedFile().getName();
try {
String[] name1 = name.split("\.");
if(name1[1].equals(“sql”) || name1[1].equals(“SQL”)){
InputStream is = ExportImportMysqlCodeController.class.getClassLoader().getResourceAsStream(“jdbc.properties”);
Properties properties = new Properties();
properties.setProperty(“jdbc.importPath”,fileChoser.getSelectedFile().getPath());
properties.load(is);
ExportImportMysqlCodeController.importSql(properties);
map.put(“code”,200);
map.put(“msg”,“导入成功”);
}else{
map.put(“code”,201);
map.put(“msg”,“操作有误,例子 aa.sql或aa.SQL”);
}
}catch (Exception e){
map.put(“code”,300);
map.put(“msg”,“操作有误,请重新导入”);
}
}else{
System.out.println(“取消”);
map.put(“code”,301);
map.put(“msg”,“释放取消按钮”);
}

	return AppUtil.returnObject(new PageData(), map);
}
/**导出生成sql文件
 * @param
 * @throws Exception
 */
@RequestMapping(value="/exportCode")
@ResponseBody
public Object exportCode() throws Exception{
	if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){} 		//校验权限
	logBefore(logger, Jurisdiction.getUsername()+"导出生成sql文件");
	Map<String, Object> map = new HashMap<String, Object>();
	JFileChooser fileChoser = new JFileChooser();
	UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel");//改变选择框的外观
	fileChoser.setDragEnabled(true);
	FileNameExtensionFilter filter = new FileNameExtensionFilter("数据库文件(sql/SQL)","sql");
	fileChoser.setFileFilter(filter);
	fileChoser.setDialogTitle("导出");
	fileChoser.setSelectedFile(new File("tzb.sql"));
	int open = fileChoser.showSaveDialog(null);
	if(open == 0){
		String name = fileChoser.getSelectedFile().getName();//获取输入的名称
		try{
			String[] name1 = name.split("\\.");
			if(name1[1].equals("sql") || name1[1].equals("SQL")){
				InputStream is = ExportImportMysqlCodeController.class.getClassLoader().getResourceAsStream("jdbc.properties");
				Properties properties = new Properties();
				properties.setProperty("jdbc.exportPath",fileChoser.getSelectedFile().getPath());
				properties.load(is);
				ExportImportMysqlCodeController.export(properties);
				map.put("code",200);
				map.put("msg","导出成功");
			}else{
				map.put("code",201);
				map.put("msg","操作有误,例子 aa.sql或aa.SQL");
			}
		}catch (Exception e){
			map.put("code",300);
			map.put("msg","操作有误,请重新导出");
		}
	}else{
		System.out.println("取消成功");
		map.put("code",301);
		map.put("msg","释放取消按钮");
	}

	return AppUtil.returnObject(new PageData(), map);
}




/**
 * 根据属性文件的配置导出指定位置的指定数据库到指定位置
 * @param properties
 * @throws IOException
 */
public static void export(Properties properties) throws IOException {
    Runtime runtime = Runtime.getRuntime();
    String command = getExportCommand(properties);
    runtime.exec(command);//这里简单一点异常我就直接往上抛
}

/**
 * 根据属性文件的配置把指定位置的指定文件内容导入到指定的数据库中
 * 在命令窗口进行mysql的数据库导入一般分三步走:
 * 第一步是登到到mysql; mysql -uusername -ppassword -hhost -Pport -DdatabaseName;如果在登录的时候指定了数据库名则会
 * 直接转向该数据库,这样就可以跳过第二步,直接第三步;
 * 第二步是切换到导入的目标数据库;use importDatabaseName;
 * 第三步是开始从目标文件导入数据到目标数据库;source importPath;
 * @param properties
 * @throws IOException
 */
public static void importSql(Properties properties) throws IOException {
    Runtime runtime = Runtime.getRuntime();
    //因为在命令窗口进行mysql数据库的导入一般分三步走,所以所执行的命令将以字符串数组的形式出现
    String cmdarray[] = getImportCommand(properties);//根据属性文件的配置获取数据库导入所需的命令,组成一个数组
    //runtime.exec(cmdarray);//这里也是简单的直接抛出异常
    Process process = runtime.exec(cmdarray[0]);
    //执行了第一条命令以后已经登录到mysql了,所以之后就是利用mysql的命令窗口
    //进程执行后面的代码
    OutputStream os = process.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(os);
    //命令1和命令2要放在一起执行
    writer.write(cmdarray[1] + "\r\n" + cmdarray[2]);
    writer.flush();
    writer.close();
    os.close();
}

/**
 * 利用属性文件提供的配置来拼装命令语句
 * 在拼装命令语句的时候有一点是需要注意的:一般我们在命令窗口直接使用命令来
 * 进行导出的时候可以简单使用“>”来表示导出到什么地方,即mysqldump -uusername -ppassword databaseName > exportPath,
 * 但在Java中这样写是不行的,它需要你用-r明确的指出导出到什么地方,如:
 * mysqldump -uusername -ppassword databaseName -r exportPath。
 * @param properties
 * @return
 */
private static String getExportCommand(Properties properties) {
    StringBuffer command = new StringBuffer();
    String username = properties.getProperty("jdbc.username");//用户名
    String password = properties.getProperty("jdbc.password");//用户密码
    String exportDatabaseName = properties.getProperty("jdbc.exportDatabaseName");//需要导出的数据库名
    String host = properties.getProperty("jdbc.host");//从哪个主机导出数据库,如果没有指定这个值,则默认取localhost
    String port = properties.getProperty("jdbc.port");//使用的端口号
    String exportPath = properties.getProperty("jdbc.exportPath");//导出路径
    String tablename = properties.getProperty("jdbc.tableName");//导出表

    //注意哪些地方要空格,哪些不要空格
    command.append("mysqldump -u")
            .append(username)
            .append(" -p")
            .append(password)//密码是用的小p,而端口是用的大P。
            .append(" -h")
            .append(host)
            .append(" -P")
            .append(port)
            .append(" ")
            .append(exportDatabaseName)
            .append(" "+tablename)
            .append(" -r ")
            .append(exportPath);
    return command.toString();
}

/**
 * 根据属性文件的配置,分三步走获取从目标文件导入数据到目标数据库所需的命令
 * 如果在登录的时候指定了数据库名则会
 * 直接转向该数据库,这样就可以跳过第二步,直接第三步;
 * @param properties
 * @return
 */
private static String[] getImportCommand(Properties properties) {
    String username = properties.getProperty("jdbc.username");//用户名
    String password = properties.getProperty("jdbc.password");//密码
    String host = properties.getProperty("jdbc.host");//导入的目标数据库所在的主机
    String port = properties.getProperty("jdbc.port");//使用的端口号
    String importDatabaseName = properties.getProperty("jdbc.importDatabaseName");//导入的目标数据库的名称
    String importPath = properties.getProperty("jdbc.importPath");//导入的目标文件所在的位置
    //第一步,获取登录命令语句
    String loginCommand = new StringBuffer().append("mysql -u").append(username).append(" -p").append(password).append(" -h").append(host)
            .append(" -P").append(port).toString();
    //第二步,获取切换数据库到目标数据库的命令语句
    String switchCommand = new StringBuffer("use ").append(importDatabaseName).toString();
    //第三步,获取导入的命令语句
    String importCommand = new StringBuffer("source ").append(importPath).toString();
    //需要返回的命令语句数组
    String[] commands = new String[] {loginCommand, switchCommand, importCommand};
    return commands;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值