为了账号安全,请及时绑定邮箱和手机立即绑定

我这jdbc哪里错了 怎么连不上数据库

我这jdbc哪里错了 怎么连不上数据库

学无丶直径 2016-12-19 17:37:31
package com.lyh.loginDao;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.Collection;import com.lyh.loginDao.MessageBO;import com.mysql.jdbc.Connection;public class MessageBean{ private Connection con; MessageBO msg; public MessageBean(){ String CLASSFORNAME="com.mysql.jdbc.Driver"; String SERVANDDB="jdbc:mysql://localhost:3306/forum?useUnicode=true&amp;characterEncoding=UTF-8 "; String USER="root"; String PWD="123123"; try { Class.forName(CLASSFORNAME); con=(Connection) DriverManager.getConnection(SERVANDDB, USER, PWD); } catch (Exception e) { e.printStackTrace(); } } public void setMessage(MessageBO msg){ this.msg=msg; } public void addMessage()throws Exception{ //response.setContentType("text/html; charset=utf-8"); try { PreparedStatement stm= con.prepareStatement("insert into text1 values(?,?,?,?,?)");    stm.setString(1, msg.getTitle());    stm.setString(2, msg.getName());        System.out.println(msg.getName());        if(msg.getEmail().length()==0){     stm.setString(3, null);    }else{     stm.setString(3, msg.getEmail());    }    stm.setString(4, msg.getContent());    stm.setDate(5, new java.sql.Date(new java.util.Date().getTime()));    System.out.println(new java.util.Date().getTime());    stm.executeUpdate();    con.close(); } catch (Exception e) { e.printStackTrace(); } } public Collection<MessageBO> getMessage()throws Exception{ Collection<MessageBO> ret=new ArrayList<MessageBO>(); try { Statement stm=con.createStatement(); ResultSet result=stm.executeQuery("select count(*) from text1"); int message_count=0; if(result.next()){ message_count=result.getInt(1); result.close(); } if(message_count>0){ result=stm.executeQuery("select * from text1 order by date desc"); while(result.next()){ String title=result.getString("title"); String name=result.getString("name"); String mail=result.getString("email"); String content=result.getString("content"); java.sql.Date date=result.getDate("date"); MessageBO message=new MessageBO(); message.setName(name); message.setTitle(title); message.setContent(content); message.setEmail(mail); message.setDate(date); ret.add(message); } result.close(); stm.close(); } con.close(); } catch (Exception e) { e.printStackTrace(); } return ret; }}No operations allowed after connection closed.(报的错  关闭后不能操作数据库)
查看完整描述

1 回答

?
慕瓜4807886

TA贡献25条经验 获得超9个赞

if(result.next()){

message_count=result.getInt(1);

result.close();

}

估计是这里关了

查看完整回答
反对 回复 2016-12-19
  • 1 回答
  • 0 关注
  • 1661 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信