SQL轉Java代碼小工具


工作中使用SQL的時候很多,當使用hibernate的時候,經常遇到多行的SQL,通常在PL/SQL或其他地方寫好SQL,測試沒問題后,需要將SQL寫到程序代碼中,多行SQL需要拼接字符串,手動一行行添加很不方便,所以,既然經常會遇到,就寫個小工具來自動處理吧。

 

該工具使用Java進行開發,我上傳的程序已經打包成exe了(運行仍然需要系統有jre),源代碼會在這里全部貼出,因為只有一個類。

 

 

 

先看兩個實際運行圖:

 

1.生成String類型,這個類型在大部分的編程語言中通用。

 

2.StringBuffer類型,這種類型適用於JAVA,從性能來看,這兩種類型在執行多次時,StringBuffer效率更高。

 

 

代碼很簡單,就是兩個textarea,通過\n分割字符串,然后一行行拼接。

 

代碼如下:

 

 

[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("\n");  
  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]+" \"\n");  
  114.                         }  
  115.                         else {  
  116.                             result.append(" +\" "+sqls[i]+" \"\n");  
  117.                         }  
  118.                     }  
  119.                     result.append(" +\" "+sqls[sqls.length-1]+" \";\n");  
  120.                 }  
  121.                 else{  
  122.                     //string形式  
  123.                     for(int i=0;i<sqls.length;i++){  
  124.                         result.append(valibleName+".append(\" "+sqls[i]+" \");\n");  
  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM