performbuttonselectionlistener.java

来自「一个eclipse插件源代码。用于web开发」· Java 代码 · 共 382 行

JAVA
382
字号
/*
 * $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/sql/PerformButtonSelectionListener.java,v 1.2 2004/12/29 09:45:30 wang_j Exp $
 * $Revision: 1.2 $
 * $Date: 2004/12/29 09:45:30 $
 *
 * ====================================================================
 *
 * The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
 *
 * Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
 *                        IT Forest Corporation
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 */
 
package com.webpump.ui.sql;

import java.sql.*;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;
import org.eclipse.core.resources.IFile;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.FileEditorInput;
import com.webpump.ui.datasource.data.DataSource;
import com.webpump.ui.datasource.data.DataSourceModel;
import com.webpump.ui.datasource.data.SourceList;
import org.eclipse.jface.dialogs.MessageDialog;

import com.webpump.ui.perspective.MacroResource;
import com.webpump.ui.perspective.WebpumpIDEPlugin;

/**
 * Class for listener of PerformButton selection.
 * 
 * @author luo_sa
 * @version 2.0.0 2004-2-24
 */
public class PerformButtonSelectionListener extends SelectionAdapter {
    
    private final static int QUERY  = 1;
    private final static int UPDATE = 2;
    /** object of PerformSection */
    private PerformSection m_objPerformSection;
    public static Shell m_sShell = null;
    
    /**
     * Constructor
     * 
     * @param objPerformSection
     */
    public PerformButtonSelectionListener(PerformSection objPerformSection, Shell shell) {
        m_objPerformSection = objPerformSection;
        m_sShell = shell;
    }
    
    /**
     * Achieve operations when the button be selected
     * 
     */
    public void widgetSelected(SelectionEvent e) {
        
        HashMap hmResultInfo = null;
        Table objTable = m_objPerformSection.getTable();
        
        //clear the table
        objTable.removeAll();
        int index = objTable.getColumnCount()-1;
        for(int i = index ; i >= 0 ; i--) {
            objTable.getColumn(i).dispose();
        }
        
        SQLSection objSqlSection = ((SQLForm) (m_objPerformSection.getFormPage().getForm())).getSqlSection();
        if (objSqlSection.getFullSQL() == null) {
            return ;
        }
        
        String strDBName = m_objPerformSection.getConnectDataBaseCombo().getText();
        IFile m_objFile = ((FileEditorInput) m_objPerformSection.getFormPage().getEditor().getEditorInput()).getFile();
        DataSourceModel objDataSourceModel = WebpumpIDEPlugin.getDataSourceModelToSQL(m_objFile,true);
        Vector vDataSource = ((SourceList)objDataSourceModel.getDataObject()).getvSource();
        
        String[] values = new String[4];
        for (int i = 0; i < vDataSource.size(); i++) {
            
            DataSource objDataSource = (DataSource) vDataSource.get(i);
            
            if (strDBName.compareTo(objDataSource.toString()) == 0) {
                
                String[] propertyvalues = objDataSource.getPropertyInfo();
                values[0] = propertyvalues[1];//driver
				values[1] = propertyvalues[2];//url
				values[2] = propertyvalues[3];//user
                values[3] = propertyvalues[4];//password
                break;
            }
            
        }
        
        try {
            //execute SQL
            hmResultInfo = execute(objSqlSection.getFullSQL(), values);
            
            for(int i = 0; i < hmResultInfo.size(); i++) {
                TableColumn tablecolumn = new TableColumn(objTable, SWT.NONE);
                tablecolumn.setWidth(900/hmResultInfo.size());                  
            }
            
            Set set = hmResultInfo.keySet();
            Object[] keyarray = set.toArray();
            //show result in table
            for(int j = 0; j < keyarray.length; j++) {
                objTable.getColumn(j).setText((String) keyarray[j]);
                
            }
            
            if (keyarray.length > 0) {
                
                String key = (String) keyarray[0];
                String[] strings = (String[]) hmResultInfo.get((Object) key);
                for(int k = 0; k < strings.length; k++) {
                    TableItem tableitem0 = new TableItem(objTable, k);
					
                    String value0 = (String) strings[k];
                    if (value0 == null) value0="";
                    tableitem0.setText(0,value0);
                }                          
            }
            
            for (int i = 1; i < keyarray.length; i++) {
                
                String key = (String) keyarray[i];
                String[] strings = (String[]) hmResultInfo.get((Object) key);                
                for (int k = 0; k < strings.length; k++) {
                    TableItem tableitem0 = objTable.getItem(k);
                    String value1 = (String) strings[k];
					if (value1 == null) value1="";
                    tableitem0.setText(i, value1);
                }
            }
        }
        catch (Exception e1) {
            WebpumpIDEPlugin.log(e1);
        }                              
    }
    
    /**
     * Execute the SQL.
     * 
     * @param strSql the sql to execute
     * @return hmCustomerInfo the result 
     * @throws Exception
     */
    public HashMap execute(String strSql, String[] values)
    throws Exception {
        
        HashMap hmCustomerInfo = null;
        Connection conn = null;
        
        boolean bNeedClose = false;
        boolean bNeedRollBack = false;
        try {   
            String strDBURL         = values[1];
            String strDBUser        = values[2];
            String strDBPassword    = values[3];
                             
            //1、联接??据??    
            //Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            Class.forName(values[0]).newInstance();
            String url=strDBURL;
            //orcl为??据库的SID     
            String user = strDBUser;
            String password = strDBPassword;
            conn= DriverManager.getConnection(url,user,password);
            bNeedClose = true;
            
            //开始事??
            conn.setAutoCommit(true);    
            bNeedRollBack = true;
                        
            Statement stmt = conn.createStatement();                   
            hmCustomerInfo = getDataFromDB(strSql,stmt);    
			stmt.close();                                
        }
        catch(Exception e) { 
			WebpumpIDEPlugin.log(e);
            MessageDialog.openError(m_sShell ,
                WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_NOTE),
                WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_FAIL)+"\n" +e.toString());            
        }
        finally {
            if (bNeedRollBack) {
                try {
                    conn.rollback();
                }
                catch(Exception e1) {
                    WebpumpIDEPlugin.log(e1);
                }
            }            
            if (bNeedClose) {
                try {
                    conn.close();
                }
                catch(Exception e1) {
                    WebpumpIDEPlugin.log(e1);
                }
            }
            return hmCustomerInfo;         
        }       
    }
    
    /**
     * Execute SQL and return result in HashMap
     * @param strSql sql to execute
     * @param stmt
     * @return hmReturn the result
     * @throws Exception
     */
    public static HashMap getDataFromDB(String strSql, Statement stmt)
    throws Exception {     
        HashMap hmReturn = new HashMap();
		int RecordNum = 0;
		ResultSetMetaData rm = null;
		String[] strColumnArray = null;           
		Vector[] vValueArray = null;                 
		int flag = 0;   
        try {              
            
            stmt.execute(strSql);
            
			while(true){
				ResultSet rs = stmt.getResultSet();
            	
				if(rs != null){
            		
					rm = rs.getMetaData();                        
					RecordNum = rm.getColumnCount();
	            
					strColumnArray = new String[RecordNum];
	            
					vValueArray = new Vector[RecordNum];
				
					//遍历结果集放??Vector                                   
					for (int i = 1; i <= RecordNum; i++) {
						strColumnArray[i-1] = rm.getColumnName(i);
						vValueArray[i-1] = new Vector();
					}
	                                    
					while (rs.next()) {            
						for (int j = 0; j < RecordNum; j++) {
							vValueArray[j].addElement(rs.getString(strColumnArray[j]));
						}
					}   
				                        
					//将结果Vector放??HashMap
					for (int i=0;i<RecordNum;i++) {            
						hmReturn.put(strColumnArray[i],vectorToStringArray(vValueArray[i]));               
					}
					rs.close();
					return hmReturn;
					//stmt.getMoreResults();
					//continue;
				}
            	
				int rowCount = stmt.getUpdateCount();
				if(rowCount >= 0){
					MessageDialog.openInformation(m_sShell,
								WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_NOTE),
								WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_SUCCESS));
					return hmReturn;
					//stmt.getMoreResults();
					//continue;
				}
				break;
			}
			
			
			/*                       
						//下传柄??据信息
						if(strSql.toLowerCase().startsWith("select")){
							flag = QUERY;
						}else{
							flag = UPDATE;
						}
			

						if(flag == QUERY) {
							ResultSet rs = stmt.executeQuery(strSql);
				
							//if (rs == null)
							rm = rs.getMetaData();                        
							RecordNum = rm.getColumnCount();
	            
							strColumnArray = new String[RecordNum];
	            
							vValueArray = new Vector[RecordNum];
				
							//遍历结果集放??Vector                                   
							for (int i = 1; i <= RecordNum; i++) {
								strColumnArray[i-1] = rm.getColumnName(i);
								vValueArray[i-1] = new Vector();
							}
	                                    
							while (rs.next()) {            
								for (int j = 0; j < RecordNum; j++) {
									vValueArray[j].addElement(rs.getString(strColumnArray[j]));
								}
							}   
				                        
							//将结果Vector放??HashMap
							for (int i=0;i<RecordNum;i++) {            
								hmReturn.put(strColumnArray[i],vectorToStringArray(vValueArray[i]));               
							}
							if(rs!=null){
								rs.close();
								return hmReturn;                          
							}
				
						}
						else if(flag == UPDATE)
						{	
							int rowcount = stmt.executeUpdate(strSql); 
							bSucceed = true;    
							if(rowcount >= 0){
								MessageDialog.openInformation(m_sShell,
											WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_NOTE),
											WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_SUCCESS));
								return hmReturn;
							}

						}       
			*/ 			
		} 
		catch(SQLException e) { 
			WebpumpIDEPlugin.log(e);
			MessageDialog.openError(m_sShell ,
						WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_NOTE),
						WebpumpIDEPlugin.getResourceString(MacroResource.PERFORMBUTTONSELECTIONLISTENER_FAIL)+"\n" +e.toString());
		}
		finally {
			return hmReturn;
		}           
    }    
    

    /**
     * Transfer Vector to String Array and return .
     * 
     * @param vDataArray    Vector
     *
     * @return String[]     String Array
     */
    public  static String[] vectorToStringArray(Vector vDataArray ) {
        
        String[] strTempArray = null;

        try {
            if (vDataArray != null ){
                strTempArray = new String[vDataArray.size()];
                vDataArray.copyInto(strTempArray);
            }
        }
        catch( Exception e) {
            WebpumpIDEPlugin.log(e);
            strTempArray = null;
        }
        return strTempArray;
    }   

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?