版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025 小学六年级语文下册 综合性学习 活动设计课件
- 【项目方案】233KWh 定制户外一体柜储能系统项目技术方案
- 注册会计师就业前景分析
- 跨境电商2025年海运整箱保险协议
- 2025 小学六年级语文上册综合性学习轻叩诗歌大门课件
- 科技研发终止协议2025年成果转化条款
- 2025 小学六年级语文上册借代修辞手法课件
- 浙江省丽水市2025年九年级上学期期末考试数学试卷附答案
- 股权架构方案(后附模板)
- 赣州医院面试题及答案
- 2025至2030年中国汽车用碳纤维行业竞争格局分析及市场需求前景报告
- 焊接作业指导书完整版
- T/CHEC 007-2021自动平移门安装验收技术规范
- 2025年部编版道德与法治六年级上册全册教案设计(共4个单元含有教学计划)
- 招标代理公司制度与流程汇编
- 课题申报书:“职教出海”战略下中国职业教育国际化路径与策略研究
- 课程设计说明书
- 2025年广东省粤科金融集团有限公司招聘笔试参考题库含答案解析
- 2025年中国纳秒紫外激光器行业市场运行现状及投资规划建议报告
- 房屋租赁合同(附房屋交割清单)
- 奥特曼涂色画简笔画直接打印版A4
评论
0/150
提交评论