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.
java下找JNDI
最新推荐文章于 2024-09-08 10:40:56 发布