根据MySQL表数据导出INSERT INTO语句的方法

因项目中有需要需根据MySQL表数据生成INSERT INTO语句,在网上找了些现成的代码,原作者是谁就不知道了,但是发现有BUG,不能适用,遂对他人代码进行了修改。修改后能较好的导出INSERT INTO语句。

代码如下:

package dwz.interaction;

import Java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Update by internetroot on 2014-09-06.
 */
public class SqliteSQLGenerator {

private static Connection conn = null;
    private static Statement sm = null;
    private static String insert = "INSERT INTO";//插入sql
    private static String values = "VALUES";//values关键字
    private static List<String> tableList = new ArrayList<String>();//全局存放表名列表
    private static List<String> insertList = new ArrayList<String>();//全局存放insertsql文件的数据
    private static String filePath = "E://insertSQL.txt";//绝对路径 导出数据的文件

public static String generateTableDataSQL(String sql, String[] params) {
        return null;
    }

public static String executeSelectSQLFile(String file, String[] params) throws Exception {
        List<String> listSQL = new ArrayList<String>();
        connectSQL("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3308/htedu?useUnicode=true&characterEncoding=UTF-8", "root", "");//连接数据库
        listSQL = createSQL(file);//创建查询语句
        executeSQL(conn, sm, listSQL, tableList);//执行sql并拼装
        createFile();//创建文件
        return null;
    }

/**
    * 拼装查询语句
    *
    * @return 返回select集合
    */
    private static List<String> createSQL(String file) throws Exception {
        List<String> listSQL = new ArrayList<String>();
        BufferedReader br = null;
        InputStreamReader fr = null;
        InputStream is = null;

int i;//表名的第一个字符位置
        int k;//表名单最后一个字符的位置
        String tableName;

try {
            is = SqliteSQLGenerator.class.getResourceAsStream(file);
            fr = new InputStreamReader(is);
            br = new BufferedReader(fr);
            String rec = null;//一行
            while ((rec = br.readLine()) != null) {
                rec = rec.toLowerCase();
                i = rec.indexOf("from ", 1) + 5;
                k = rec.indexOf(" ", i);
                if (k == -1) {
                    k = rec.length();
                }
                ;
                tableName = rec.substring(i, k);
                tableList.add(tableName);
                //获取所有查询语句
                listSQL.add(rec.toString());
            }

} finally {
            if (br != null) {
                br.close();
            }
            if (fr != null) {
                fr.close();
            }
            if (is != null) {
                is.close();
            }
        }
        return listSQL;
    }

/**
    * 创建insertsql.txt并导出数据
    */
    private static void createFile() {
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("创建文件名失败!!");
                e.printStackTrace();
            }
        }
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(file);
            bw = new BufferedWriter(fw);
            if (insertList.size() > 0) {
                for (int i = 0; i < insertList.size(); i++) {
                    bw.append(insertList.get(i));
                    bw.append("\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a4d468a6dbe02c0e7563254c1604969b.html