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);
}
}
}
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);
}
}
}
解决方案 »
- 关于SSH2整合开发运行一段时间spring就释放bean?请高手帮忙解决。
- 正在学习servlet和jsp,大家推荐本书啊
- 非常经典的java编写数据库sql性能优化工具
- servlet验证数据究竟该怎么写? 一般人不知道
- 一个简单的Java聊天室,帮我看看那里出错了.
- 从jsp页面怎么往XML文件存取数据,100分等你拿?
- servlet 问题。(1)重定向。(2)tomcat4.1.2问题。
- 这里的人是不是死光了,连这么简单的问题都没人回答吗??
- Frame 模式 窗口的制作?
- 刚毕业上班1个月Java程序员开始写技术小结
- servlet问题
- 我整合Struts和spring时一个简单错误
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);
}
}
}
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();
}
}
}
严重: 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行,是不是你调用了某个空引用的方法,解决了这个空引用就可以了。
/查询要修改的课程
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在查询返回后可能没有数据导致了空指针的异常!
是好事啊,每次遇到这种错我就笑了,那种少包多包冲突的问题我看着就晕...
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;
}
说起这个我想起一件很有味的事情
某java高手进了一家公司
上班的第二天出现了~NullPointerException ,他找了半天不知道怎么回事,就去问项目经理
项目经理看他工作态度很积极,就去帮他调试,这下好了,当经理看到错误信息NullPointerException 的时候,大吃一惊~对他说了句:NullPointerException 你能翻译这句话么?不能的话马上走人,能的话也给我滚!
at com.rong.jpkc.manager.JpcourceManager.queryCourceById(JpcourceManager.java:91) 看看那个 DB.java的52行吧,