用JAVA list集合语句增删改查全部学生信息。

用JAVA list集合语句增删改查全部学生信息。,第1张

1、首先在电脑上启动数据库 ,在数据库中创建表,下面给出具体的SQL语句。

2、然后打开eclipse 创建新项目 JDBCTest,需要导入相关的jar包并构建路径,如图。

3、接着创建entity实体层如图对应表中的数据。

4、创建数据连接层conn 用于MySQL数据库的连接代码如图 如图。

5、创建dao层持久层,在里面编写数据库表的增删改查的具体 *** 作。

6、最后编写测试类 Test代码如图,就完成了。

import java.util.*

import java.io.*

class StuMgr{

public static class Student{

public int id

public String name

public int age

public Student(int id ,String name,int age){

this.id = id

this.name = name

this.age = age

}

@Override

public String toString(){

return id + "," + name + "," + age

}

}

public List<Student> stuList = new LinkedList<>()

public void add(){

Scanner sc = new Scanner(System.in) 

System.out.println("请输入学生学号:")

String id = sc.nextLine()

int intId = 0

try{

intId = Integer.parseInt(id)

}catch(NumberFormatException ex){

System.out.println("学号输入有误,请输入数字!")

return

}

if (find(intId) != null){

System.out.println("该学号已经存在!")

return 

}

System.out.println("请输入学生姓名:")

String name = sc.nextLine()

System.out.println("请输入学生年龄:")

String age = sc.nextLine()

int intAge = 0

try{

intAge = Integer.parseInt(age)

}catch(NumberFormatException ex){

System.out.println("年龄输入有误,请输入数字!")

return

}

Student stu = new Student(intId,name,intAge)

stuList.add(stu)

store()

System.out.println("-----------------------")

System.out.println("学生信息已增加")

System.out.println(stu)

System.out.println("-----------------------")

}

public void del(){

Scanner sc = new Scanner(System.in) 

System.out.println("请输入学生学号:")

String id = sc.nextLine()

int intId = 0

try{

intId = Integer.parseInt(id)

}catch(NumberFormatException ex){

System.out.println("学号输入有误,请输入数字!")

return

}

Student stu = find(intId)

if ( stu == null){

System.out.println("该学号不存在!")

return 

}

stuList.remove(stu)

store()

System.out.println("-----------------------")

System.out.println("学生信息已删除")

System.out.println(stu)

System.out.println("-----------------------")

}

public void find(){

Scanner sc = new Scanner(System.in) 

System.out.println("请输入学生学号:")

String id = sc.nextLine()

int intId = 0

try{

intId = Integer.parseInt(id)

}catch(NumberFormatException ex){

System.out.println("学号输入有误,请输入数字!")

return

}

Student stu = find(intId)

if ( stu == null){

System.out.println("该学号不存在!")

return 

}

System.out.println("-----------------------")

System.out.println("查找学生信息如下")

System.out.println(stu)

System.out.println("-----------------------")

}

public Student find(int id){

for(Student stu : stuList){

if(stu.id == id){

return stu

}

}

return null

}

public void modify(){

store()

}

public void foreach(){

System.out.println("-----------------------")

for(Student stu : stuList){

System.out.println(stu)

}

System.out.println("-----------------------")

}

public void store(){

Iterator iterator = stuList.iterator()

File file = new File("stuList.txt")

        FileWriter fw = null

        BufferedWriter writer = null

        try {

            fw = new FileWriter(file)

            writer = new BufferedWriter(fw)

            while(iterator.hasNext()){

                writer.write(iterator.next().toString())

                writer.newLine()//换行

            }

            writer.flush()

        } catch (FileNotFoundException e) {

            e.printStackTrace()

        }catch (IOException e) {

            e.printStackTrace()

        }finally{

            try {

                writer.close()

                fw.close()

            } catch (IOException e) {

                e.printStackTrace()

            }

        }

}

public static void main(String[] args){

StuMgr mgr = new StuMgr()

while(true){

System.out.println("请选择您要进行的 *** 作:")

System.out.println("1:增加学生信息")

System.out.println("2:删除学生信息")

System.out.println("3:查找学生信息")

System.out.println("4:修改学生信息")

System.out.println("5:遍历学生信息")

System.out.println("6:退出")

System.out.println("-----------------------")

Scanner sc = new Scanner(System.in) 

String op = sc.nextLine()

if("6".equals(op)){

return

}

if("1".equals(op)){

mgr.add()

}

if("2".equals(op)){

mgr.del()

}

if("3".equals(op)){

mgr.find()

}

if("4".equals(op)){

mgr.modify()

}

if("5".equals(op)){

mgr.foreach()

}

}

}

}

时间仓促,还有一个modify方法没实现,留给你自己练手。

for (int i = 0i <services.lengthi++) {

List<String>stu=new ArrayList<String>()

stu.set(index, element)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11748541.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存