会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134088个问题
JAVA 全系列/第八阶段:Linux入门到实战/Maven 31411楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>window对象的name属性</title>
</head>
<body>
    <button>跨域传输</button>
    <script>
/*         var name=123;
        console.log(name);
        console.log(typeof name);
        var age=123;
        console.log(age);
        console.log(typeof age); */

/*         window.name
        描述:是页面在切换之后,甚至域名更改之后会储存信息的容器
        说明:借助window.name可以实现页面之间数据的传递,称为跨域传输。 */
        //console.log(num);
        //如果想要获取一个页面的信息,那么必须先加载
        var but=document.querySelector('button');
        but.onclick=function(){
            var iframe=document.createElement('iframe');
            iframe.src='page.html';//加载保存了信息的页面
            iframe.style.display='none';
            document.body.appendChild(iframe);
            //当iframe加载完成,意味着window.name的内容已经被赋予完成
            iframe.onload=function(eve){
                var iframeWindowName=eve.target.contentWindow.name;
                //console.log(iframeWindowName);
                eval(iframeWindowName);
                console.log(num);

            }
        }
    </script>
</body>
</html>

image.png老师为什么这一行在vscode里运行会报错误呢?在WebStorm 就可以运行!!

image.png

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 31413楼
Python全系列/第一阶段:AI驱动的Python编程/编程基本概念 31416楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/IDEA的使用和第一个java项目 31418楼
JAVA 全系列/第三十一阶段:数据结构和算法BATJ大厂面试重难点/字符串和矩阵 31420楼
JAVA 全系列/第十四阶段:分布式文件存储与数据缓存/FastDFS 31421楼
JAVA 全系列/第八阶段:生产环境部署与协同开发/Docker 31422楼

package com.bjsxt;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * preparedStatement对象的使用
 * @author tui
 *
 */
public class PreparedStatementDemo {
	
	//向departments表插入一条数据
	public void insertDepartments(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) {
			// TODO: handle exception
		}finally {
			JdbcUtil.closeResource( ps, conn, null);
		}
	}
	//数据更新
	public void updateDepartments(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) {
			// TODO: handle exception
		}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 departemnts where department_id=?");
			ps.setInt(1, departmentId);
			rs=ps.executeQuery();
			while (rs.next()) {
				dept =new Departments();
				dept.setDepartmentId(rs.getInt("department_id"));
				dept.setDepartmentName(rs.getString("department_name"));
				dept.setLocationId(rs.getInt("location_id"));				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JdbcUtil.closeResource(ps, conn, rs);
		}
		return dept;
	}
	public static void main(String[] args) {
		PreparedStatementDemo demo=new PreparedStatementDemo();
		//demo.insertDepartments("人力资源10", 10);
		//demo.updateDepartments(9, "人力资源20", 20);
		Departments dept=demo.selectDepartmentsById(10);
			if (dept!=null) {
				System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+dept.getLocationId());
			}		
	}
}

老师,为什么查询数据selectDepartmentsById这个方法测试报空指针异常(主方法里把if条件删了报空指针异常)

JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 31423楼
JAVA 全系列/第六阶段:JavaWeb开发/Servlet技术详解 31424楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 31425楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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