java下找JNDI

protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//TODO Method stub generated by Lomboz
Context initctx=null;
Context ctx=null;
DataSource ds=null;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
response.setContentType("text/html");
PrintWriter out=response.getWriter();

try {
initctx=new InitialContext();
ctx= (Context) initctx.lookup("jdbc:comp/env");
ds=(DataSource)ctx.lookup("jdbc/hibernate");
if(ds==null) throw new NamingException("no database");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
conn=ds.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from brand");
rs.next();
out.println("<html>");
out.println("<head");
out.println("<title>test jndi</title>");
out.println("</head>");
out.println("<body>");
out.println(rs.getString("name"));
} catch (SQLException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
}
}
Why you lookup SessionFactory, instead of datasource?

try this:
initctx=new InitialContext();
ctx= (Context) initctx.lookup("java:comp/env");
ds=(DataSource)ctx.lookup("jdbc/hibernate");

And move your resource defintion outside of your Context - move it to default Context and try.



or post your server.xml here.

If still cannot work, reverse to the original and make changes step by step. 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值