Windchill中使用JDBC查询

阅读更多
需要注意,wtconnection不能关闭
package ext.leanore.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import wt.auth.SimpleAuthenticator;
import wt.method.MethodContext;
import wt.method.MethodServerException;
import wt.method.RemoteMethodServer;
import wt.pom.DataServicesRegistry;
import wt.pom.UnsupportedPDSException;
import wt.pom.WTConnection;

/**
 * @author leanore
 */
public class DBOperator {

	/**
	 * @param sql
	 * @param parameters
	 * @return
	 */
	public static Vector doQuery(String sql, String[] parameters) {
		Vector vec = new Vector();
		WTConnection wtconnection = null;
		PreparedStatement pstmt = null;
		ResultSet result;

		try {
			MethodContext methodcontext = getMethodContext();
			wtconnection = (WTConnection) methodcontext.getConnection();
			pstmt = wtconnection.prepareStatement(sql);
			if (parameters != null) {
				for (int j = 1; j <= parameters.length; j++) {
					 pstmt.setString(j, parameters[j - 1]);
				}
			}
			result = pstmt.executeQuery();
			vec = getResult(result);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return vec;

	}

	private static Vector getResult(ResultSet result) {
		//对查询结果进行封装
		return new Vector();
	}

	/**
	 ***************************************
	 * ClassName:DBOperator
	 * @TODO: 

* * @author leanore * @since leanore -Ver 1.1 * @Date 2011-7-1 * @return * @throws UnsupportedPDSException * @throws UnknownHostException *************************************** */ public static MethodContext getMethodContext() throws UnsupportedPDSException, UnknownHostException { MethodContext methodcontext; try { methodcontext = MethodContext.getContext(); } catch (MethodServerException methodserverexception) { RemoteMethodServer.ServerFlag = true; InetAddress inetaddress = InetAddress.getLocalHost(); String s = inetaddress.getHostName(); if (s == null) s = inetaddress.getHostAddress(); SimpleAuthenticator simpleauthenticator = new SimpleAuthenticator(); methodcontext = new MethodContext(s, simpleauthenticator); methodcontext.setThread(Thread.currentThread()); wt.pds.PDSIfc pdsifc = DataServicesRegistry.getDefault().getPdsFor("DEFAULT"); } return methodcontext; } }

你可能感兴趣的:(jdbc,windchill,pdm)