一个数据库连接池的例子,可能对你有帮助:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class ConnectionPoolServlet extends HttpServlet{
//初始化全局变量
public void init (ServletConfig config)
throws ServletException{
super.init(config);
ConnectionPool pool=new ConnectionPool();
try{
pool.setDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
pool.setURL("jdbc:microsoft:sqlserver://192.168.0.208:1433;DataBaseName=pubs");
pool.setUsername("sa");
pool.setPassword("");
pool.initializePool();
//一旦连接池初始化后,把它加入全局变量 ServletContext中
//这使得其他Servlet都可以使用它
ServletContext context=getServletContext();
context.setAttribute("CONNECTION_POOL",pool);
}
catch (Exception e){
System.err.println(e.getMessage());
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out =response.getWriter();
out.println("This Servlet does not service requests!");
out.close();
}
public void destroy(){
ServletContext context=getServletContext();
ConnectionPool pool=(ConnectionPool)context.getAttribute("CONNECTION_POOL");
if(pool!=null){
pool.emptyPool();
context.removeAttribute("CONNECTION_POOL");
}
else{
System.err.println("Can't get a reference to pool!");
}
}public String getServletInfo(){
return "ConnectionPoolServlet information!";
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class ConnectionPoolServlet extends HttpServlet{
//初始化全局变量
public void init (ServletConfig config)
throws ServletException{
super.init(config);
ConnectionPool pool=new ConnectionPool();
try{
pool.setDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
pool.setURL("jdbc:microsoft:sqlserver://192.168.0.208:1433;DataBaseName=pubs");
pool.setUsername("sa");
pool.setPassword("");
pool.initializePool();
//一旦连接池初始化后,把它加入全局变量 ServletContext中
//这使得其他Servlet都可以使用它
ServletContext context=getServletContext();
context.setAttribute("CONNECTION_POOL",pool);
}
catch (Exception e){
System.err.println(e.getMessage());
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out =response.getWriter();
out.println("This Servlet does not service requests!");
out.close();
}
public void destroy(){
ServletContext context=getServletContext();
ConnectionPool pool=(ConnectionPool)context.getAttribute("CONNECTION_POOL");
if(pool!=null){
pool.emptyPool();
context.removeAttribute("CONNECTION_POOL");
}
else{
System.err.println("Can't get a reference to pool!");
}
}public String getServletInfo(){
return "ConnectionPoolServlet information!";
}
}
解决方案 »
- servlet 中控制 serial port
- Unknown column 'users0_.user_id' in 'field list'
- 求java中的正则表达式
- 老问题:怎么往数据库里写中文.网上搜了很多资料都没有搞定
- 又是邮件中文问题!!!
- 高分求Spring中的getJdbcTemplate().queryForList()使用方法
- 急急急,请推荐基本Java开发Web Service的好书!
- 小弟初学CMP/CMR,有个问题想向老鸟们请教
- 关于SQL SERVER的JDBC驱动的疑问。
- 兼职项目经理,系统分析员,架构师
- 关于数据库链接错误问题
- 请问JAVA如何调用VC++编写的COM组件?
ServletContext context=getServletContext();
context.setAttribute("CONNECTION_POOL",pool);其他Servlet就可以直接引用了