tomcat的 配置,进入conf->context.xml
web中调用
Context c=new InitialContext();
DataSource ds=(DataSource)c.lookup("java:comp/env/mysql");
out.println(ds.getConnection());
通过注记获取
@Resource(name="mysql",type=DataSource.class,authenticationType=AuthenticationType.CONTAINER)
public class Test extends HttpServlet {
@Resource(name="mysql")
private DataSource ds;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
Context c=new InitialContext();
out.println(ds.getConnection().toString());
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
out.close();
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章