eclipse开发一个文件向导

比如我们要生成一个生成jdbc帮组类的插件

 

此时我们就要用到文件生成向导

 

打开eclipse 或者myeclipse  选中任意位置 new puginproject

输入工程名称 其他默认

选择向导里面的一项  custom plugin in wizard 右边的框中

进入下一步 里面有文件导入导出向导 xml编辑器向导 我们选择

NeW File Wizard

利用jigloo 插件生成一个composite的页面 jigloo可以使用拖拽可视化设计swt界面

 

拓展点

 

   <extension
         point="org.eclipse.ui.newWizards">
      <category
            name="LH语言互相转换工具"
            id="lhplugin">
      </category>
       <wizard
            name="LH Jdbc帮助类"
            icon="icons/sample.gif"
            category="lhplugin"
            class="lhplugin.wizards.SqlNewWizard"
            id="lhplugin.wizards.SqlNewWizard">
      </wizard>
   </extension>

 

<category
            name="LH语言互相转换工具"
            id="lhplugin">
      </category>
表示会在 eclipse --new ---others  会弹出一个选择生成内容的列表

表示会添加一个文件夹分类  名字是 LH语言互相转换工具

<wizard
            name="LH Jdbc帮助类"
            icon="icons/sample.gif"
            category="lhplugin"
            class="lhplugin.wizards.SqlNewWizard"
            id="lhplugin.wizards.SqlNewWizard">
      </wizard>
表示添加一个向导的控制类 类是lhplugin.wizards.SqlNewWizard 添加到lhplugin 这个文件夹分类下面

SqlNewWizard 负责添加向导页到里面 该类必须extends Wizard implements INewWizard

 

package lhplugin.wizards;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import lhplugin.utils.ConnectEntry;
import lhplugin.utils.JdbcGen;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.core.ResolvedSourceMethod;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.PartInitException;

/**
 * This is a sample new wizard. Its role is to create a new file resource in the
 * provided container. If the container resource (a folder or a project) is
 * selected in the workspace when the wizard is opened, it will accept it as the
 * target container. The wizard creates one file with the extension "mpe". If a
 * sample multi-page editor (also available as a template) is registered for the
 * same extension, it will be able to open it.
 */

public class SqlNewWizard extends Wizard implements INewWizard {
 private SqlNewWizardPage page;
 private ISelection selection;

 /**
  * Constructor for SampleNewWizard.
  */
 public SqlNewWizard() {
  super();
  setNeedsProgressMonitor(true);
 }

 /**
  * Adding the page to the wizard.
  */

 public void addPages() {
  page = new SqlNewWizardPage(selection);
  this.setWindowTitle("sql帮组类生成工具 作者:小琼琼");
  addPage(page);
 }

 /**
  * This method is called when 'Finish' button is pressed in the wizard. We
  * will create an operation and run it using wizard as execution context.
  */
 public boolean performFinish() {
  final IPackageFragment frg = page.pac;
  final String url = page.getTxtUrl().getText();
  final String pac = page.pac.getElementName();
  final String cls = "JdbcHelper";
  final String clsName = page.getText1().getText();
  final String uid = page.getText2().getText();
  final String pwd = page.getText3().getText();
  final ConnectEntry entry = new ConnectEntry();
  entry.setCls(cls);
  entry.setClsName(clsName);
  entry.setPac(pac);
  entry.setPwd(pwd);
  entry.setUid(uid);
  entry.setUrl(url);
  IRunnableWithProgress prs = new IRunnableWithProgress() {

   public void run(IProgressMonitor m)
     throws InvocationTargetException, InterruptedException {
    try {
     m.beginTask("开始提取模板需要的资源", 2);
     JdbcGen gen = new JdbcGen();
     String content = gen.generate(entry);
     m.worked(1);
     m.setTaskName("生成类文件");
     try {
      // 可以使用这种方法创建 下面注视为foler下创建文件
      final ICompilationUnit u = frg.createCompilationUnit(
        cls + ".java", content, false, m);
      /**
       * 如果是获取到 IFolder对象如何创建一个文件了代码如下 IWorkspaceRoot
       * myWorkspaceRoot =
       * ResourcesPlugin.getWorkspace().getRoot(); IProject
       * myProject = myWorkspaceRoot.getProject("MyProject");
       * if (myProject.exists() && !myProject.isOpen())
       * myProject.open(null); IFolder imagesFolder =
       * myProject.getFolder("images"); if
       * (imagesFolder.exists()) { IFile newLogo =
       * imagesFolder.getFile("newLogo.png"); FileInputStream
       * fileStream = new FileInputStream(
       * "c:/MyOtherData/newLogo.png");
       * newLogo.create(fileStream, false, null); }
       */
      getShell().getDisplay().asyncExec(new Runnable() {

       public void run() {
        // TODO Auto-generated method stub
        try {
         // 获取方法
         IMember member = new ResolvedSourceMethod(
           (JavaElement) u, "queryList",
           new String[] {
             String.class.getName(),
             List.class.getName(),
             int.class.getName() }, null);
         IEditorPart part = JavaUI.openInEditor(u);
         // 定位到该方法 让别人去看我的
         JavaUI.revealInEditor(part,
           (IJavaElement) member);
        } catch (PartInitException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        } catch (JavaModelException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }

      });

     } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     m.worked(1);
    } finally {
     m.done();
    }

   }

  };
  try {
   getContainer().run(true, false, prs);
  } catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return true;
 }

 /**
  * We will initialize file contents with a sample text.
  */

 private InputStream openContentStream() {
  String contents = "This is the initial file contents for *.mpe file that should be word-sorted in the Preview page of the multi-page editor";
  return new ByteArrayInputStream(contents.getBytes());
 }

 private void throwCoreException(String message) throws CoreException {
  IStatus status = new Status(IStatus.ERROR, "lhplugin", IStatus.OK,
    message, null);
  throw new CoreException(status);
 }

 /**
  * We will accept the selection in the workbench to see if we can initialize
  * from it.
  *
  * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
  */
 public void init(IWorkbench workbench, IStructuredSelection selection) {
  this.selection = selection;
 }
}

 以下表示添加页面  添加的页面 比如SqlNewWizardPage  必须extends WizardPage

 public void addPages() {
  page = new SqlNewWizardPage(selection);
  this.setWindowTitle("sql帮组类生成工具 作者:小琼琼");
  addPage(page);
 }

 下面是向导页面

package lhplugin.wizards;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import lhplugin.utils.SampleUtils;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

public class SqlNewWizardPage extends WizardPage {

 private ISelection selection;
 private Group group1;
 private Button button2;
 private Button button1;
 private Label label5;
 private Label label4;
 private Text text3;
 private Text text2;
 private Label label3;
 private Text text1;
 private Combo cboDb;
 private Label label2;
 private Text txtUrl;
 private Label label1;
 /**
  * Constructor for SampleNewWizardPage.
  *
  * @param pageName
  */
 public SqlNewWizardPage(ISelection selection) {
  super("sql帮组类生成工具向导页面");
  setTitle(" LH-SQL帮组类生成工具");
  setDescription("/n LH产品 提供商 提供 此工具 ,此 插件 那是 相当的nb");
  this.selection = selection;
  
 }

 public void addSpaceGraid(Composite container, int length) {
  for (int i = 0; i < length; i++) {
   Label temp = new Label(container, SWT.NULL);
  }
 }
 private void initGUI(Composite parent) {
  try {
   final Composite container=new Composite(parent,SWT.NULL);
   GridLayout thisLayout = new GridLayout();
   thisLayout.makeColumnsEqualWidth = true;
   container.setLayout(thisLayout);
   container.setSize(481, 234);
   {
    group1 = new Group(container, SWT.NONE);
    FormLayout group1Layout = new FormLayout();
    group1.setLayout(group1Layout);
    GridData group1LData = new GridData();
    group1LData.widthHint = 452;
    group1LData.heightHint = 193;
    group1.setLayoutData(group1LData);
    group1.setText("jdbc/u6a21/u677f/u5c5e/u6027");
    {
     button1 = new Button(group1, SWT.PUSH | SWT.CENTER);
     FormData button1LData = new FormData();
     button1LData.left =  new FormAttachment(0, 1000, 331);
     button1LData.top =  new FormAttachment(0, 1000, 153);
     button1LData.width = 60;
     button1LData.height = 22;
     button1.setLayoutData(button1LData);
     button1.setText("/u6d4b/u8bd5/u8fde/u63a5");
     button1.addSelectionListener(new SelectionAdapter() {

      @Override
      public void widgetSelected(SelectionEvent e) {
       // TODO Auto-generated method stub
       MessageDialog.openInformation(container.getShell(), "警告消息", "改功能暂未实现");
      }
      
      
      
     });
    }
    {
     FormData cboDbLData = new FormData();
     cboDbLData.left =  new FormAttachment(0, 1000, 110);
     cboDbLData.top =  new FormAttachment(0, 1000, 15);
     cboDbLData.width = 266;
     cboDbLData.height = 20;
     cboDb = new Combo(group1, SWT.NONE);
     cboDb.setLayoutData(cboDbLData);
     cboDb.setItems(new java.lang.String[] {"mysql","oracle","sqlserver","db2","sybase","odbc","postgresql","informix"});
     cboDb.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent evt) {
       Combo cbo=(Combo)evt.getSource();
       if("mysql".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:mysql://[localhost]:3306/dbname");
        text1.setText("com.mysql.jdbc.Driver");
       }
       if("oracle".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:oracle:thin:@localhost:1521:dbname");
        text1.setText("oracle.jdbc.driver.OracleDriver");
       }
       if("sqlserver".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:microsoft:sqlserver://localhost:1433;databaseName=dbname");
        text1.setText("com.microsoft.jdbc.sqlserver.SQLServerDriver");
       }
       if("db2".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:db2://192.9.200.108:6789/dbname");
        text1.setText("com.ibm.db2.jdbc.net.DB2Driver");
       }
       if("sybase".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:sybase:Tds:localhost:2638");
        text1.setText("com.sybase.jdbc2.jdbc.SybDriver");
       }
       if("informix".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:informix-sqli://localhost:1533");
        text1.setText("com.informix.jdbc.IfxDriver");
       }
       if("postgresql".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:postgresql://localhost:port/dbname");
        text1.setText("org.postgresql.Driver");
       }
       if("odbc".equals(cbo.getText()))
       {
        txtUrl.setText("jdbc:odbc:dbname");
        text1.setText("sun.jdbc.odbc.JdbcOdbcDriver");
       }
       //TODO add your code for cboDb.modifyTextPostgresql
       text2.setText("dba");
       text3.setText("sql");
      }
     });
    }
    {
     label2 = new Label(group1, SWT.NONE);
     FormData label2LData = new FormData();
     label2LData.left =  new FormAttachment(0, 1000, 38);
     label2LData.top =  new FormAttachment(0, 1000, 20);
     label2LData.width = 60;
     label2LData.height = 12;
     label2.setLayoutData(label2LData);
     label2.setText("/u6570/u636e/u5e93/u9009/u62e9");
    }
    {
     FormData txtUrlLData = new FormData();
     txtUrlLData.left =  new FormAttachment(0, 1000, 110);
     txtUrlLData.top =  new FormAttachment(0, 1000, 44);
     txtUrlLData.width = 283;
     txtUrlLData.height = 12;
     txtUrl = new Text(group1, SWT.BORDER|SWT.SINGLE);
     txtUrl.setLayoutData(txtUrlLData);
     txtUrl.setOrientation(SWT.HORIZONTAL);
    }
    {
     label1 = new Label(group1, SWT.NONE);
     FormData label1LData = new FormData();
     label1LData.width = 52;
     label1LData.height = 22;
     label1LData.left =  new FormAttachment(89, 1000, 0);
     label1LData.right =  new FormAttachment(204, 1000, 0);
     label1LData.top =  new FormAttachment(230, 1000, 0);
     label1LData.bottom =  new FormAttachment(344, 1000, 0);
     label1.setLayoutData(label1LData);
     label1.setText("/u8fde/u63a5url");
    }
    {
     text1 = new Text(group1, SWT.SINGLE | SWT.BORDER);
     FormData text1LData = new FormData();
     text1LData.left =  new FormAttachment(0, 1000, 110);
     text1LData.top =  new FormAttachment(0, 1000, 71);
     text1LData.width = 283;
     text1LData.height = 12;
     text1.setLayoutData(text1LData);
     text1.setOrientation(SWT.HORIZONTAL);
    }
    {
     label3 = new Label(group1, SWT.NONE);
     FormData label3LData = new FormData();
     label3LData.left =  new FormAttachment(0, 1000, 42);
     label3LData.top =  new FormAttachment(0, 1000, 71);
     label3LData.width = 50;
     label3LData.height = 18;
     label3.setLayoutData(label3LData);
     label3.setText("/u8fde/u63a5/u7c7b");
    }
    {
     text2 = new Text(group1, SWT.SINGLE | SWT.BORDER);
     FormData text2LData = new FormData();
     text2LData.left =  new FormAttachment(0, 1000, 110);
     text2LData.top =  new FormAttachment(0, 1000, 97);
     text2LData.width = 283;
     text2LData.height = 12;
     text2.setLayoutData(text2LData);
     text2.setOrientation(SWT.HORIZONTAL);
    }
    {
     text3 = new Text(group1, SWT.SINGLE | SWT.BORDER);
     FormData text3LData = new FormData();
     text3LData.left =  new FormAttachment(0, 1000, 110);
     text3LData.top =  new FormAttachment(0, 1000, 123);
     text3LData.width = 283;
     text3LData.height = 12;
     text3.setLayoutData(text3LData);
     text3.setOrientation(SWT.HORIZONTAL);
    }
    {
     label4 = new Label(group1, SWT.NONE);
     FormData label4LData = new FormData();
     label4LData.left =  new FormAttachment(0, 1000, 42);
     label4LData.top =  new FormAttachment(0, 1000, 97);
     label4LData.width = 50;
     label4LData.height = 18;
     label4.setLayoutData(label4LData);
     label4.setText("/u7528/u6237/u540d");
    }
    {
     label5 = new Label(group1, SWT.NONE);
     FormData label5LData = new FormData();
     label5LData.left =  new FormAttachment(0, 1000, 48);
     label5LData.top =  new FormAttachment(0, 1000, 123);
     label5LData.width = 50;
     label5LData.height = 18;
     label5.setLayoutData(label5LData);
     label5.setText("/u5bc6/u7801");
    }
   }
   this.setControl(container);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * @see IDialogPage#createControl(Composite)
  */
 public void createControl(Composite parent) {
  initGUI(parent);
  initialize();
 }
 public IPackageFragment pac=null;
 private void initialize() {
  if (selection != null && selection.isEmpty() == false
    && selection instanceof IStructuredSelection) {
   IStructuredSelection ssel = (IStructuredSelection) selection;
   if (ssel.size() > 1)
    return;
   Object obj = ssel.getFirstElement();
   if (obj instanceof IPackageFragment) {
    pac=(IPackageFragment)obj;
     
    
    // containerText.setText(container.getFullPath().toString());
   }else
   {
    updateStatus("请选择包文件");
   }
  }

 }

 private void updateStatus(String message) {
  setErrorMessage(message);
  setPageComplete(message == null);
 }
 public String getContainerName() {
  return "q";
 }

 public ISelection getSelection() {
  return selection;
 }

 public void setSelection(ISelection selection) {
  this.selection = selection;
 }

 public Group getGroup1() {
  return group1;
 }

 public void setGroup1(Group group1) {
  this.group1 = group1;
 }

 public Button getButton2() {
  return button2;
 }

 public void setButton2(Button button2) {
  this.button2 = button2;
 }

 public Button getButton1() {
  return button1;
 }

 public void setButton1(Button button1) {
  this.button1 = button1;
 }

 public Label getLabel5() {
  return label5;
 }

 public void setLabel5(Label label5) {
  this.label5 = label5;
 }

 public Label getLabel4() {
  return label4;
 }

 public void setLabel4(Label label4) {
  this.label4 = label4;
 }

 public Text getText3() {
  return text3;
 }

 public void setText3(Text text3) {
  this.text3 = text3;
 }

 public Text getText2() {
  return text2;
 }

 public void setText2(Text text2) {
  this.text2 = text2;
 }

 public Label getLabel3() {
  return label3;
 }

 public void setLabel3(Label label3) {
  this.label3 = label3;
 }

 public Text getText1() {
  return text1;
 }

 public void setText1(Text text1) {
  this.text1 = text1;
 }

 public Combo getCboDb() {
  return cboDb;
 }

 public void setCboDb(Combo cboDb) {
  this.cboDb = cboDb;
 }

 public Label getLabel2() {
  return label2;
 }

 public void setLabel2(Label label2) {
  this.label2 = label2;
 }

 public Text getTxtUrl() {
  return txtUrl;
 }

 public void setTxtUrl(Text txtUrl) {
  this.txtUrl = txtUrl;
 }

 public Label getLabel1() {
  return label1;
 }

 public void setLabel1(Label label1) {
  this.label1 = label1;
 }

 public IPackageFragment getPac() {
  return pac;
 }

 public void setPac(IPackageFragment pac) {
  this.pac = pac;
 }
}

 

initGUI方法表示初始 向导页面 这个页面可以从 jigloo插件生成的页面拷贝过来用

用于生成代码的帮族类

JdbcGen

 

package lhplugin.utils;

import java.util.*;

public class JdbcGen
{
  protected static String nl;
  public static synchronized JdbcGen create(String lineSeparator)
  {
    nl = lineSeparator;
    JdbcGen result = new JdbcGen();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = NL + "package ";
  protected final String TEXT_2 = ";" + NL + "" + NL + "import java.sql.*;" + NL + "import java.util.*;" + NL + "" + NL + "public class ";
  protected final String TEXT_3 = "{" + NL + "/tprivate static String url=/"";
  protected final String TEXT_4 = "/";" + NL + "/tprivate static String clsName=/"";
  protected final String TEXT_5 = "/";" + NL + "/tprivate static String uid=/"";
  protected final String TEXT_6 = "/";" + NL + "/tprivate static String pwd=/"";
  protected final String TEXT_7 = "/";" + NL + "/tpublic static synchronized Connection getConnection() throws SQLException {" + NL + "/t/tConnection conn = null;" + NL + "" + NL + "/t/ttry {" + NL + "" + NL + "/t/t/tClass.forName(clsName);" + NL + "/t/t/tconn=DriverManager.getConnection(url, uid, pwd);" + NL + "/t/t} catch (Exception e) {" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t}" + NL + "/t/treturn conn;" + NL + "/t}" + NL + "/tpublic static void closeConnection(Connection con) {" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tif(con!=null)" + NL + "/t/t/tcon.close();" + NL + "/t/t} catch (SQLException e) {" + NL + "/t/t/t// TODO Auto-generated catch block" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t}" + NL + "" + NL + "/t}" + NL + "/tpublic static boolean execute(String sql) {" + NL + "/t/tboolean bool = false;" + NL + "/t/tConnection conn = null;" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "/t/t/tif (null != conn) {" + NL + "/t/t/t/tStatement sm = conn.createStatement();" + NL + "/t/t/t/tbool = sm.execute(sql);" + NL + "/t/t/t}" + NL + "/t/t} catch (SQLException e) {" + NL + "" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn bool;" + NL + "/t}" + NL + "/tpublic static boolean execute(Connection con, String sql) {" + NL + "/t/tboolean bool = false;" + NL + "/t/tConnection conn = con;" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tif (null != conn) {" + NL + "/t/t/t/tStatement sm = conn.createStatement();" + NL + "/t/t/t/tbool = sm.execute(sql);" + NL + "/t/t/t}" + NL + "/t/t} catch (SQLException e) {" + NL + "" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn bool;" + NL + "/t}" + NL + "/t" + NL + "/tpublic static boolean execute(String sql, Object[] param) {" + NL + "/t/tboolean bool = false;" + NL + "/t/tConnection conn = null;" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "/t/t/tif (null != conn) {" + NL + "/t/t/t/tPreparedStatement sm = conn.prepareStatement(sql);" + NL + "/t/t/t/tfor (int i = 0; i < param.length; i++) {" + NL + "/t/t/t/t/tsm.setObject(i + 1, param[i]);" + NL + "/t/t/t/t}" + NL + "/t/t/t/tbool = sm.execute();" + NL + "/t/t/t}" + NL + "/t/t} catch (SQLException e) {" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "" + NL + "/t/treturn bool;" + NL + "/t}" + NL + "/tpublic static boolean[] execute(String[] sqlList) {" + NL + "/t/tboolean[] bool = new boolean[sqlList.length];" + NL + "/t/tint i = 0;" + NL + "/t/tfor (String sql : sqlList) {" + NL + "/t/t/tbool[i++] = execute(sql);" + NL + "/t/t}" + NL + "" + NL + "/t/treturn bool;" + NL + "/t}" + NL + "/tpublic static Object getUniqueValue(String sql, Object[] param) {" + NL + "/t/tConnection conn = null;" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "/t/t/tif (null != conn) {" + NL + "/t/t/t/tPreparedStatement sm = conn.prepareStatement(sql);" + NL + "/t/t/t/tif (param != null) {" + NL + "/t/t/t/t/tfor (int i = 0; i < param.length; i++) {" + NL + "/t/t/t/t/t/tsm.setObject(i + 1, param[i]);" + NL + "/t/t/t/t/t}" + NL + "/t/t/t/t}" + NL + "/t/t/t/tResultSet set = sm.executeQuery();" + NL + "/t/t/t/tset.next();" + NL + "/t/t/t/treturn set.getObject(1);" + NL + "" + NL + "/t/t/t}" + NL + "/t/t} catch (SQLException e) {" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn null;" + NL + "/t}" + NL + "/tpublic static Object getUniqueValue(String sql, List param) {" + NL + "/t/tConnection conn = null;" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "/t/t/tif (null != conn) {" + NL + "/t/t/t/tPreparedStatement sm = conn.prepareStatement(sql);" + NL + "/t/t/t/tif (param != null) {" + NL + "/t/t/t/t/tfor (int i = 0; i < param.size(); i++) {" + NL + "/t/t/t/t/t/tsm.setObject(i + 1, param.get(i));" + NL + "/t/t/t/t/t}" + NL + "/t/t/t/t}" + NL + "/t/t/t/tResultSet set = sm.executeQuery();" + NL + "/t/t/t/tset.next();" + NL + "/t/t/t/treturn set.getObject(1);" + NL + "" + NL + "/t/t/t}" + NL + "/t/t} catch (SQLException e) {" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn null;" + NL + "/t}" + NL + "/tpublic static int getCount(String sql) {" + NL + "/t/tint count = 0;" + NL + "/t/tString[][] list = null;" + NL + "/t/tlist = queryArray(sql, 1);" + NL + "/t/tif (null != list) {" + NL + "/t/t/tcount = Integer.parseInt(list[1][0]);" + NL + "/t/t}" + NL + "/t/treturn count;" + NL + "/t}" + NL + "/t//该方法必须添加jstl支持" + NL + "/tpublic static SortedMap[] queryList(String sql, Object[] param) {" + NL + "/t/tConnection conn = null;" + NL + "/t/tPreparedStatement ps = null;" + NL + "/t/tResultSet rs = null;" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "" + NL + "/t/t/tps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE," + NL + "/t/t/t/t/tResultSet.CONCUR_READ_ONLY);" + NL + "/t/t/tif (param != null) {" + NL + "/t/t/t/tfor (int i = 0; i < param.length; i++) {" + NL + "/t/t/t/t/tps.setObject(i + 1, param[i]);" + NL + "/t/t/t/t}" + NL + "/t/t/t}" + NL + "/t/t/trs = ps.executeQuery();" + NL + "/t/t/tResult result = ResultSupport.toResult(rs);" + NL + "/t/t/treturn result.getRows();" + NL + "" + NL + "/t/t} catch (SQLException e) {" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn null;" + NL + "/t}" + NL + "/tpublic static SortedMap[] queryList(String sql, List param) {" + NL + "/t/tConnection conn = null;" + NL + "/t/tPreparedStatement ps = null;" + NL + "/t/tResultSet rs = null;" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "" + NL + "/t/t/tps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE," + NL + "/t/t/t/t/tResultSet.CONCUR_READ_ONLY);" + NL + "/t/t/tif (param != null) {" + NL + "/t/t/t/tfor (int i = 0; i < param.size(); i++) {" + NL + "/t/t/t/t/tps.setObject(i + 1, param.get(i));" + NL + "/t/t/t/t}" + NL + "/t/t/t}" + NL + "/t/t/trs = ps.executeQuery();" + NL + "/t/t/tResult result = ResultSupport.toResult(rs);" + NL + "/t/t/treturn result.getRows();" + NL + "" + NL + "/t/t} catch (SQLException e) {" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn null;" + NL + "/t}" + NL + "/t//取0开始的count条记录" + NL + "/tpublic static SortedMap[] queryList(String sql, List param, int count) {" + NL + "/t/treturn queryList(sql,param,0,count);" + NL + "/t}" + NL + "/t//取指定location位置开始 count的数量的记录集合" + NL + "/tpublic static SortedMap[] queryList(String sql, List param, int location," + NL + "/t/t/tint count) {" + NL + "/t/tConnection conn = null;" + NL + "/t/tPreparedStatement ps = null;" + NL + "/t/tResultSet rs = null;" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "" + NL + "/t/t/tps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE," + NL + "/t/t/t/t/tResultSet.CONCUR_READ_ONLY);" + NL + "/t/t/tif (param != null) {" + NL + "/t/t/t/tfor (int i = 0; i < param.size(); i++) {" + NL + "/t/t/t/t/tps.setObject(i + 1, param.get(i));" + NL + "/t/t/t/t}" + NL + "/t/t/t}" + NL + "/t/t/trs = ps.executeQuery();" + NL + "/t/t/tResult result = null;" + NL + "/t/t/tif (location != -1)" + NL + "/t/t/t/tresult = new ResultImpl(rs, location, count);" + NL + "/t/t/telse" + NL + "/t/t/t/tresult = ResultSupport.toResult(rs);" + NL + "/t/t/treturn result.getRows();" + NL + "" + NL + "/t/t} catch (SQLException e) {" + NL + "/t/t/te.printStackTrace();" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn null;" + NL + "/t}" + NL + "/tpublic static String[][] queryArray(String sql, int limit) {" + NL + "/t/tlimit += 1;" + NL + "/t/tString[][] list = null;" + NL + "/t/tConnection conn = null;" + NL + "/t/tPreparedStatement ps = null;" + NL + "/t/tResultSet rs = null;" + NL + "" + NL + "/t/ttry {" + NL + "/t/t/tconn = getConnection();" + NL + "/t/t/tif (null == conn) {" + NL + "/t/t/t/treturn list;" + NL + "/t/t/t}" + NL + "/t/t/tps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE," + NL + "/t/t/t/t/tResultSet.CONCUR_READ_ONLY);" + NL + "/t/t/trs = ps.executeQuery();" + NL + "" + NL + "/t/t/tif (rs == null) {" + NL + "/t/t/t/treturn list;" + NL + "/t/t/t}" + NL + "/t/t/tResultSetMetaData meta = rs.getMetaData();" + NL + "/t/t/tint columnNumber = meta.getColumnCount();" + NL + "/t/t/tlist = new String[limit][];" + NL + "/t/t/tlist[0] = new String[columnNumber];" + NL + "/t/t/tfor (int i = 0; i < columnNumber; i++) {" + NL + "/t/t/t/tlist[0][i] = meta.getColumnName(i + 1);" + NL + "/t/t/t}" + NL + "/t/t/tint x = 0;" + NL + "/t/t/twhile (rs.next()) {" + NL + "/t/t/t/tx++;" + NL + "/t/t/t/tif (x == limit)" + NL + "/t/t/t/t/tbreak;" + NL + "/t/t/t/tlist[x] = new String[columnNumber];" + NL + "/t/t/t/tfor (int i = 0; i < columnNumber; i++) {" + NL + "/t/t/t/t/tlist[x][i] = rs.getString(i + 1);" + NL + "/t/t/t/t}" + NL + "/t/t/t}" + NL + "" + NL + "/t/t} catch (SQLException e) {" + NL + "" + NL + "/t/t} finally {" + NL + "/t/t/tcloseConnection(conn);" + NL + "/t/t}" + NL + "/t/treturn list;" + NL + "/t}" + NL + "}" + NL + NL;
  protected final String TEXT_8 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
   
   ConnectEntry utils=(ConnectEntry)argument;
   String clsName=utils.getClsName();
   String url=utils.getUrl();
   String uid=utils.getUid();
   String pwd=utils.getPwd();
   String pac=utils.getPac();
   String cls=utils.getCls();


    stringBuffer.append(TEXT_1);
    stringBuffer.append(pac);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(cls);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(url);
    stringBuffer.append(TEXT_4);
    stringBuffer.append(clsName);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(uid);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(pwd);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(TEXT_8);
    return stringBuffer.toString();
  }
}

 

 

你可能感兴趣的:(eclipse,sql,String,null,button,wizard)