SQL转Java代码小工具

工作中使用SQL的时候很多,当使用hibernate的时候,经常遇到多行的SQL,通常在PL/SQL或其他地方写好SQL,测试没问题后,需要将SQL写到程序代码中,多行SQL需要拼接字符串,手动一行行添加很不方便,所以,既然经常会遇到,就写个小工具来自动处理吧。

该工具使用Java进行开发,我上传的程序已经打包成exe了(运行仍然需要系统有jre),源代码会在这里全部贴出,因为只有一个类。

先看两个实际运行图:

1.生成String类型,这个类型在大部分的编程语言中通用。

2.StringBuffer类型,这种类型适用于JAVA,从性能来看,这两种类型在执行多次时,StringBuffer效率更高。

代码很简单,就是两个textarea,通过 分割字符串,然后一行行拼接。

代码如下:

[java] view plain copy
 
  1. @SuppressWarnings("serial")  
  2. public class CreateSqlWin extends JFrame {  
  3.   
  4.     private JPanel contentPane;  
  5.     private JTextField txtStr;  
  6.     private JRadioButton rdbtnString;  
  7.     private JRadioButton rdbtnStringbuffer;  
  8.     private JSplitPane splitPane;  
  9.     private JTextArea newSql;  
  10.     private JTextArea oldSql;  
  11.       
  12.     private ImageIcon ico = new ImageIcon(this.getClass().getResource("sql.png"));  
  13.   
  14.     /** 
  15.      * Launch the application. 
  16.      */  
  17.     public static void main(String[] args) {  
  18.         EventQueue.invokeLater(new Runnable() {  
  19.             public void run() {  
  20.                 try {  
  21.                     CreateSqlWin frame = new CreateSqlWin();  
  22.                     frame.setVisible(true);  
  23.                 } catch (Exception e) {  
  24.                     e.printStackTrace();  
  25.                 }  
  26.             }  
  27.         });  
  28.     }  
  29.   
  30.     /** 
  31.      * Create the frame. 
  32.      */  
  33.     public CreateSqlWin() {  
  34.         setIconImage(ico.getImage());  
  35.         setMinimumSize(new Dimension(840, 600));  
  36.         setTitle("SQL转JAVA字符串");  
  37.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  38.         setBounds(100, 100, 842, 605);  
  39.         contentPane = new JPanel();  
  40.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  
  41.         setContentPane(contentPane);  
  42.         contentPane.setLayout(new BorderLayout(0, 0));  
  43.           
  44.         JPanel panel = new JPanel();  
  45.         panel.setPreferredSize(new Dimension(10, 80));  
  46.         contentPane.add(panel, BorderLayout.NORTH);  
  47.         panel.setLayout(new BorderLayout(0, 0));  
  48.           
  49.         JPanel panel_1 = new JPanel();  
  50.         panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));  
  51.         panel_1.setPreferredSize(new Dimension(300, 10));  
  52.         panel.add(panel_1, BorderLayout.CENTER);  
  53.         panel_1.setLayout(null);  
  54.           
  55.         JLabel label = new JLabel("选择生成方式:");  
  56.         label.setBounds(10, 10, 153, 20);  
  57.         panel_1.add(label);  
  58.           
  59.         rdbtnString = new JRadioButton("String");  
  60.         rdbtnString.setSelected(true);  
  61.         rdbtnString.setBounds(52, 36, 79, 23);  
  62.         panel_1.add(rdbtnString);  
  63.           
  64.         rdbtnStringbuffer = new JRadioButton("StringBuffer");  
  65.         rdbtnStringbuffer.setBounds(144, 36, 107, 23);  
  66.         panel_1.add(rdbtnStringbuffer);  
  67.           
  68.         ButtonGroup bGroup = new ButtonGroup();  
  69.         bGroup.add(rdbtnString);  
  70.         bGroup.add(rdbtnStringbuffer);  
  71.           
  72.         txtStr = new JTextField();  
  73.         txtStr.setText("str");  
  74.         txtStr.setBounds(313, 31, 180, 33);  
  75.         panel_1.add(txtStr);  
  76.         txtStr.setColumns(10);  
  77.           
  78.         JLabel label_1 = new JLabel("输入变量名:");  
  79.         label_1.setBounds(276, 13, 87, 15);  
  80.         panel_1.add(label_1);  
  81.           
  82.         JPanel panel_3 = new JPanel();  
  83.         panel_3.setBorder(new MatteBorder(1, 0, 1, 1, (Color) new Color(0, 0, 0)));  
  84.         panel_3.setPreferredSize(new Dimension(200, 10));  
  85.         panel.add(panel_3, BorderLayout.EAST);  
  86.         panel_3.setLayout(new BorderLayout(0, 0));  
  87.           
  88.         JButton button = new JButton("生成");  
  89.         button.addActionListener(new ActionListener() {  
  90.             public void actionPerformed(ActionEvent e) {  
  91.                 //生成SQL  
  92.                 String oldSqlStr = oldSql.getText();  
  93.                 if(oldSqlStr.equals("")){  
  94.                     JOptionPane.showMessageDialog(CreateSqlWin.this, "请在左侧输入SQL再执行!");  
  95.                     return;  
  96.                 }  
  97.                 //清空  
  98.                 if(!newSql.getText().equals("")){  
  99.                     newSql.setText("");  
  100.                 }  
  101.                 String valibleName = txtStr.getText();  
  102.                 if(valibleName.trim().equals("")){  
  103.                     JOptionPane.showMessageDialog(CreateSqlWin.this, "请输入变量名!");  
  104.                     return;  
  105.                 }  
  106.                 String[] sqls = oldSqlStr.split(" ");  
  107.                 StringBuffer result = new StringBuffer();  
  108.                 //对SQL进行拼接  
  109.                 if(rdbtnString.isSelected()){  
  110.                     //string形式  
  111.                     for(int i=0;i<sqls.length-1;i++){  
  112.                         if(result.toString().equals("")){  
  113.                             result.append(valibleName+" = " "+sqls[i]+" " ");  
  114.                         }  
  115.                         else {  
  116.                             result.append(" +" "+sqls[i]+" " ");  
  117.                         }  
  118.                     }  
  119.                     result.append(" +" "+sqls[sqls.length-1]+" "; ");  
  120.                 }  
  121.                 else{  
  122.                     //string形式  
  123.                     for(int i=0;i<sqls.length;i++){  
  124.                         result.append(valibleName+".append(" "+sqls[i]+" "); ");  
  125.                     }  
  126.                 }  
  127.                 newSql.setText(result.toString());  
  128.             }  
  129.         });  
  130.         button.setFont(new Font("楷体", Font.PLAIN, 32));  
  131.         panel_3.add(button, BorderLayout.CENTER);  
  132.           
  133.         JPanel panel_2 = new JPanel();  
  134.         panel_2.setBorder(new MatteBorder(0, 1, 1, 1, (Color) new Color(0, 0, 0)));  
  135.         contentPane.add(panel_2, BorderLayout.CENTER);  
  136.         panel_2.setLayout(new BorderLayout(0, 0));  
  137.           
  138.         splitPane = new JSplitPane();  
  139.         splitPane.addComponentListener(new ComponentAdapter() {  
  140.             @Override  
  141.             public void componentResized(ComponentEvent e) {  
  142.                 divider();  
  143.             }  
  144.         });  
  145.         panel_2.add(splitPane, BorderLayout.CENTER);  
  146.           
  147.         JScrollPane scrollPane = new JScrollPane();  
  148.         splitPane.setLeftComponent(scrollPane);  
  149.           
  150.         oldSql = new JTextArea();  
  151.         scrollPane.setViewportView(oldSql);  
  152.           
  153.         JScrollPane scrollPane_1 = new JScrollPane();  
  154.         splitPane.setRightComponent(scrollPane_1);  
  155.           
  156.         newSql = new JTextArea();  
  157.         scrollPane_1.setViewportView(newSql);  
  158.           
  159.         JPanel panel_4 = new JPanel();  
  160.         FlowLayout flowLayout = (FlowLayout) panel_4.getLayout();  
  161.         flowLayout.setAlignment(FlowLayout.LEFT);  
  162.         panel_4.setPreferredSize(new Dimension(10, 30));  
  163.         panel_2.add(panel_4, BorderLayout.NORTH);  
  164.           
  165.         JLabel lblsql = new JLabel("请在左侧输入你要格式化的SQL语句:");  
  166.         lblsql.setHorizontalAlignment(SwingConstants.LEFT);  
  167.         panel_4.add(lblsql);  
  168.     }  
  169.       
  170.     public void divider(){  
  171.         splitPane.setDividerLocation(0.4);  
  172.     }  
  173. }  



代码中用到了一张图片,仅仅是用来显示图标的,可以使用任意图片代替或者去掉相应代码。

程序下载:

                                      在发布该帖之前上传了一次,结果发现不是免费下载,需要1金币才可以,果断删除重新上传,结果就一直没反应了。

临时放个GOOGLE DRIVE的地址:https://docs.google.com/file/d/0ByAG1xopZV6kU3VfOGxQQU1LZjQ/edit?usp=sharing

CSDN地址彻底没希望了,不知道是不是有什么算法...刚上传的和已删除的一样难道就不行?

增加一个百度网盘地址:http://pan.baidu.com/share/link?shareid=181300461&uk=1325762948

原文地址:https://www.cnblogs.com/firstdream/p/5648308.html