mport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * JDBC工具类
 */
public class JDBCUtil {
   
    private static String driver = "com.mysql.jbdc.Driver";
    private static String jdbcUrl = "jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8";
    private static String username = "root";
    private static String userpassword = "rsc125979";
    static {
        // 注册驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    // 获取Connection对象
    public static Connection getConnection(){
        // 创建连接
        Connection  conn =null;
        try {
            conn = DriverManager.getConnection(jdbcUrl,username,userpassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    // 关闭Statement
    public static void closeStatement(Statement state){
        if(state != null){
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    // 关闭Connection
    public static void closeConnection(Connection conn){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    // 关闭资源  将关闭Connection和Statement方法放到一起
    public static void closeResource(Statement state,Connection conn){
        closeStatement(state);
        closeConnection(conn);
    }
}
public class JDBXTest 
    // 更新departments表中department_id为6的数据
    public void updateDepartments(String department_name,int location_id,int department_id){
        Connection conn = null;
        Statement state = null;
        try{
            conn = JDBCUtil.getConnection();
            state = conn.createStatement();
            // sql语句
            String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id="+location_id+" where d.department_id = "+department_id+" ";
            // 执行sql语句
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch(Exception e) {
            e.printStackTrace();
        } finally{
            JDBCUtil.closeResource(state,conn);
        }
    }
    public static void main(String[] args) {
        JDBXTest test = new JDBXTest();
       test.updateDepartments("IT",6,11);
    }
}

语句执行成功了,但是为什么会报异常呀,没有使用工具类时更新数据没有报异常
报错的位置分别是
JDBCUtil中的
Class.forName(driver);
JDBCTest中的
conn = JDBCUtil.getConnection();
和
test.updateDepartments("IT",6,11);