第二个文件是新闻操作文件DoNews:/*
 * Created on 2005-9-14
 * By winds
 */
package news;
import java.sql.*;
/**
 * @author winds
 * 11:07:36
 * @email [email protected]
 */
public class DoNews {
private NewsData news;
private Connection conn;
private DataConnect dataConn;
private ResultSet rs;
private boolean isNo;
private String test; /**
 * @param news The news to set.
 */
public void setNews(NewsData news) {
this.news = news;
}
//得到数据库连接
public void getConnection(String accessStr) {
try{
this.conn = dataConn.accessConnection(accessStr);
}catch(Exception e){
e.printStackTrace();
}
}
//得到所有新闻记录
public ResultSet getAll(){
try{
Statement stm = this.conn.createStatement();
this.rs = stm.executeQuery("select * from news order by newstime desc");
stm.close();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
//添加新记录
public void addNews()throws Exception{
String sql = "insert into news values(?,?,?,?,?)";
try{
PreparedStatement pstmt = this.conn.prepareStatement(sql);
pstmt.setString(1,news.getNewsName());
pstmt.setString(2,news.getNewsAuthor());
pstmt.setString(3,news.getNewsPic());
pstmt.setDate(4,new java.sql.Date(new java.util.Date().getTime()));
pstmt.setString(5,news.getNewsBody());
pstmt.executeUpdate();
pstmt.close();
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
//删除记录
public boolean delNews(int id)throws Exception{
String sql = "delete from news where id="+id;
try{
Statement stm = this.conn.createStatement();
this.isNo = stm.execute(sql);
}catch(Exception e){
e.printStackTrace();
throw e;
}
return this.isNo;
}
}

解决方案 »

  1.   

    现在的问题是在jsp页面中调用DoNews这个bean后,把数据库路径传入函数getConnection之后,函数中conn参数得到的变量是空值,也就是说DoNews调用DataConnect中的accessConnection()返回的是空值,这是为什么?传入的数据库路径我已经测试过了,是正确的,我直接在jsp页面中写出数据库连接元代码调用都正确,但是这两个bean文件的调用确是错误的,十分迷惑,希望大虾们能给看看,谢谢
      

  2.   

    DoNews中的dataConn对象实例化了没有??
    还有,如果没有得到conn看看后台有没有报错信息