NOTE: Java component can't use cached ODBC connection, you need to use a cached JDBC connection. If there is no JDBC driver available, it's still possible to use the ODBC-JDBC provided by Sun. But performance of such a bridge is poor and the reliability is not good in a multi-threaded environment.
To define a cache using ODBC-JDBC (bridge) connection, follow these steps:
<%@page contentType="text/html"%>
<html>
<head><title>JSP Page</title></head>
<body>
<%@ page import="java.sql.*" %>
<%
com.sybase.jaguar.jcm.JCMCache jcmCache= null;
java.sql.Connection dbConn = null;
String jcmCacheString = "mycachename";
String msg = "";
try {
jcmCache =
com.sybase.jaguar.jcm.JCM.getCacheByName(jcmCacheString);
dbConn =
jcmCache.getConnection(com.sybase.jaguar.jcm.JCMCache.JCM_WAIT);
Statement stmt = dbConn.createStatement();
ResultSet rs =
stmt.executeQuery
("select msg from messages where msgid='00001'");
if(rs != null) {
while(rs.next()) {
msg = rs.getString("msg");
}
}
rs.close();
dbConn.close();
}
catch (Exception e) {
out.println("OUPS " + e.getMessage() + "<BR>");
}
%>
msgtext =
<i><%= msg %></i>
</body>
</html>
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup("java:comp/env/jdbc/myconnection");
java.sql.Connection con = ds.getConnection();
java.sql.PreparedStatement stmt = con.prepareStatement(sql);
java.sql.ResultSet rs = stmt.executeQuery();
while (rs.next()) {
//do something
}
Written and compiled by Réal Gagnon ©1998-2005
[ home ]