比如我們要生成一個生成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;
public class SqlNewWizard extends Wizard implements INewWizard {
private SqlNewWizardPage page;
private ISelection selection;
public SqlNewWizard() {
super();
setNeedsProgressMonitor(true);
}
public void addPages() {
page = new SqlNewWizardPage(selection);
this.setWindowTitle("sql幫組類生成工具 作者:小瓊瓊");
addPage(page);
}
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);
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;
}
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);
}
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;
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;
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();
}
}
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();
}
}