java操作:mysql数据库导入、导出
来源:互联网 发布:东方财富通 for mac 编辑:程序博客网 时间:2024/06/05 04:40
首先在这之前,我将mysql数据库的密码、用户名等一些信息保存在对应的工程的属性文件中,以便在之后调用。
还有就是导出的一个数据库中包含的是多个表格,以数据库为单位导出,
这里记录一下属性文件的创建过程在工程的 src 文件下右击选择 new ->file -> 文件名以 .properties 结尾就可以了
属性文件的内容
jdbc.host=127.0.0.1 // localhost jdbc.exportDatabaseName=students // 要导出的数据库名称jdbc.password=123456 // 数据库密码jdbc.importPath=/Users/sz/Desktop/students.sql // 要导入的数据库所在路径jdbc.port=3306 // 端口号 MysqlPath=/usr/local/mysql/bin/ //mysql下的bin文件的路径 jdbc.exportPath=/Users/sz/Desktop/students.sql // 导出的数据库存放路径jdbc.username=root // 用户名jdbc.importDatabaseName=DesignModel // 要导入的目标数据库
代码如下:
package dataInput;import java.io.IOException;import java.io.*;import java.util.Properties;public class ImportAndExport { // 实现数据库的导出 public static void exportSql() throws IOException{ Properties properties = new Properties(); // 读取属性文件 properties.load(Import.class.getClassLoader().getResourceAsStream("jdbc.properties")); Runtime runtime = Runtime.getRuntime(); String command = getExportCommand(properties); // 这里其实是在命令窗口中执行的 command 命令行 runtime.exec(command); } // 实现数据库的导入 public static void importSql() throws IOException { Properties properties = new Properties(); // 读取属性文件 properties.load(Import.class.getClassLoader().getResourceAsStream("jdbc.properties")); Runtime runtime = Runtime.getRuntime(); //把所执行的命令将以字符串数组的形式出现 String cmdarray[] = getImportCommand(properties);//根据属性文件的配置获取数据库导入所需的命令,组成一个数组 Process process = runtime.exec(cmdarray[0]); //执行了第一条命令以后已经登录到mysql了,所以之后就是利用mysql的命令窗口 java.io.OutputStream os = process.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(os); //命令1和命令2要放在一起执行 // 这里会执行后面的代码, 将命令输出到mysql的命令窗口,进行执行 writer.write(cmdarray[1] + "\r\n" + cmdarray[2]); writer.flush(); writer.close(); os.close(); } // 得到 导入 数据库的命令 // 得到 导入数据 的 命令行语句 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 MysqlPath = properties.getProperty("MysqlPath"); // 路径是mysql中 bin 文件 的位置 //第一步,获取登录命令语句 String loginCommand = new StringBuffer().append(MysqlPath).append("mysql -h").append(host).append(" -u").append(username).append(" -p").append(password) .append(" -P").append(port).toString(); //第二步,获取切换数据库到目标数据库的命令语句 String switchCommand = new StringBuffer().append("use ").append(importDatabaseName).toString(); //第三步,获取导入的命令语句 String importCommand = new StringBuffer(" source ").append(importPath).toString(); //需要返回的命令语句数组 String[] commands = new String[] {loginCommand, switchCommand, importCommand}; return commands; } // 得到 导出数据 的 命令行语句 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 MysqlPath = properties.getProperty("MysqlPath"); // 路径是mysql中 bin 文件 的位置 //注意哪些地方要空格,哪些不要空格 command.append(MysqlPath).append("mysqldump -u").append(username).append(" -p").append(password)//密码是用的小p,而端口是用的大P。 .append(" -h").append(host).append(" -P").append(port).append(" ").append(exportDatabaseName).append(" -r ").append(exportPath); return command.toString(); }}
有错误地方希望指出。
阅读全文
2 0
- java操作:mysql数据库导入、导出
- MySQL 数据库导出导入操作
- java操作mysql导入导出
- Java Mysql 数据库导出导入
- mysql 导出导入数据库等常用操作
- MySQL操作及数据库导入导出
- java对mysql数据库的导入导出
- java调用mysql命令 导入导出数据库
- 数据库导入导出操作
- 数据库导入、导出操作
- mysql 导入导出操作
- mysql:导入导出操作
- mysql导入导出操作
- Mysql数据库导入导出
- mysql 数据库导入导出
- mysql 数据库导入导出
- mysql 数据库导入导出
- mysql数据库导入导出
- ATF EL3 Runtime Service
- android相对布局中编辑框与文本框对其问题
- 【剑指offer】面试题07:用两个栈实现队列
- shell和awk之间互传参数
- dmidecode查看linux硬件信息
- java操作:mysql数据库导入、导出
- 初步使用HttpClient
- Dubbo实战(一)快速入门
- 283. Move Zeroes
- Vue2.0 事件发射与接收
- windows命令行能连上ftp,但是资源管理器连接报550
- mybatis级联删除
- 图片处理
- OKHttp_utils第三方库的使用学习,亲自验证。