package wanjing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SqlReplaceUtil extends JFrame {

    private static final Pattern HUMP_PATTERN = Pattern.compile("\\?");
    JPanel jp;
    JButton b1;
    JTextArea sqlTextField;
    JTextArea paramTextField;
    JTextArea resultTextField;
    JLabel lb3;
    JLabel lb4;

    SqlReplaceUtil() {
        jp = new JPanel();
        b1 = new JButton("替换");
        sqlTextField = new JTextArea("输入待处理的SQL,比如:insert into test (id,name,age) values (?,?,?)",10,90);
        paramTextField = new JTextArea("输入待处理参数,比如:100(Integer),zhangsan(String),null",10,90);
        resultTextField = new JTextArea(10,90);
        lb3 = new JLabel("结果为:");
        lb4 = new JLabel("");
        b1.addActionListener(new ActionListener() {//响应按钮的事件
            public void actionPerformed(ActionEvent e) {
                try {
                    lb4.setText("");
                    String sql = sqlTextField.getText();
                    String param = paramTextField.getText();
                    resultTextField.setText(replaceSql(sql, param));
                } catch (Exception ex) {
                    lb4.setText(ex.getMessage());
                }
            }

        });
        jp.add(sqlTextField);
        jp.add(paramTextField);
        jp.add(b1);
        jp.add(resultTextField);
        jp.add(lb4);

        add(jp);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLocation(600, 200);
        setVisible(true);
        pack();
        setSize(1050, 700);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
    }

    public static void main(String[] args) {
        SqlReplaceUtil e = new SqlReplaceUtil();
        Container contentPane = e.getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
    }

    private String replaceSql(String sql, String param) {
        String[] split = param.split(",");
        //校验数量
        int paramCount = split.length;
        int matcherCount = 0;
        Matcher matcher = HUMP_PATTERN.matcher(sql);
        while (matcher.find()) {
            matcherCount++;
        }
        if (paramCount != matcherCount) {
            throw new RuntimeException("待替换参数和参数数量不一致");
        }
        //处理参数
        for (int i = 0; i < split.length; i++) {
            split[i] = split[i].trim();
            int index = split[i].lastIndexOf('(');
            if (split[i].equals("null")||index<0) {
                continue;
            }
            split[i] = "'" + split[i].substring(0, index) + "'";
        }
        StringBuffer sb = new StringBuffer();
        Matcher matcher2 = HUMP_PATTERN.matcher(sql);
        int index = 0;
        while (matcher2.find()) {
            matcher2.appendReplacement(sb, split[index]);
            index++;
        }
        matcher2.appendTail(sb);
        return sb.toString();
    }
}

  

 posted on 2020-04-23 23:45  改变一下  阅读(1262)  评论(0编辑  收藏  举报