The following code sample shows a servlet using JDBC connection pooling in DataDirect XQuery®.
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import com.ddtek.xquery3.XQConnection;
import com.ddtek.xquery3.XQException;
import com.ddtek.xquery3.XQExpression;
import com.ddtek.xquery3.XQSequence;
import com.ddtek.xquery3.jdbc.XQueryConnection;
/**
* DataDirect Servlet example demonstrating the integration
with JDBC Connection Pooling
*/
public class DDXQServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection jdbc_c = null;
XQExpression xqj_e = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource jdbc_ds = (DataSource)envContext.lookup("jdbc/DDXQExample");
jdbc_c = jdbc_ds.getConnection();
PrintWriter out = response.getWriter();
XQConnection xqj_c = XQueryConnection.getXQConnection(jdbc_c);
xqj_e = xqj_c.createExpression();
XQSequence xqj_s = xqj_e.executeQuery(
"<>Current date: </b>,current-date(),<br/>," +
"<b>Current time: </b>,current-time(), " +
" <table border='1'> "+
" <tr> "+
" <th>User</th> "+
" <th>Stock</th> "+
" <th>Shares</th> "+
" </tr> "+ "
" { "+
" for $item in collection('holdings')/holdings "+
" return "+
" <tr> "+
" <td>{$item/userid/data(.)}</td> "+
" <td>{$item/stockticker/data(.)}</td> "+
" <td>{$item/shares/data(.)}</td> "+
" </tr> "+
" } "+
" </table> ");
xqj_s.writeSequence(out, new Properties());
out.close();
}
catch(Exception e){
throw new ServletException(e);
}
finally {
if (xqj_e != null) try{xqj_e.close();}
catch (XQException e) {}
if (jdbc_c != null) try{jdbc_c.close();}
catch (SQLException e) {}
}
}
}