本文环境:
Win2003 + myeclipse6.01 + sqlserver2000(sp4)
1、 安装glassfish——启动,在管理控制台下配置如下
首先配置连接池——大家把sqlserver的驱动包放在glassfish安装目录下的lib里。
接着是配置JNDI(同时选中刚才配置好的连接池)
2、测试
常规方法测试1:在servlet中测试代码如下:
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("myds");
Connection conn = ds.getConnection();
System.out.println(conn);
ResultSet rs = conn.createStatement().executeQuery(
"select * from admin ");
while (rs.next()) {
System.out.println(rs.getObject(2));
System.out.println(rs.getObject(3));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
常规方法测试2:在servlet中测试代码如下:
public class HelloWorld extends HttpServlet {
private @Resource(name="myds") DataSource nt;
private @Resource(name="welcomeMessage") String welcomeMsg;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
System.out.println("OK");
PrintWriter out = response.getWriter();
Connection conn = nt.getConnection();
}