import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.naming.Context;
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;
import org.apache.commons.dbutils.handlers.BeanListHandler;
public class GetBlogServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static Statement stmt = null;
Connection conn;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String sql = "select id,title,content,createTime from blog where id="
+ id;
DataSource ds = null;
try {
Context context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (Exception e) {
System.out.println("连接出错");
}
try {
QueryRunner qr = new QueryRunner(ds);
List list = (List) qr.query(sql, new BeanListHandler(Blog.class));
Blog blog = (Blog) list.get(0);
System.out.println(blog.getId());
System.out.println(blog.getTitle());
System.out.println(blog.getContent());
System.out.println(blog.getCreateTime());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
------解决方案--------------------------------------------------------
那就是你的连接数据库没成功吧