使用io流将insert语句修改为delete语句

1 篇文章 0 订阅
1 篇文章 0 订阅

使用io流将insert语句修改为delete语句,delete语句使用主键作为删除条件,并将结果输出到文件中

import java.io.*;
public class MyInsertToDelete {

    public static void main(String[] args) {
        // 文件路径
        String filePath = "D:/b.sql";
        File file = new File(filePath);
        //字符输入流进行读取操作读取
        BufferedReader reader = null;
        // 每次写入一行
        FileWriter writer = null;

        //每一行的内容
        String tempString = null;
        // 行号
        int line = 1;

        try {
            writer = new FileWriter("D:/a.sql");
            //输入字节流,FileInputStream主要用来操作文件输入流
            FileInputStream intput = new FileInputStream(file);
            // System.out.println("以行为单位读取文件内容,一次读一整行:")
            //InputStreamReader是转换流,将字节流转成字符流
            reader = new BufferedReader(new InputStreamReader(intput));
            while ((tempString = reader.readLine()) != null) {
                writer.write(convert(tempString)+";\n");
                //System.out.println(convert(tempString));
                //System.out.println(tempString);
                writer.write(tempString+"\n");
                line++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String convert(String sql) {
        String result = "DELETE FROM ";
        sql = sql.toUpperCase();
        sql = sql.replace("INSERT INTO ", "");
        String table = sql.substring(0, sql.indexOf("("));
        sql = sql.replace(table, "").replace("'", "");
        String[] values = sql.split("VALUES");
        String[] k = values[0].replace("(", "").replace(")", "").split(",");
        String[] v = values[1].replace("(", "").replace(")", "").split(",");
        result += table + " WHERE ";
        result += k[0] + "=" + "'" + v[0] + "'";
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将 Excel 转换成 SQL 建表语句Java 代码示例: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelToSqlConverter { public static void main(String[] args) throws IOException { // 读取 Excel 文件 FileInputStream inputStream = new FileInputStream(new File("input.xlsx")); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); // 获取表名 String tableName = sheet.getSheetName(); // 遍历 Excel 表格数据 Iterator<Row> iterator = sheet.iterator(); while (iterator.hasNext()) { Row currentRow = iterator.next(); StringBuilder sqlBuilder = new StringBuilder(); if (currentRow.getRowNum() == 0) { // 第一行为表头,生成表结构 sqlBuilder.append("CREATE TABLE ").append(tableName).append(" ("); Iterator<Cell> cellIterator = currentRow.iterator(); while (cellIterator.hasNext()) { Cell currentCell = cellIterator.next(); sqlBuilder.append(currentCell.getStringCellValue()).append(" VARCHAR(255), "); } sqlBuilder.delete(sqlBuilder.length() - 2, sqlBuilder.length()); // 去掉最后一个逗号 sqlBuilder.append(");"); } else { // 生成数据插入语句 sqlBuilder.append("INSERT INTO ").append(tableName).append(" VALUES ("); Iterator<Cell> cellIterator = currentRow.iterator(); while (cellIterator.hasNext()) { Cell currentCell = cellIterator.next(); if (currentCell.getCellType() == Cell.CELL_TYPE_STRING) { sqlBuilder.append("'").append(currentCell.getStringCellValue()).append("', "); } else if (currentCell.getCellType() == Cell.CELL_TYPE_NUMERIC) { sqlBuilder.append(currentCell.getNumericCellValue()).append(", "); } } sqlBuilder.delete(sqlBuilder.length() - 2, sqlBuilder.length()); // 去掉最后一个逗号 sqlBuilder.append(");"); } System.out.println(sqlBuilder.toString()); } workbook.close(); inputStream.close(); } } ``` 这段代码可以将 Excel 文件中的数据转换成 SQL 建表语句和数据插入语句,你只需要修改 "input.xlsx" 为你要转换的 Excel 文件名即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值