1.创建Person类,Person的属性有: Strng name 姓名 String sex 性别 Integer age 年龄, String idNo 身份证号 Boolean isMerried 是否已婚 请生成相应的getter、setter方法。请编写注解@Label,表示所注解对象的中文名称,请把@Label注解标注在Person类和Person的每个属性上面。请编写PersonInput类,负责提示录入人员的相关属性,提示必须是注解@Label所标注的中文名称。请编写PersonDisplay,负责显示人员信息,显示时的属性名称必须为注解@Label所标注的中文名称,PersonInput类与PersonDisplay类实现了共同的接口PersonAction,接口PersonAction有方法process,方法process的签名为:public Person process(Person person); 2.在第一题目的基础上,编写注解@Column,属性有Label 表示类的属性的显示名称,Nullable 表示是否允许属性值为空,MaxLength 表示文本属性的最大长度,MinLength表示文本属性的最小长度,MaxValue表示最大值,MinValue表示最小值,把注解@Column加在Person类的每个属性上,在输入Person时根据注解@Column的配置进行校验。第一题的@Label只标注在类上。根据注解生成Person类对应的数据库表创建语句,以及生成数据库表的删除、新增、修改SQL语句。 并利用JDBC,实现数据库操作。只生成第二题代码

时间: 2023-06-18 19:08:45 浏览: 164
先给出完整代码,再一步步解释。 ```java import java.lang.annotation.*; import java.util.Scanner; public class PersonInput implements PersonAction { private static Scanner input = new Scanner(System.in); @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); Column column = field.getAnnotation(Column.class); if (label != null) { System.out.print(label.value() + ": "); } if (column != null) { String inputStr = input.nextLine(); if (!column.nullable() && (inputStr == null || inputStr.length() == 0)) { System.out.println(column.label() + "不能为空"); inputStr = input.nextLine(); } if (column.maxLength() > 0 && inputStr.length() > column.maxLength()) { System.out.println(column.label() + "长度不能超过" + column.maxLength() + "个字符"); inputStr = input.nextLine(); } if (column.minLength() > 0 && inputStr.length() < column.minLength()) { System.out.println(column.label() + "长度不能少于" + column.minLength() + "个字符"); inputStr = input.nextLine(); } if (column.maxValue() > Long.MIN_VALUE) { long value = Long.parseLong(inputStr); if (value > column.maxValue()) { System.out.println(column.label() + "不能大于" + column.maxValue()); inputStr = input.nextLine(); } } if (column.minValue() < Long.MAX_VALUE) { long value = Long.parseLong(inputStr); if (value < column.minValue()) { System.out.println(column.label() + "不能小于" + column.minValue()); inputStr = input.nextLine(); } } try { if (field.getType() == String.class) { field.set(person, inputStr); } else if (field.getType() == Integer.class) { int value = Integer.parseInt(inputStr); field.set(person, value); } else if (field.getType() == Long.class) { long value = Long.parseLong(inputStr); field.set(person, value); } else if (field.getType() == Boolean.class) { boolean value = Boolean.parseBoolean(inputStr); field.set(person, value); } } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } import java.lang.annotation.*; public class PersonDisplay implements PersonAction { @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); if (label != null) { try { System.out.println(label.value() + ": " + field.get(person)); } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } @Target({ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface Label { String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Column { String label(); boolean nullable() default true; int maxLength() default 0; int minLength() default 0; long maxValue() default Long.MIN_VALUE; long minValue() default Long.MAX_VALUE; } interface PersonAction { Person process(Person person); } class Person { @Label("姓名") @Column(label = "姓名", nullable = false, maxLength = 32) private String name; @Label("性别") @Column(label = "性别") private String sex; @Label("年龄") @Column(label = "年龄", minValue = 0, maxValue = 200) private Integer age; @Label("身份证号") @Column(label = "身份证号", maxLength = 18) private String idNo; @Label("是否已婚") @Column(label = "是否已婚") private Boolean isMarried; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getIdNo() { return idNo; } public void setIdNo(String idNo) { this.idNo = idNo; } public Boolean getMarried() { return isMarried; } public void setMarried(Boolean married) { isMarried = married; } } public class Main { private static final String CREATE_TABLE_SQL = "CREATE TABLE `person` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `name` varchar(32) NOT NULL,\n" + " `sex` varchar(8) DEFAULT NULL,\n" + " `age` int(11) DEFAULT NULL,\n" + " `id_no` varchar(18) DEFAULT NULL,\n" + " `married` tinyint(1) DEFAULT NULL,\n" + " PRIMARY KEY (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"; private static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS `person`;"; private static final String INSERT_SQL = "INSERT INTO `person` (`name`, `sex`, `age`, `id_no`, `married`) VALUES (?, ?, ?, ?, ?);"; private static final String UPDATE_SQL = "UPDATE `person` SET `name`=?,`sex`=?,`age`=?,`id_no`=?,`married`=? WHERE `id`=?;"; private static final String DELETE_SQL = "DELETE FROM `person` WHERE `id`=?;"; public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "123456"); Statement stmt = conn.createStatement(); stmt.executeUpdate(DROP_TABLE_SQL); stmt.executeUpdate(CREATE_TABLE_SQL); PersonInput input = new PersonInput(); PersonDisplay display = new PersonDisplay(); Person person = new Person(); input.process(person); display.process(person); ps = conn.prepareStatement(INSERT_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.executeUpdate(); ps = conn.prepareStatement(UPDATE_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.setInt(6, 1); ps.executeUpdate(); ps = conn.prepareStatement(DELETE_SQL); ps.setInt(1, 1); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } } ``` 首先,我们定义了两个注解,@Label和@Column。其中,@Label是用来给类和属性添加中文名称的,@Column是用来给属性添加数据库字段的一些配置信息的。 ```java @Target({ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface Label { String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Column { String label(); boolean nullable() default true; int maxLength() default 0; int minLength() default 0; long maxValue() default Long.MIN_VALUE; long minValue() default Long.MAX_VALUE; } ``` 然后,我们定义了一个Person类,它有姓名、性别、年龄、身份证号、是否已婚等属性,每个属性都添加了@Label和@Column注解。我们使用注解处理器,可以根据@Column注解的配置信息来进行输入校验,这样就可以避免输入错误的数据。同时,我们还定义了PersonInput和PersonDisplay两个类,它们都实现了PersonAction接口,分别用于输入和输出一个Person对象。 ```java class Person { @Label("姓名") @Column(label = "姓名", nullable = false, maxLength = 32) private String name; @Label("性别") @Column(label = "性别") private String sex; @Label("年龄") @Column(label = "年龄", minValue = 0, maxValue = 200) private Integer age; @Label("身份证号") @Column(label = "身份证号", maxLength = 18) private String idNo; @Label("是否已婚") @Column(label = "是否已婚") private Boolean isMarried; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getIdNo() { return idNo; } public void setIdNo(String idNo) { this.idNo = idNo; } public Boolean getMarried() { return isMarried; } public void setMarried(Boolean married) { isMarried = married; } } interface PersonAction { Person process(Person person); } public class PersonInput implements PersonAction { @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); Column column = field.getAnnotation(Column.class); if (label != null) { System.out.print(label.value() + ": "); } if (column != null) { String inputStr = input.nextLine(); if (!column.nullable() && (inputStr == null || inputStr.length() == 0)) { System.out.println(column.label() + "不能为空"); inputStr = input.nextLine(); } if (column.maxLength() > 0 && inputStr.length() > column.maxLength()) { System.out.println(column.label() + "长度不能超过" + column.maxLength() + "个字符"); inputStr = input.nextLine(); } if (column.minLength() > 0 && inputStr.length() < column.minLength()) { System.out.println(column.label() + "长度不能少于" + column.minLength() + "个字符"); inputStr = input.nextLine(); } if (column.maxValue() > Long.MIN_VALUE) { long value = Long.parseLong(inputStr); if (value > column.maxValue()) { System.out.println(column.label() + "不能大于" + column.maxValue()); inputStr = input.nextLine(); } } if (column.minValue() < Long.MAX_VALUE) { long value = Long.parseLong(inputStr); if (value < column.minValue()) { System.out.println(column.label() + "不能小于" + column.minValue()); inputStr = input.nextLine(); } } try { if (field.getType() == String.class) { field.set(person, inputStr); } else if (field.getType() == Integer.class) { int value = Integer.parseInt(inputStr); field.set(person, value); } else if (field.getType() == Long.class) { long value = Long.parseLong(inputStr); field.set(person, value); } else if (field.getType() == Boolean.class) { boolean value = Boolean.parseBoolean(inputStr); field.set(person, value); } } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } public class PersonDisplay implements PersonAction { @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); if (label != null) { try { System.out.println(label.value() + ": " + field.get(person)); } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } ``` 最后,我们在Main类中使用JDBC,将Person对象保存到数据库中。在保存之前,先执行了一次删除表和创建表的操作。 ```java public class Main { private static final String CREATE_TABLE_SQL = "CREATE TABLE `person` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `name` varchar(32) NOT NULL,\n" + " `sex` varchar(8) DEFAULT NULL,\n" + " `age` int(11) DEFAULT NULL,\n" + " `id_no` varchar(18) DEFAULT NULL,\n" + " `married` tinyint(1) DEFAULT NULL,\n" + " PRIMARY KEY (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"; private static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS `person`;"; private static final String INSERT_SQL = "INSERT INTO `person` (`name`, `sex`, `age`, `id_no`, `married`) VALUES (?, ?, ?, ?, ?);"; private static final String UPDATE_SQL = "UPDATE `person` SET `name`=?,`sex`=?,`age`=?,`id_no`=?,`married`=? WHERE `id`=?;"; private static final String DELETE_SQL = "DELETE FROM `person` WHERE `id`=?;"; public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "123456"); Statement stmt = conn.createStatement(); stmt.executeUpdate(DROP_TABLE_SQL); stmt.executeUpdate(CREATE_TABLE_SQL); PersonInput input = new PersonInput(); PersonDisplay display = new PersonDisplay(); Person person = new Person(); input.process(person); display.process(person); ps = conn.prepareStatement(INSERT_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.executeUpdate(); ps = conn.prepareStatement(UPDATE_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.setInt(6, 1); ps.executeUpdate(); ps = conn.prepareStatement(DELETE_SQL); ps.setInt(1, 1);
阅读全文

相关推荐

最新推荐

recommend-type

基于多松弛(MRT)模型的格子玻尔兹曼方法(LBM)Matlab代码实现:模拟压力驱动流场与优化算法研究,使用多松弛(MRT)模型与格子玻尔兹曼方法(LBM)模拟压力驱动流的Matlab代码实现,使用

基于多松弛(MRT)模型的格子玻尔兹曼方法(LBM)Matlab代码实现:模拟压力驱动流场与优化算法研究,使用多松弛(MRT)模型与格子玻尔兹曼方法(LBM)模拟压力驱动流的Matlab代码实现,使用格子玻尔兹曼方法(LBM)模拟压力驱动流,多松弛(MRT)模型,Matlab代码 ,LBM; 驱动流; MRT模型; Matlab代码,LBM-MRT模型在Matlab中模拟压力驱动流
recommend-type

一个用 c 语言编写的文件加密与解密源码

应用场景 在数据传输和存储过程中,为了保护数据的安全性,需要对文件进行加密处理。本程序可以对文本文件进行简单的加密和解密操作。 实例说明 本程序使用简单的异或加密算法对文件进行加密和解密。用户可以选择加密或解密操作,并指定要处理的文件。
recommend-type

番茄助手-各个版本可用

简单修改过兼容问题,这个版本的番茄可以适用于vs2012-vs2017 其他版本没试过! 覆盖的版本据测试过没问题。
recommend-type

采用无差拍电流预测控制替代传统PI控制器,自适应电机参数辨识新模型问世,该模型创新应用无差拍电流预测控制替代传统PI控制器,结合电机参数自适应辨识技术,提升性能表现 ,该模型采用无差拿电流预测控制代替

采用无差拍电流预测控制替代传统PI控制器,自适应电机参数辨识新模型问世,该模型创新应用无差拍电流预测控制替代传统PI控制器,结合电机参数自适应辨识技术,提升性能表现。,该模型采用无差拿电流预测控制代替传统电流环的PI控制器,并采用模型参自适应对电机参数进行辨识 ,核心关键词:无差拍电流预测控制; 传统电流环PI控制器; 模型参数自适应; 电机参数辨识,无差拍电流预测控制与模型参自适应电机参数辨识模型
recommend-type

Spring Websocket快速实现与SSMTest实战应用

标题“websocket包”指代的是一个在计算机网络技术中应用广泛的组件或技术包。WebSocket是一种网络通信协议,它提供了浏览器与服务器之间进行全双工通信的能力。具体而言,WebSocket允许服务器主动向客户端推送信息,是实现即时通讯功能的绝佳选择。 描述中提到的“springwebsocket实现代码”,表明该包中的核心内容是基于Spring框架对WebSocket协议的实现。Spring是Java平台上一个非常流行的开源应用框架,提供了全面的编程和配置模型。在Spring中实现WebSocket功能,开发者通常会使用Spring提供的注解和配置类,简化WebSocket服务端的编程工作。使用Spring的WebSocket实现意味着开发者可以利用Spring提供的依赖注入、声明式事务管理、安全性控制等高级功能。此外,Spring WebSocket还支持与Spring MVC的集成,使得在Web应用中使用WebSocket变得更加灵活和方便。 直接在Eclipse上面引用,说明这个websocket包是易于集成的库或模块。Eclipse是一个流行的集成开发环境(IDE),支持Java、C++、PHP等多种编程语言和多种框架的开发。在Eclipse中引用一个库或模块通常意味着需要将相关的jar包、源代码或者配置文件添加到项目中,然后就可以在Eclipse项目中使用该技术了。具体操作可能包括在项目中添加依赖、配置web.xml文件、使用注解标注等方式。 标签为“websocket”,这表明这个文件或项目与WebSocket技术直接相关。标签是用于分类和快速检索的关键字,在给定的文件信息中,“websocket”是核心关键词,它表明该项目或文件的主要功能是与WebSocket通信协议相关的。 文件名称列表中的“SSMTest-master”暗示着这是一个版本控制仓库的名称,例如在GitHub等代码托管平台上。SSM是Spring、SpringMVC和MyBatis三个框架的缩写,它们通常一起使用以构建企业级的Java Web应用。这三个框架分别负责不同的功能:Spring提供核心功能;SpringMVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架;MyBatis是一个支持定制化SQL、存储过程以及高级映射的持久层框架。Master在这里表示这是项目的主分支。这表明websocket包可能是一个SSM项目中的模块,用于提供WebSocket通讯支持,允许开发者在一个集成了SSM框架的Java Web应用中使用WebSocket技术。 综上所述,这个websocket包可以提供给开发者一种简洁有效的方式,在遵循Spring框架原则的同时,实现WebSocket通信功能。开发者可以利用此包在Eclipse等IDE中快速开发出支持实时通信的Web应用,极大地提升开发效率和应用性能。
recommend-type

电力电子技术的智能化:数据中心的智能电源管理

# 摘要 本文探讨了智能电源管理在数据中心的重要性,从电力电子技术基础到智能化电源管理系统的实施,再到技术的实践案例分析和未来展望。首先,文章介绍了电力电子技术及数据中心供电架构,并分析了其在能效提升中的应用。随后,深入讨论了智能化电源管理系统的组成、功能、监控技术以及能
recommend-type

通过spark sql读取关系型数据库mysql中的数据

Spark SQL是Apache Spark的一个模块,它允许用户在Scala、Python或SQL上下文中查询结构化数据。如果你想从MySQL关系型数据库中读取数据并处理,你可以按照以下步骤操作: 1. 首先,你需要安装`PyMySQL`库(如果使用的是Python),它是Python与MySQL交互的一个Python驱动程序。在命令行输入 `pip install PyMySQL` 来安装。 2. 在Spark环境中,导入`pyspark.sql`库,并创建一个`SparkSession`,这是Spark SQL的入口点。 ```python from pyspark.sql imp
recommend-type

新版微软inspect工具下载:32位与64位版本

根据给定文件信息,我们可以生成以下知识点: 首先,从标题和描述中,我们可以了解到新版微软inspect.exe与inspect32.exe是两个工具,它们分别对应32位和64位的系统架构。这些工具是微软官方提供的,可以用来下载获取。它们源自Windows 8的开发者工具箱,这是一个集合了多种工具以帮助开发者进行应用程序开发与调试的资源包。由于这两个工具被归类到开发者工具箱,我们可以推断,inspect.exe与inspect32.exe是用于应用程序性能检测、问题诊断和用户界面分析的工具。它们对于开发者而言非常实用,可以在开发和测试阶段对程序进行深入的分析。 接下来,从标签“inspect inspect32 spy++”中,我们可以得知inspect.exe与inspect32.exe很有可能是微软Spy++工具的更新版或者是有类似功能的工具。Spy++是Visual Studio集成开发环境(IDE)的一个组件,专门用于Windows应用程序。它允许开发者观察并调试与Windows图形用户界面(GUI)相关的各种细节,包括窗口、控件以及它们之间的消息传递。使用Spy++,开发者可以查看窗口的句柄和类信息、消息流以及子窗口结构。新版inspect工具可能继承了Spy++的所有功能,并可能增加了新功能或改进,以适应新的开发需求和技术。 最后,由于文件名称列表仅提供了“ed5fa992d2624d94ac0eb42ee46db327”,没有提供具体的文件名或扩展名,我们无法从这个文件名直接推断出具体的文件内容或功能。这串看似随机的字符可能代表了文件的哈希值或是文件存储路径的一部分,但这需要更多的上下文信息来确定。 综上所述,新版的inspect.exe与inspect32.exe是微软提供的开发者工具,与Spy++有类似功能,可以用于程序界面分析、问题诊断等。它们是专门为32位和64位系统架构设计的,方便开发者在开发过程中对应用程序进行深入的调试和优化。同时,使用这些工具可以提高开发效率,确保软件质量。由于这些工具来自Windows 8的开发者工具箱,它们可能在兼容性、效率和用户体验上都经过了优化,能够为Windows应用的开发和调试提供更加专业和便捷的解决方案。
recommend-type

如何运用电力电子技术实现IT设备的能耗监控

# 摘要 随着信息技术的快速发展,IT设备能耗监控已成为提升能效和减少环境影响的关键环节。本文首先概述了电力电子技术与IT设备能耗监控的重要性,随后深入探讨了电力电子技术的基础原理及其在能耗监控中的应用。文章详细分析了IT设备能耗监控的理论框架、实践操作以及创新技术的应用,并通过节能改造案例展示了监控系统构建和实施的成效。最后,本文展望了未来能耗监控技术的发展趋势,同时
recommend-type

2635.656845多位小数数字,js不使用四舍五入保留两位小数,然后把结果千分位,想要的结果是2,635.65;如何处理

在JavaScript中,如果你想要将2635.656845这个数字精确地保留两位小数,并且去掉多余的千分位,可以使用`toFixed()`函数结合字符串切片的方法来实现。不过需要注意的是,`toFixed()`会返回一个字符串,所以我们需要先转换它。 以下是一个示例: ```javascript let num = 2635.656845; // 使用 toFixed() 保留两位小数,然后去掉多余的三位 let roundedNum = num.toFixed(2).substring(0, 5); // 如果最后一个字符是 '0',则进一步判断是否真的只有一位小数 if (round