JpcourceManager.java的代码: 
package com.rong.jpkc.manager;import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import com.rong.jpkc.db.DB;
import com.rong.jpkc.pojo.Jpcource;public class JpcourceManager {

DB db = new DB();

//添加精品课程
public void addCource(Jpcource cource){
PreparedStatement pstmt = null;
String sql = "insert into jpcource(name,teacher,url) values(?,?,?)";
pstmt = db.prepareStmt(sql);
try {
pstmt.setString(1, cource.getName());
pstmt.setString(2, cource.getTeacher());
pstmt.setString(3, cource.getUrl());
pstmt.executeUpdate();

} catch (SQLException e) {
System.out.println("-----------------JpcourceManager.addCource()方法异常-------------------------");
e.printStackTrace();
} finally {
db.closePstmt(pstmt);
}
}

//删除精品课程
public void delCource(int id){
PreparedStatement pstmt = null;
String sql = "delete jpcource where id=?";
pstmt = db.prepareStmt(sql);
try {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("--------------------- JpcourceManager.delCource方法异常 -------------------------------");
} finally {
db.closePstmt(pstmt);
}
}

//查询所有课程操作
public List<Jpcource> queryCource(){
List<Jpcource> list = new ArrayList<Jpcource>(); //new 一个list容器来保存查询结果
ResultSet rs = null;
String sql = "select * from jpcource";
rs = db.exeQuery(sql);
try {
while(rs.next()){
Jpcource cource = new Jpcource();
cource.setId(rs.getInt("id"));
cource.setName(rs.getString("name"));
cource.setTeacher(rs.getString("teacher"));
cource.setUrl(rs.getString("url"));
list.add(cource);
}
} catch (SQLException e) {
System.out.println("-----------------------JpcourceManager.queryCource()方法异常-------------");
e.printStackTrace();
} finally{
db.closeRs(rs);
}
return list;
}


//查询要修改的课程
public Jpcource queryCourceById(int id) {
PreparedStatement pstmt = null;
String sql = "select * from jpcource where id=?";
pstmt = db.prepareStmt(sql);
try {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("---------JpcourceManager.queryCourceById()方法异常-------------");
} finally {
db.closePstmt(pstmt);
}

Jpcource cource = new Jpcource();
ResultSet rs = null;
rs = db.exeQuery(sql);
try {
cource.setId(rs.getInt("id"));
cource.setName(rs.getString("name"));
cource.setTeacher(rs.getString("teacher"));
cource.setUrl(rs.getString("url"));
} catch (SQLException e) {
System.out.println("-----------JpcourceManager.querryCourceById()方法异常--------------");
e.printStackTrace();
} finally {
db.closeRs(rs);
}
return cource;
}
//修改精品课程
public void modCource(Jpcource cource){
PreparedStatement pstmt = null;
String sql = "update jpcource set name=?,teacher=?,url=? where id=?";
pstmt = db.prepareStmt(sql);
try {
pstmt.setString(1, cource.getName());
pstmt.setString(2, cource.getTeacher());
pstmt.setString(3, cource.getUrl());
pstmt.setInt(4, cource.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("-----------------JpcourceManager.modCource()方法异常-------------------------");
e.printStackTrace();
} finally {
db.closePstmt(pstmt);
}
}
}

解决方案 »

  1.   

    JpcourceManager.java的代码: 
    package com.rong.jpkc.manager;import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;import com.rong.jpkc.db.DB;
    import com.rong.jpkc.pojo.Jpcource;public class JpcourceManager {

    DB db = new DB();

    //添加精品课程
    public void addCource(Jpcource cource){
    PreparedStatement pstmt = null;
    String sql = "insert into jpcource(name,teacher,url) values(?,?,?)";
    pstmt = db.prepareStmt(sql);
    try {
    pstmt.setString(1, cource.getName());
    pstmt.setString(2, cource.getTeacher());
    pstmt.setString(3, cource.getUrl());
    pstmt.executeUpdate();

    } catch (SQLException e) {
    System.out.println("-----------------JpcourceManager.addCource()方法异常-------------------------");
    e.printStackTrace();
    } finally {
    db.closePstmt(pstmt);
    }
    }

    //删除精品课程
    public void delCource(int id){
    PreparedStatement pstmt = null;
    String sql = "delete jpcource where id=?";
    pstmt = db.prepareStmt(sql);
    try {
    pstmt.setInt(1, id);
    pstmt.executeUpdate();
    } catch (SQLException e) {
    System.out.println("--------------------- JpcourceManager.delCource方法异常 -------------------------------");
    } finally {
    db.closePstmt(pstmt);
    }
    }

    //查询所有课程操作
    public List<Jpcource> queryCource(){
    List<Jpcource> list = new ArrayList<Jpcource>(); //new 一个list容器来保存查询结果
    ResultSet rs = null;
    String sql = "select * from jpcource";
    rs = db.exeQuery(sql);
    try {
    while(rs.next()){
    Jpcource cource = new Jpcource();
    cource.setId(rs.getInt("id"));
    cource.setName(rs.getString("name"));
    cource.setTeacher(rs.getString("teacher"));
    cource.setUrl(rs.getString("url"));
    list.add(cource);
    }
    } catch (SQLException e) {
    System.out.println("-----------------------JpcourceManager.queryCource()方法异常-------------");
    e.printStackTrace();
    } finally{
    db.closeRs(rs);
    }
    return list;
    }


    //查询要修改的课程
    public Jpcource queryCourceById(int id) {
    PreparedStatement pstmt = null;
    String sql = "select * from jpcource where id=?";
    pstmt = db.prepareStmt(sql);
    try {
    pstmt.setInt(1, id);
    pstmt.executeUpdate();
    } catch (SQLException e) {
    System.out.println("---------JpcourceManager.queryCourceById()方法异常-------------");
    } finally {
    db.closePstmt(pstmt);
    }

    Jpcource cource = new Jpcource();
    ResultSet rs = null;
    rs = db.exeQuery(sql);
    try {
    cource.setId(rs.getInt("id"));
    cource.setName(rs.getString("name"));
    cource.setTeacher(rs.getString("teacher"));
    cource.setUrl(rs.getString("url"));
    } catch (SQLException e) {
    System.out.println("-----------JpcourceManager.querryCourceById()方法异常--------------");
    e.printStackTrace();
    } finally {
    db.closeRs(rs);
    }
    return cource;
    }
    //修改精品课程
    public void modCource(Jpcource cource){
    PreparedStatement pstmt = null;
    String sql = "update jpcource set name=?,teacher=?,url=? where id=?";
    pstmt = db.prepareStmt(sql);
    try {
    pstmt.setString(1, cource.getName());
    pstmt.setString(2, cource.getTeacher());
    pstmt.setString(3, cource.getUrl());
    pstmt.setInt(4, cource.getId());
    pstmt.executeUpdate();
    } catch (SQLException e) {
    System.out.println("-----------------JpcourceManager.modCource()方法异常-------------------------");
    e.printStackTrace();
    } finally {
    db.closePstmt(pstmt);
    }
    }
    }
      

  2.   

    DB.java的代码:package com.rong.jpkc.db;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class DB {

    private static DB db = null;
    private static Connection conn = null;
    private static Statement stmt = null;

    //单例模式,new DB类的时候,创建唯一对象,只初始化一次,
    //注意:不要显式交闭此static中的Connection和Statement对象,否则抛空指针异常
    static{
    try {
    String driverClass = "net.sourceforge.jtds.jdbc.Driver";
    String url = "jdbc:jtds:sqlserver://localhost:1433;DatabaseName=yjsc";
    String username = "sa"; //edit
    String password = ""; //edit
    Class.forName(driverClass);
    conn = DriverManager.getConnection(url,username,password);
    stmt = conn.createStatement();
    System.out.println("--------初始化---------");
    } catch (ClassNotFoundException e) {
    System.out.println("---------- 加载数据库驱动类时发生异常: ----------");
    e.printStackTrace();
    } catch (SQLException e) {
    System.out.println("------------ getConnection()方法发生异常--------------");
    }
    }

    //创建PrepareStatement对象
    public PreparedStatement prepareStmt(String sql){
    PreparedStatement pstmt = null;
    try {
    pstmt = conn.prepareStatement(sql);
    } catch (SQLException e) {
    System.out.println("-------------prepareStmt()方法发生异常-------------------");
    e.printStackTrace();
    }
    return pstmt;
    }

    //执行查询所有记录操作
    public ResultSet exeQuery(String sql){
    ResultSet rs = null;
    try {
    rs = stmt.executeQuery(sql);
    } catch (SQLException e) {
    System.out.println("------------exeQuery()方法发生异常: --------------------");
    e.printStackTrace();
    }
    return rs;
    }

    //执行保存、更新、删除操作
    public void exeUpdate(String sql){
    try{
    stmt.executeUpdate(sql);
    } catch(SQLException e){
    System.out.println("------------- exeUpdate()方法发生异常------------------");
    e.printStackTrace();
    }
    }


    //关闭PreparedStatement对象
    public void closePstmt(PreparedStatement pstmt){
    try{
    pstmt.close();
    pstmt = null;
    }catch(SQLException e){
    System.out.println("-------------------- DB.closePstmt()方法发生异常 -------------------------");
    e.printStackTrace();
    }
    }

    //关闭ResultSet对象
    public void closeRs(ResultSet rs){
    try{
    rs.close();
    rs = null;
    }catch(SQLException e){
    System.out.println("-------------------- DB.closePstmt()方法发生异常 -------------------------");
    e.printStackTrace();
    }
    }
    }
      

  3.   

    错误在这儿:
    严重: Servlet.service() for servlet UpdateJpcourceServlet threw exception 
    java.lang.NullPointerException 
    at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:168) 
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeQuery(JtdsStatement.java:1291) 
    at com.rong.jpkc.db.DB.exeQuery(DB.java:52) 你检查一下DB.java的52行,是不是你调用了某个空引用的方法,解决了这个空引用就可以了。
      

  4.   


    /查询要修改的课程 
    public Jpcource queryCourceById(int id) { 
    PreparedStatement pstmt = null; 
    String sql = "select * from jpcource where id=?"; 
    pstmt = db.prepareStmt(sql); 
    try { 
    pstmt.setInt(1, id); 
    pstmt.executeUpdate(); 
    } catch (SQLException e) { 
    System.out.println("---------JpcourceManager.queryCourceById()方法异常-------------"); 
    } finally { 
    db.closePstmt(pstmt); 
    } Jpcource cource = new Jpcource(); 
    ResultSet rs = null; 
    rs = db.exeQuery(sql); 
    try { 
    cource.setId(rs.getInt("id")); 
    cource.setName(rs.getString("name")); 
    cource.setTeacher(rs.getString("teacher")); 
    cource.setUrl(rs.getString("url")); 
    } catch (SQLException e) { 
    System.out.println("-----------JpcourceManager.querryCourceById()方法异常--------------"); 
    e.printStackTrace(); 
    } finally { 
    db.closeRs(rs); 

    return cource; 
    }db.closePstmt(pstmt);
    rs在查询返回后可能没有数据导致了空指针的异常!
      

  5.   

    NullPointerException 
    是好事啊,每次遇到这种错我就笑了,那种少包多包冲突的问题我看着就晕...
      

  6.   

    /查询要修改的课程 
    public Jpcource queryCourceById(int id) { 
    PreparedStatement pstmt = null; 
    String sql = "select * from jpcource where id=?"; 
    pstmt = db.prepareStmt(sql); 
    try { 
    pstmt.setInt(1, id); 
    pstmt.executeUpdate(); 
    } catch (SQLException e) { 
    System.out.println("---------JpcourceManager.queryCourceById()方法异常-------------"); 
    } finally { 
    db.closePstmt(pstmt); 
    } Jpcource cource = new Jpcource(); 
    ResultSet rs = null; 
    rs = db.exeQuery(sql); 
    try { 
    cource.setId(rs.getInt("id")); 
    cource.setName(rs.getString("name")); 
    cource.setTeacher(rs.getString("teacher")); 
    cource.setUrl(rs.getString("url")); 
    } catch (SQLException e) { 
    System.out.println("-----------JpcourceManager.querryCourceById()方法异常--------------"); 
    e.printStackTrace(); 
    } finally { 
    db.closeRs(rs); 

    return cource; 
    }
      

  7.   

    NullPointerException 
    说起这个我想起一件很有味的事情
    某java高手进了一家公司
    上班的第二天出现了~NullPointerException ,他找了半天不知道怎么回事,就去问项目经理
    项目经理看他工作态度很积极,就去帮他调试,这下好了,当经理看到错误信息NullPointerException 的时候,大吃一惊~对他说了句:NullPointerException 你能翻译这句话么?不能的话马上走人,能的话也给我滚!
      

  8.   

    at com.rong.jpkc.db.DB.exeQuery(DB.java:52)
    at com.rong.jpkc.manager.JpcourceManager.queryCourceById(JpcourceManager.java:91) 看看那个 DB.java的52行吧,
      

  9.   

    rs = db.exeQuery(sql); 这个 db 100%是 null 啊!至于原因,去看看 db从哪里来的吧!