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

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&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.(报的错  关闭后不能操作数据库)

学无丶直径
浏览 1669回答 1
1回答

慕瓜4807886

if(result.next()){message_count=result.getInt(1);result.close();}估计是这里关了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java