<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <%@page import="javax.sql.*,javax.naming.*"%> <% long beginTime=System.currentTimeMillis(); int n=500; ResultSet rs=null; Statement st=null; Connection con=null; for(int i=0;i<n;i++) { try { Context ctx=new InitialContext(); //获得一个上下文对象 DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/pubs"); //通过上下文对象在JNDI中查找指定名字的数据源 con=ds.getConnection();//通过数据源获得一个连接池 String sql="select title,type,price from titles "; st=con.createStatement(); rs=st.executeQuery(sql); while(rs.next()) { rs.getString("title"); rs.getString("type"); rs.getString("price"); } } catch(Exception e) { out.print(e.getMessage()); } finally { if(rs!=null) rs.close(); if(st!=null) st.close(); if(con!=null) con.close(); } } long endTime=System.currentTimeMillis(); out.print("用连接池作500次查询耗时:"+(endTime-beginTime)+"毫秒"); %> </body> </html>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <%@page import="javax.sql.*,javax.naming.*"%> <% long beginTime=System.currentTimeMillis(); int n=500; ResultSet rs=null; Statement st=null; Connection con=null; for(int i=0;i<n;i++) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url="jdbc:sqlserver://localhost:1433;databaseName=pubs;user=sa;password="; con = DriverManager.getConnection(url); String sql="select title,type,price from titles "; st=con.createStatement(); rs=st.executeQuery(sql); while(rs.next()) { rs.getString("title"); rs.getString("type"); rs.getString("price"); } rs.close(); st.close(); con.close(); } catch(Exception e) { out.print(e.getMessage()); } finally { if(rs!=null) rs.close(); if(st!=null) st.close(); if(con!=null) con.close(); } } long endTime=System.currentTimeMillis(); out.print("直接用JDBC连接作500次查询耗时:"+(endTime-beginTime)+"毫秒"); %> </body> </html>