Java程序设计(七)作业
题目1:编写一个应用程序,在主类Test1类中,创建两个链表List<E>对象,分别存储通过键盘输入的字符串内容——“chen”,“wang”,“liu”,“zhang”和“chen”,“hu”,“zhang”(假定输入的内容在各自的链表中没有重复的),输出显示这两个链表List<E>对象并集的结果。程序运行结果如下图1所示:
1 //题目1:编写一个应用程序,在主类Test1类中,创建两个链表List<E>对象,分别存储通过键盘输入的字符串内容 2 //——“chen”,“wang”,“liu”,“zhang”和“chen”,“hu”,“zhang” 3 //(假定输入的内容在各自的链表中没有重复的),输出显示这两个链表List<E>对象并集的结果。 4 5 import java.util.*; 6 7 public class Test1 { 8 9 public static void main(String args[]) { 10 getList(); 11 } 12 13 private static void getList() { 14 LinkedList<String> List1 = new LinkedList<String>(); 15 LinkedList<String> List2 = new LinkedList<String>(); 16 List<String> unionList = new ArrayList<String>(); 17 try { 18 19 Scanner reader = new Scanner(System.in); 20 System.out.println("please input list1: "); 21 String a = reader.nextLine(); 22 String b = reader.nextLine(); 23 String c = reader.nextLine(); 24 String d = reader.nextLine(); 25 List1.add(a); 26 List1.add(b); 27 List1.add(c); 28 List1.add(d); 29 System.out.println("please input list2: "); 30 String q = reader.nextLine(); 31 String w = reader.nextLine(); 32 String e = reader.nextLine(); 33 List2.add(q); 34 List2.add(w); 35 List2.add(e); 36 // 获取去重并集 37 unionList = receiveUnionList(List1, List2); 38 Iterator<String> unionIterator = unionList.iterator(); 39 System.out.println("Union: "); 40 while (unionIterator.hasNext()) { 41 System.out.println(unionIterator.next()); 42 } 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 } 47 48 public static List<String> receiveUnionList(List<String> List1, List<String> List2) { 49 List<String> resultList = new ArrayList<String>(); 50 Set<String> firstSet = new TreeSet<String>(List1); 51 for (String id : List1) { 52 firstSet.add(id); 53 } 54 resultList = new ArrayList<String>(firstSet); 55 return resultList; 56 } 57 }
题目2:编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计。
表1 t_student表结构
字段名 |
类型 |
说明 |
id |
int |
主键,自增长 |
name |
char(10) |
学生姓名 |
classes |
char(10) |
学生班级 |
之后,可根据显示的内容进行某条记录的删除(以id为条件)和修改(以id为条件,修改name字段值)。
1 //编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计。 2 2 //修改 3 3 4 4 import java.sql.Connection; 5 5 import java.sql.DriverManager; 6 6 import java.sql.ResultSet; 7 7 import java.sql.SQLException; 8 8 import java.sql.Statement; 9 9 import java.util.*; 10 10 11 11 public class Test_4 { 12 12 public static void main(String[] args) { 13 13 System.out.println("id: "); 14 14 Scanner reader = new Scanner(System.in); 15 15 String name = reader.next(); 16 16 if (1 > 0) { 17 17 try { 18 18 Class.forName("com.mysql.jdbc.Driver"); 19 19 } catch (ClassNotFoundException e) { 20 20 e.printStackTrace(); 21 21 } 22 22 try { 23 23 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu", "root", "dai324542"); 24 24 Statement sql = con.createStatement(); 25 25 ResultSet rs = sql.executeQuery("select * from t_student where id=" + "'" + name + "'"); 26 26 if (rs.next()) { 27 27 System.out.println("id已存在!"); 28 28 } else { 29 29 int n = sql.executeUpdate("insert into t_student(id) values('" + name + "'"); 30 30 } 31 31 System.out.println("please input id: "); 32 32 Scanner reader1 = new Scanner(System.in); 33 33 String name1 = reader1.next(); 34 34 System.out.println("please input new name: "); 35 35 String newname = reader.next(); 36 36 if (1 > 0) { 37 37 int n = sql.executeUpdate("update users set ='" + newname + "'"); 38 38 if (n > 0) { 39 39 System.out.println("success!"); 40 40 } else { 41 41 System.out.println("修改失败!"); 42 42 } 43 43 } 44 44 con.close(); 45 45 } catch (SQLException e) { 46 46 e.printStackTrace(); 47 47 } 48 48 } 49 49 } 50 50 }
1 /* 2 SQLyog Ultimate v11.11 (64 bit) 3 MySQL - 5.5.37 : Database - stu 4 ********************************************************************* 5 */ 6 7 8 /*!40101 SET NAMES utf8 */; 9 10 /*!40101 SET SQL_MODE=''*/; 11 12 /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 13 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 14 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 15 /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 16 CREATE DATABASE /*!32312 IF NOT EXISTS*/`stu` /*!40100 DEFAULT CHARACTER SET gbk */; 17 18 USE `stu`; 19 20 /*Table structure for table `t_student` */ 21 22 DROP TABLE IF EXISTS `t_student`; 23 24 CREATE TABLE `t_student` ( 25 `id` int(11) NOT NULL AUTO_INCREMENT, 26 `name` char(10) DEFAULT NULL, 27 `classes` char(10) DEFAULT NULL, 28 PRIMARY KEY (`id`) 29 ) ENGINE=InnoDB DEFAULT CHARSET=gbk; 30 31 /*Data for the table `t_student` */ 32 33 /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 34 /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 35 /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 36 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;