首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

为什么小弟我的

2012-05-20 
为什么我的import java.io.IOExceptionimport java.sql.Connectionimport java.sql.SQLExceptionimport

为什么我的
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();
}

}

}


[解决办法]
那就是你的连接数据库没成功吧

热点排行