如何通过连接池连接数据库?
1 将tomcate服务器的配置文件中添加 一组代码,内含 driver,url,username,password2 在公共类 baseDao.java 中 编写 公共连接方法:getConnection()
3 启动Tomcat服务器,在webroot目录下创建aa.jsp,编写测试代码
1、配置Tomcat的conf/context.xml(将下面代码复制粘贴到context.xml中)
<Resource name="jdbc/news"auth="Container" type="javax.sql.DataSource" maxActive="100"
maxIdle="30" maxWait="10000" username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/kgcnews"/>
2 BaseDao.java
package com.kgc.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import com.kgc.util.ConfigManager; public class BaseDao { //实现增删改查的功能 以及连接 和释放资源 Connection cnt=null; PreparedStatement ps=null; ResultSet rs=null; //连接有3种方式 :通过连接池配置文件连接 public boolean getConnection(){ try { //初始化上下文 Context ct=new InitialContext(); //获取与逻辑名相关联的数据源对象 DataSource ds=(DataSource) ct.lookup("java:comp/env/jdbc/news"); cnt=ds.getConnection(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } //关闭资源 public boolean closeResource(){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(cnt!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return true; } //增删改 public int executeUpdate(String sql ,Object [] params){ int updateRows=0; if(this.getConnection()){ //获得sql语句 try { ps=cnt.prepareStatement(sql); //给占位符的赋值 for(int i=0;i<params.length;i++){ ps.setObject(i+1,params[i] ); } //执行 增删改,返回影响的行数 updateRows=ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return updateRows; } //查询 全部信息 或者指定部分信息 public ResultSet ExecuteQuery(String sql ,Object [] params){ if(this.getConnection()){ try { ps=cnt.prepareStatement(sql); //给占位符赋值 for(int i=0;i<params.length;i++){ ps.setObject(i+1, params[i]); } // 执行查询,返回值是ResultSet型的 rs=ps.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return rs; } }
3 aa.jsp
<%@ page import="com.kgc.dao.NewsDao" %> <%@ page import="com.kgc.dao.impl.NewsDaoImpl" %> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'aa.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% NewsDao dao=new NewsDaoImpl(); dao.getAS("%课工场%"); %> </body> </html>