会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133672个问题
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 23791楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/飞机大战小项目训练 23793楼
人工智能/第五阶段:机器学习-线性回归/Lasso回归_Ridge回归_多项式回归 23794楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/虚拟环境 23796楼

package com.bjsxt;

import java.nio.file.attribute.UserPrincipalLookupService;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * PreparedStatement对象的使用
 * @author Administrator
 *
 */
public class PreparedStatementDemo {

	//向Departments表中插入一条数据
	public void insertDempartments(String departmentName,int locationId){
		Connection conn = null;
		PreparedStatement ps = null;
		try{
			conn = JdbcUtil.getConnection();
			ps = conn.prepareStatement("insert into departments values(default,?,?)");
			ps.setString(1, departmentName);
			ps.setInt(2, locationId);
			ps.execute();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResource(ps, conn, null);
		}
	}
	//更新数据
	public void updateDepartment(int departmentId,String departmentName,int localhostId){
		Connection conn= null;
		PreparedStatement ps = null;
		try{
			conn = JdbcUtil.getConnection();
			ps = conn.prepareStatement("update departments set department_name = ?,location_id = ? where department_id = ?");
			ps.setString(1, departmentName);
			ps.setInt(2, localhostId);
			ps.setInt(3, departmentId);
			ps.execute();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResource(ps, conn, null);
		}
	}
	//完成数据查询
	public Departments selectDepartmentsById(int departmentId){
		Connection conn = null;
		PreparedStatement  ps = null;
		ResultSet rs = null;
		Departments dept = null;
		try{
			conn = JdbcUtil.getConnection();
			ps = conn.prepareStatement("select * from departments where department_id = ?");
			ps.setInt(1, departmentId);
			rs = ps.executeQuery();
			while(rs.next()){
			//ORM: Object Relational Mapping
			 dept=new Departments();
			 dept.setDepartmentId(rs.getInt("department_id"));
			 dept.setDepartmentName(rs.getString("department_name"));
			 dept.setLocationId(rs.getInt("location_id"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResource(ps, conn, rs);
		}
		return dept;
	} 
	//查询部门表中的部门名称,找到那些包含“人力”的部门信息
	public List<Departments> selectDepartmentByLikeName(String departmentName){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Departments> list = new ArrayList<>();
		try{
			conn = JdbcUtil.getConnection(); 
			ps = conn.prepareStatement("select * from departments where department_name like ?");
			ps.setString(1, "%"+departmentName+"%");
			rs = ps.executeQuery();
			while(rs.next()){
				 Departments dept = new Departments();
				 dept.setDepartmentId(rs.getInt("department_id"));
				 dept.setDepartmentName(rs.getString("department_name"));
				 dept.setLocationId(rs.getInt("location_id"));
				 list.add(dept);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResource(ps, conn, rs);
		}
		return list;
	}
	//批量添加
	public void addBatch(List<Departments> list){
		Connection conn = null;
		PreparedStatement ps = null;
		try{
			conn = JdbcUtil.getConnection();
			ps = conn.prepareStatement("insert into  departments values(default,?,?)");
			for(int i=0;i<list.size();i++){
				ps.setString(1, list.get(i).getDepartmentName());
				ps.setInt(2, list.get(i).getLocationId());
				//添加批处理
				ps.addBatch();
			}
			int[] arr =ps.executeBatch();
			for(int i=0;i<arr.length;i++){
				System.out.println(i);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResource(ps, conn, null);
		}
	}
	//事务处理
	public void deleteDempartments(String depratmentName){
		Connection conn = null;
		PreparedStatement ps = null;
		try{
			conn = JdbcUtil.getConnection();
			//关闭事务的自动提交
			conn.setAutoCommit(false);
			ps = conn.prepareStatement("delete from departments where department_name like ?");
			ps.setString(1, "%"+depratmentName+"%");
			ps.executeUpdate();
			ps = conn.prepareStatement("insert into departments values(default,'开发部',2)");
			ps.executeUpdate();
			String str = null;
			str.length();
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeResource(ps, conn, null);
		}
	}
	public static void main(String[] args) {
		PreparedStatementDemo demo = new PreparedStatementDemo();
		//demo.insertDempartments("人力资源部3", 10);
		//demo.updateDepartment(10, "人力资源部10", 20);
		/*Departments dept = demo.selectDepartmentsById(10);
		if(dept != null){
			System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+dept.getLocationId());
			
		}*/
		/*List<Departments> list = demo.selectDepartmentByLikeName("人力");
		for(Departments dept :list){
			System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+dept.getLocationId());
		}*/
		/*
		List<Departments> list = new ArrayList<>();
		for(int i=1;i<=10;i++){
			Departments dept =new Departments();
			dept.setDepartmentName("教学部"+i);
			dept.setLocationId(20+i);
			list.add(dept);
		}		
		demo.addBatch(list);*/
		demo.deleteDempartments("研发部");
	}

}


报错代码:
java.lang.NullPointerException
	at com.bjsxt.PreparedStatementDemo.deleteDempartments(PreparedStatementDemo.java:138)
	at com.bjsxt.PreparedStatementDemo.main(PreparedStatementDemo.java:169)

老师,问一下,这个报错的原因是什么呢?

JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 23797楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 23798楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用(旧) 23800楼
Python 全系列/第二十一阶段:数据分析-统计分析/Python统计分析 23801楼
7u职场软实力/第五阶段:沟通力/第五章:批评的来源与方法 23803楼
Python 全系列/第四阶段:函数式编程和核心特性/内存管理 23804楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 23805楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637