工作中使用SQL的时候很多,当使用的时候,经常遇到多行的SQL,通常在PL/SQL或其他地方写好SQL,测试没问题后,需要将SQL写到程序代码中,多行SQL需要拼接字符串,手动一行行添加很不方便,所以,既然经常会遇到,就写个小工具来自动处理吧。
该工具使用进行开发,我上传的程序已经打包成exe了(运行仍然需要系统有jre),源代码会在这里全部贴出,因为只有一个类。
先看两个实际运行图:
1.生成String类型,这个类型在大部分的编程语言中通用。
2.StringBuffer类型,这种类型适用于JAVA,从性能来看,这两种类型在执行多次时,StringBuffer效率更高。
代码很简单,就是两个textarea,通过\n分割字符串,然后一行行拼接。
代码如下:
- @SuppressWarnings("serial")
- public class CreateSqlWin extends JFrame {
- private JPanel contentPane;
- private JTextField txtStr;
- private JRadioButton rdbtnString;
- private JRadioButton rdbtnStringbuffer;
- private JSplitPane splitPane;
- private JTextArea newSql;
- private JTextArea oldSql;
- private ImageIcon ico = new ImageIcon(this.getClass().getResource("sql.png"));
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- CreateSqlWin frame = new CreateSqlWin();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public CreateSqlWin() {
- setIconImage(ico.getImage());
- setMinimumSize(new Dimension(840, 600));
- setTitle("SQL转JAVA字符串");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 842, 605);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(new BorderLayout(0, 0));
- JPanel panel = new JPanel();
- panel.setPreferredSize(new Dimension(10, 80));
- contentPane.add(panel, BorderLayout.NORTH);
- panel.setLayout(new BorderLayout(0, 0));
- JPanel panel_1 = new JPanel();
- panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
- panel_1.setPreferredSize(new Dimension(300, 10));
- panel.add(panel_1, BorderLayout.CENTER);
- panel_1.setLayout(null);
- JLabel label = new JLabel("选择生成方式:");
- label.setBounds(10, 10, 153, 20);
- panel_1.add(label);
- rdbtnString = new JRadioButton("String");
- rdbtnString.setSelected(true);
- rdbtnString.setBounds(52, 36, 79, 23);
- panel_1.add(rdbtnString);
- rdbtnStringbuffer = new JRadioButton("StringBuffer");
- rdbtnStringbuffer.setBounds(144, 36, 107, 23);
- panel_1.add(rdbtnStringbuffer);
- ButtonGroup bGroup = new ButtonGroup();
- bGroup.add(rdbtnString);
- bGroup.add(rdbtnStringbuffer);
- txtStr = new JTextField();
- txtStr.setText("str");
- txtStr.setBounds(313, 31, 180, 33);
- panel_1.add(txtStr);
- txtStr.setColumns(10);
- JLabel label_1 = new JLabel("输入变量名:");
- label_1.setBounds(276, 13, 87, 15);
- panel_1.add(label_1);
- JPanel panel_3 = new JPanel();
- panel_3.setBorder(new MatteBorder(1, 0, 1, 1, (Color) new Color(0, 0, 0)));
- panel_3.setPreferredSize(new Dimension(200, 10));
- panel.add(panel_3, BorderLayout.EAST);
- panel_3.setLayout(new BorderLayout(0, 0));
- JButton button = new JButton("生成");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- //生成SQL
- String oldSqlStr = oldSql.getText();
- if(oldSqlStr.equals("")){
- JOptionPane.showMessageDialog(CreateSqlWin.this, "请在左侧输入SQL再执行!");
- return;
- }
- //清空
- if(!newSql.getText().equals("")){
- newSql.setText("");
- }
- String valibleName = txtStr.getText();
- if(valibleName.trim().equals("")){
- JOptionPane.showMessageDialog(CreateSqlWin.this, "请输入变量名!");
- return;
- }
- String[] sqls = oldSqlStr.split("\n");
- StringBuffer result = new StringBuffer();
- //对SQL进行拼接
- if(rdbtnString.isSelected()){
- //string形式
- for(int i=0;i<sqls.length-1;i++){
- if(result.toString().equals("")){
- result.append(valibleName+" = \" "+sqls[i]+" \"\n");
- }
- else {
- result.append(" +\" "+sqls[i]+" \"\n");
- }
- }
- result.append(" +\" "+sqls[sqls.length-1]+" \";\n");
- }
- else{
- //string形式
- for(int i=0;i<sqls.length;i++){
- result.append(valibleName+".append(\" "+sqls[i]+" \");\n");
- }
- }
- newSql.setText(result.toString());
- }
- });
- button.setFont(new Font("楷体", Font.PLAIN, 32));
- panel_3.add(button, BorderLayout.CENTER);
- JPanel panel_2 = new JPanel();
- panel_2.setBorder(new MatteBorder(0, 1, 1, 1, (Color) new Color(0, 0, 0)));
- contentPane.add(panel_2, BorderLayout.CENTER);
- panel_2.setLayout(new BorderLayout(0, 0));
- splitPane = new JSplitPane();
- splitPane.addComponentListener(new ComponentAdapter() {
- @Override
- public void componentResized(ComponentEvent e) {
- divider();
- }
- });
- panel_2.add(splitPane, BorderLayout.CENTER);
- JScrollPane scrollPane = new JScrollPane();
- splitPane.setLeftComponent(scrollPane);
- oldSql = new JTextArea();
- scrollPane.setViewportView(oldSql);
- JScrollPane scrollPane_1 = new JScrollPane();
- splitPane.setRightComponent(scrollPane_1);
- newSql = new JTextArea();
- scrollPane_1.setViewportView(newSql);
- JPanel panel_4 = new JPanel();
- FlowLayout flowLayout = (FlowLayout) panel_4.getLayout();
- flowLayout.setAlignment(FlowLayout.LEFT);
- panel_4.setPreferredSize(new Dimension(10, 30));
- panel_2.add(panel_4, BorderLayout.NORTH);
- JLabel lblsql = new JLabel("请在左侧输入你要格式化的SQL语句:");
- lblsql.setHorizontalAlignment(SwingConstants.LEFT);
- panel_4.add(lblsql);
- }
- public void divider(){
- splitPane.setDividerLocation(0.4);
- }
- }
代码中用到了一张图片,仅仅是用来显示图标的,可以使用任意图片代替或者去掉相应代码。
程序下载:
在发布该帖之前上传了一次,结果发现不是免费下载,需要1金币才可以,果断删除重新上传,结果就一直没反应了。
临时放个GOOGLE DRIVE的地址:https://docs.google.com/file/d/0ByAG1xopZV6kU3VfOGxQQU1LZjQ/edit?usp=sharing
CSDN地址彻底没希望了,不知道是不是有什么算法...刚上传的和已删除的一样难道就不行?
增加一个百度网盘地址:http://pan.baidu.com/share/link?shareid=181300461&uk=1325762948