版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 原创用JAVA来实现对数据库中信息地增删改查.txt6宽容润滑了彼此地关系,消除了彼此地隔阂,扫清了彼此地顾忌,增进了彼此地了解.这是前几天地习题,是一个宠物管理系统,就是对宠物地信息进行管理地一套系统宠物信息:宠物ID,宠物类别,宠物名字,宠物性别,宠物年龄,宠物入库日期系统完成功能:实现对宠物信息地录入,修改,删除,查询解决方法:一共创建了四个类:一个宠物类PetMessage 里面是宠物地信息 一个是数据库连接类DBCon 里面主要是完成数据连接功能 一个是宠物管理类PetDAO 完成对宠物信息地增删该查 最后一个就是一个测试类PetTest 完成对系统地测试PetMessage类:p
2、ackage petsys.Pet;import java.sql.*;public class PetMessage /宠物信息private int petId;private String petSort;private String petName;private String petSex;private int petAge;private String petDate;public int getPetAge() return petAge;public void setPetAge(int petAge) this.petAge = petAge;public int getP
3、etId() return petId;public void setPetId(int petId) this.petId = petId;public String getPetName() return petName;public void setPetName(String petName) this.petName = petName;public String getPetSex() return petSex;public void setPetSex(String petSex) this.petSex = petSex;public PetMessage() public
4、String getPetDate() return petDate;public void setPetDate(String petDate) this.petDate = petDate;public String getPetSort() return petSort;public void setPetSort(String petSort) this.petSort = petSort;/重载构造函数public PetMessage(int petId,String petSort,String petName,String petSex,int petAge,String pe
5、tDate) this.petId=petId; this.petSort=petSort; this.petName=petName; this.petSex=petSex; this.petAge=petAge; this.petDate=petDate;DBCon类:package petsys.Pet;import java.sql.*;public class DBCon public static Connection getCon() try Class.forName(com.mysql.jdbc.Driver).newInstance(); Connection con =
6、DriverManager.getConnection(jdbc:mysql:/localhost:3306/mydb,root,root); return con; catch (InstantiationException e) / TODO 自动生成 catch 块 e.printStackTrace(); catch (IllegalAccessException e) / TODO 自动生成 catch 块 e.printStackTrace(); catch (ClassNotFoundException e) / TODO 自动生成 catch 块 e.printStackTra
7、ce(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); return null;/用方法来实现对对象地关闭public static void closeAllMethod(Connection con,Statement stmt,ResultSet rs) try if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(con!=null) con.close(); catch (SQLException e) / TODO 自动生成 catch 块
8、 e.printStackTrace(); PetDAO类:package petsys.Pet;import java.sql.*;public class PetDAO /增添宠物信息public static void doAdd(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate
9、=pet.getPetDate(); /与数据库连接 Connection con=null; con=DBCon.getCon(); /创建会话 try Statement stmt=con.createStatement(); /定义sql语句 String sqlString=insert into petmessage(petid,petsort,petname,petsex,petage,petdate) values(+pet.getPetId()+,+pet.getPetSort()+,+pet.getPetName()+,+pet.getPetSex()+,+pet.getPe
10、tAge()+,+pet.getPetDate()+);/ 执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物添加成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /根据宠物ID删除宠物信息public static void doDeleteWithId(PetMessage p
11、et) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Statement stmt=con.createStatement(); /定义sql语句 String sqlStrin
12、g=delete from petmessage where petid=+pet.getPetId()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物删除成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /根据宠物名字删除宠物信息public static void d
13、oDeleteWithName(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Statement stmt=con.createStatement
14、(); /定义sql语句 String sqlString=delete from petmessage where petName=+pet.getPetName()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物删除成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /
15、根据宠物ID修改宠物信息public static void doUpdateWithID(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Stat
16、ement stmt=con.createStatement(); /定义sql语句 String sqlString=update petmessage set petName=+pet.getPetName()+ where petId=+pet.getPetId()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物信息修改成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLE
17、xception e) / TODO 自动生成 catch 块 e.printStackTrace(); /根据宠物名字修改宠物信息public static void doUpdateWithName(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate()
18、; /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Statement stmt=con.createStatement(); /定义sql语句 String sqlString=update petmessage set petAge=+pet.getPetAge()+ where petName=+pet.getPetName()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物信息修改成功!(o); System.out.pr
19、intln(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /按ID查询宠物信息public static void doSelectWithId(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int pe
20、tAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); try /创建语句对象 Statement stmt=con.createStatement(); /定义sql语句 String sqlString=select * from petMessage where petId=+pet.getPetId()+; /创建结果集 并执行sql语句 ResultSet rs=stmt.executeQuery(sqlString); /对结果集进行解析 Syste
21、m.out.println(查询结果如下:); while(rs.next() System.out.println(宠物ID: +rs.getInt(petId)+ 宠物种类:+ rs.getString(petSort)+ 宠物名字:+rs.getString(petName)+ 宠物性别:+rs.getString(petSex)+ 宠物年龄:+rs.getInt(petAge)+ 宠物入库时间:+rs.getString(petDate); DBCon.closeAllMethod(con,stmt,rs); catch (SQLException e) / TODO 自动生成 cat
22、ch 块 e.printStackTrace(); /按名字查询宠物信息public static void doSelectWithName(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); try /创建语句对象 Statement stmt=con.createStatement(); /定义sql语句 String sqlString=select * from petMessage where petName=+pet.getPetName()+; /创建结果集 并执行sql语句 ResultSet rs=stmt.executeQuery(sqlString); /对结果集进行解析 System.out.println(查
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026苏州市六年级语文小升初分班考原创仿真卷-可打印含作文范文评分标准
- 2026ios资深面试题及答案
- 2026js高频率面试题及答案
- 2026memcached面试题及答案
- 高职建筑工程技术专业二年级《全塑胶跑道系统施工技术与质量管理》教案
- 2026年事业单位公开招聘综合应用能力(B类)试题及答案
- 初中物理九年级电与磁中考专题复习教案
- 初中二年级英语 Unit 5 What a Delicious Meal!单元整体教学设计
- 小学五年级数学《展开与折叠:构建空间观念的探究之旅》教学设计
- 小学五年级心理健康教育《言语有度成长无界:拒绝“背后蛐蛐”》教学设计
- 45186-2024限制快递过度包装要求
- 医院电梯施工组织方案
- 二次搬运施工方案及措施
- 重大风险管控知识培训课件
- 国开2025年《数据库应用技术》形考作业1-4答案
- 湘江战役教学课件
- 高中数学《人工智能数学基础》教案(2025-2026学年)
- GB/T 191-2025包装储运图形符号标志
- WeleUnitDiscoveringUsefulStructures句子基本结构课件-高中英语人教版
- 医保基金管理培训课件
- 设计人工合同范本
评论
0/150
提交评论