TestDBCP.jsp 788 B

123456789101112131415161718192021222324252627282930313233
  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ page import="javax.naming.Context" %>
  3. <%@ page import="javax.naming.InitialContext" %>
  4. <%@ page import="java.sql.*" %>
  5. <%@ page import="javax.sql.DataSource" %>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Test DBCP</title>
  11. </head>
  12. <body>
  13. <%
  14. DataSource ds;
  15. try{
  16. Context context = new InitialContext();
  17. ds = (DataSource)context.lookup("java:comp/env/jdbc/dc");
  18. Connection conn = ds.getConnection();
  19. PreparedStatement pst = conn.prepareStatement("select * from bblb where rownum <= 2");
  20. ResultSet rs = pst.executeQuery();
  21. while(rs.next()){
  22. out.println(rs.getString("mc"));
  23. out.println("<br/>");
  24. }
  25. }catch(Exception e){
  26. e.printStackTrace();
  27. out.println("数据库连接失败");
  28. }
  29. %>
  30. </body>
  31. </html>