您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Java >> JavaEE MySQL数据库读取问题

JavaEE MySQL数据库读取问题

来源:网络整理     时间:2016/8/13 18:42:56     关键词:

关于网友提出的“ JavaEE MySQL数据库读取问题”问题疑问,本网通过在网上对“ JavaEE MySQL数据库读取问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: JavaEE MySQL数据库读取问题
描述:

代码如下: public List getTopicList(int id) throws Exception
   {
   PreparedStatement pst=null;
   ResultSet rs=null;
   String sql="select id,barId,title,text,pid,floor,hitnum,replynum,fromip,replyip,date_format(postDate,'%y-%m-%d %h:%i:%s') as pd,date_format(replyDate,'%y-%m-%d %h:%i:%s')as rd,author from post where barId=? and pid=0 order by replyDate desc";
   
   try
   {
 pst=conn.prepareStatement(sql);
 pst.setInt(1, id);
 rs=pst.executeQuery();
return populatePost(rs);
   }
   finally
   {
   if (rs!=null) rs.close();
   if (pst!=null) pst.close();
   
   }
   }
   private List populatePost(ResultSet rs) throws Exception
   {
   List posts=new ArrayList();
   while(rs.next())
   {
   Post post=new Post();
   post.setId(rs.getInt("id"));
   post.setBarId(rs.getInt("barId"));
   post.setTitle(rs.getString("title"));
   post.setText(rs.getString("text"));
   post.setPid(rs.getInt("pid"));
   post.setFloor(rs.getInt("floor"));
   post.setHitnum(rs.getInt("hitnum"));
   post.setReplynum(rs.getInt("replynum"));
   post.setFromip(rs.getString("fromip"));
   post.setReplyip(rs.getString("replyip"));
   post.setPostDate(rs.getTimestamp("pd"));
   post.setReplyDate(rs.getTimestamp("rd"));
   post.setAuthor(rs.getString("author"));
   posts.add(post);
   }
   return posts;
   }
小弟为了让在JSP页面上时间长格式,写了上面的代码!但是运行后会提示Buffer overrun detected信息! 如果改成
select  * from post where barId=? and pid=0 order by replyDate desc
并把pd 和re 改成数据库里的字段 可以正常运行! 但是在JSP页面上显示的时间格式不是想要的那种格式
post.setPostDate(rs.getTimestamp("pd"));
post.setReplyDate(rs.getTimestamp("rd"));
怎么样才能在JSP上显示yyyy-MM-dd HH:mm:ss 格式呢! 


解决方案1:

用dateformat转成string就是了啊?
java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

解决方案2:

  SimpleDateFormat lFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  post.setPostDate(lFormat.format(rs.getTimestamp("pd")));
  post.setReplyDate(lFormat.format(rs.getTimestamp("pd")));
试试吧


以上介绍了“ JavaEE MySQL数据库读取问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3259567.html

相关图片

相关文章