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<Object>();
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<Object>();
}

/**
***************************************
* ClassName:DBOperator
* @TODO: <P></P>
*
* @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;
}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值