你可在每个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;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.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)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println("dopost");
String title = request.getParameter("title");
String content = request.getParameter("content");
String categoryId = request.getParameter("category");
DataSource ds = null;
try {
// 通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
InitialContext context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (Exception e) {
System.out.println("获取数据源时出错");
}
int result = 0;
try {
Connection conn = ds.getConnection();
String sql = "insert into blog (title, content,category_id,createdtime) values (?, ?, ?, now())";
PreparedStatement pstmt = 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 (SQLException e) {
e.printStackTrace();
}
String message = "";
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;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.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)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String content = 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文件,设定的数据源对象的名字,获取数据源对象
InitialContext context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (Exception e) {
System.out.println("获取数据源时出错");
}
int result = 0;
try {
String sql = "insert into blog (title, content,category_id,createdtime) values (?, ?, ?, now())";
QueryRunner qr = new QueryRunner(ds);
String parmas[] = { title, content, categoryId };
result = qr.update(sql, parmas);
System.out.println(result);
} catch (SQLException e) {
e.printStackTrace();
}
String message = "";
if (result == 1) {
message = "添加博文成功";
} else {
message = "添加博文失败";
}
request.setAttribute("message", message);
request.getRequestDispatcher("/addBlogResult.jsp").forward(request,
response);
}
}