在tomcat中配置mysql数据库连接池的方法及第三方对数据库的封装:dbutils
你可在每个web工程目录下的META-INF\context.xml文件中.
context.xml文件中的内容可以这样写:
<Context><Resource name="jdbc/mysqlds"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="100"
maxWait="5000"
username="root"
password="root"
driverClassname="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1/liuwei"
/></Context>
package cn.com.jobedu.blog;
import java.io.IOException;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.SQLException;
importjavax.naming.InitialContext;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;public class BlogServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException,IOException{
doPost(request,response);
}public void doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException,IOException{
request.setCharacterEncoding("UTF-8");
System.out.println("dopost");
Stringtitle=request.getParameter("title");
Stringcontent=request.getParameter("content");
StringcategoryId=request.getParameter("category");
DataSourceds=null;
try{
//通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
InitialContextcontext=newInitialContext();
ds=(DataSource)context.lookup("java:/comp/env/jdbc/mysqlds");
}catch(Exceptione){
System.out.println("获取数据源时出错");
}
intresult=0;
try{
Connectionconn=ds.getConnection();
Stringsql="insertintoblog(title,content,category_id,createdtime)values(?,?,?,now())";
PreparedStatementpstmt=conn.prepareStatement(sql);
pstmt.setString(1,title);
pstmt.setString(2,content);
pstmt.setInt(3,Integer.parseInt(categoryId));
result=pstmt.executeUpdate();
System.out.println(result);
}catch(SQLExceptione){
e.printStackTrace();
}
Stringmessage="";
if(result==1){
message="添加博文成功";
}else{
message="添加博文失败";
}request.setAttribute("message", message);
request.getRequestDispatcher("/addBlogResult.jsp").forward(request,
response);
}
}第三方对数据库的封装:dbutils
package cn.com.jobedu.blog;
import java.io.IOException;
importjava.sql.Connection;
import java.sql.SQLException;import javax.naming.InitialContext;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;
public class BlogServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException,IOException{
doPost(request, response);}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
Stringtitle=request.getParameter("title");
Stringcontent=request.getParameter("content");
String categoryId = request.getParameter("category");System.out.println(title);
System.out.println(content);
System.out.println(categoryId);DataSource ds = null;
try{
//通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
InitialContextcontext=newInitialContext();
ds=(DataSource)context.lookup("java:/comp/env/jdbc/mysqlds");
}catch(Exceptione){
System.out.println("获取数据源时出错");
}
intresult=0;
try{
Stringsql="insertintoblog(title,content,category_id,createdtime)values(?,?,?,now())";
QueryRunnerqr=newQueryRunner(ds);
Stringparmas[]={title,content,categoryId};
result=qr.update(sql,parmas);
System.out.println(result);
}catch(SQLExceptione){
e.printStackTrace();
}
Stringmessage="";
if(result==1){
message="添加博文成功";
}else{
message="添加博文失败";
}request.setAttribute("message", message);
request.getRequestDispatcher("/addBlogResult.jsp").forward(request,
response);
}}