我正在尝试使用jdbc连接池,因为我创建了IntialContext obj和datasource obj,并将“dsjndi”作为initialcontext的查找方法的参数,但是当我运行我的项目项目时它显示“”名称[dsjndi ]不受此上下文的约束。找不到[dsjndi]。“” 任何人都可以指导我的问题是什么?
Connection con = null;
Statement st = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
public void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
//general settings
//get print Writer obj
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//read form data
String tableName = req.getParameter("table");
try
{
//get con obj from jdbc con pool
con = makeConnection();
//create Statement obj
st = con.createStatement();
//execute query
rs = st.executeQuery("select * form" +tableName);
//use metadata obj to get db vlaues along with the col names
rsmd = rs.getMetaData();
int cnt = rsmd.getColumnCount();
pw.println("<hmtl><body bgcolor='#ffffa9'>");
pw.println("<center><h3>the details of the table" +tableName);
pw.println("<br><br><table cellsapcing='1' bgcolor='#00ff00'>");
pw.println("<tr bgcolor='#ffffa9'>");
//pritnt col names
for(int col=1;col<=cnt;col++)
{
pw.println("<th>" +rsmd.getColumnLabel(col)+ "  ");
}
pw.println("</tr>");
//print col values
while(rs.next())
{
pw.println("<tr bgcolor='#ffffa9'>");
for(int i=1;i<=cnt;i++)
{
pw.println("<td>" +rs.getString(i)+ " </td>");
}
pw.println("</td>");
}//while
pw.println("<table><br/>");
pw.println("<b>to view another table</b><ahref='index.html'>click here</a>");
pw.println("</body></html>");
}//try
catch(Exception e)
{
e.printStackTrace();
}
}
private Connection makeConnection()
{
Connection con = null;
try
{
//get IntialConetext
InitialContext ic = new InitialContext();
//get data sourse obj
DataSource ds = (DataSource) ic.lookup("DsJndi");
//get jdbc con obj from con pool
con = ds.getConnection();
}//try
catch(Exception e)
{
e.printStackTrace();
}
return con;
}//make Connection
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
process(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
process(req,res);
}
}